This project has retired. For details please refer to its Attic page.
LocaleUtil xref
View Javadoc

1   /*
2    * $Id: LocaleUtil.java 667964 2008-06-15 15:00:54Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.tiles.util;
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  import java.util.Locale;
26  
27  /***
28   * Utilities for locale manipulation.
29   *
30   * @version $Rev: 667964 $ $Date: 2008-06-15 17:00:54 +0200 (dom, 15 giu 2008) $
31   * @since 2.1.0
32   */
33  public final class LocaleUtil {
34  
35      /***
36       * The "null" Locale, i.e. a Locale that points to no real locale.
37       *
38       * @since 2.1.0
39       */
40      public static final Locale NULL_LOCALE = new Locale("");
41  
42      /***
43       * Private constructor to avoid instantiation.
44       */
45      private LocaleUtil() {
46      }
47  
48      /***
49       * Calculate the postfixes along the search path from the base bundle to the
50       * bundle specified by baseName and locale. Method copied from
51       * java.util.ResourceBundle
52       *
53       * @param locale The locale.
54       * @return a list of postfixes to add to filenames.
55       * @since 2.1.0
56       */
57      public static List<String> calculatePostfixes(Locale locale) {
58          final List<String> result = new ArrayList<String>();
59          // The default configuration file must be loaded to allow correct
60          // definition inheritance.
61          result.add("");
62  
63          if (locale == null) {
64              return result;
65          }
66  
67          final String language = locale.getLanguage();
68          final int languageLength = language.length();
69          final String country = locale.getCountry();
70          final int countryLength = country.length();
71          final String variant = locale.getVariant();
72          final int variantLength = variant.length();
73  
74          if (languageLength + countryLength + variantLength == 0) {
75              // The locale is "", "", "".
76              return result;
77          }
78  
79          final StringBuffer temp = new StringBuffer();
80          temp.append('_');
81          temp.append(language);
82  
83          if (languageLength > 0) {
84              result.add(temp.toString());
85          }
86  
87          if (countryLength + variantLength == 0) {
88              return result;
89          }
90  
91          temp.append('_');
92          temp.append(country);
93  
94          if (countryLength > 0) {
95              result.add(temp.toString());
96          }
97  
98          if (variantLength == 0) {
99              return result;
100         } else {
101             temp.append('_');
102             temp.append(variant);
103             result.add(temp.toString());
104             return result;
105         }
106     }
107 
108     /***
109      * Calculate the postfix to append to a filename to load the correct single
110      * filename for that Locale.
111      *
112      * @param locale The locale.
113      * @return The postfix to append to the filename.
114      * @since 2.1.0
115      */
116     public static String calculatePostfix(Locale locale) {
117         if (locale == null) {
118             return "";
119         }
120 
121         StringBuilder builder = new StringBuilder();
122         String language = locale.getLanguage();
123         String country = locale.getCountry();
124         String variant = locale.getVariant();
125         if (!"".equals(language)) {
126             builder.append("_");
127             builder.append(language);
128             if (!"".equals(country)) {
129                 builder.append("_");
130                 builder.append(country);
131                 if (!"".equals(variant)) {
132                     builder.append("_");
133                     builder.append(variant);
134                 }
135             }
136         }
137         return builder.toString();
138     }
139 
140     /***
141      * Concat postfix to the name. Take care of existing filename extension.
142      * Transform the given name "name.ext" to have "name" + "postfix" + "ext".
143      * If there is no ext, return "name" + "postfix".
144      *
145      * @param name Filename.
146      * @param postfix Postfix to add.
147      * @return Concatenated filename.
148      * @since 2.1.0
149      */
150     public static String concatPostfix(String name, String postfix) {
151         if (postfix == null || "".equals(postfix)) {
152             return name;
153         }
154 
155         // Search file name extension.
156         // take care of Unix files starting with .
157         int dotIndex = name.lastIndexOf(".");
158         int lastNameStart = name.lastIndexOf(java.io.File.pathSeparator);
159         if (dotIndex < 1 || dotIndex < lastNameStart) {
160             return name + postfix;
161         }
162 
163         String ext = name.substring(dotIndex);
164         name = name.substring(0, dotIndex);
165         return name + postfix + ext;
166     }
167 
168     /***
169      * <p>
170      * Returns the "parent" locale of a given locale.
171      * </p>
172      * <p>
173      * If the original locale is only language-based, the {@link #NULL_LOCALE}
174      * object is returned.
175      * </p>
176      * <p>
177      * If the original locale is {@link #NULL_LOCALE}, then <code>null</code>
178      * is returned.
179      * </p>
180      *
181      * @param locale The original locale.
182      * @return The parent locale.
183      */
184     public static Locale getParentLocale(Locale locale) {
185         Locale retValue = null;
186         String language = locale.getLanguage();
187         String country = locale.getCountry();
188         String variant = locale.getVariant();
189         if (!"".equals(variant)) {
190             retValue = new Locale(language, country);
191         } else if (!"".equals(country)) {
192             retValue = new Locale(language);
193         } else if (!"".equals(language)) {
194             retValue = NULL_LOCALE;
195         }
196 
197         return retValue;
198     }
199 }