1/*2 * $Id: LocaleUtil.java 667964 2008-06-15 15:00:54Z apetrelli $3 *4 * Licensed to the Apache Software Foundation (ASF) under one5 * or more contributor license agreements. See the NOTICE file6 * distributed with this work for additional information7 * regarding copyright ownership. The ASF licenses this file8 * to you under the Apache License, Version 2.0 (the9 * "License"); you may not use this file except in compliance10 * with the License. You may obtain a copy of the License at11 *12 * http://www.apache.org/licenses/LICENSE-2.013 *14 * Unless required by applicable law or agreed to in writing,15 * software distributed under the License is distributed on an16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17 * KIND, either express or implied. See the License for the18 * specific language governing permissions and limitations19 * under the License.20 */21package org.apache.tiles.util;
2223import java.util.ArrayList;
24import java.util.List;
25import java.util.Locale;
2627/***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.032 */33publicfinalclassLocaleUtil {
3435/***36 * The "null" Locale, i.e. a Locale that points to no real locale.37 *38 * @since 2.1.039 */40publicstaticfinal Locale NULL_LOCALE = new Locale("");
4142/***43 * Private constructor to avoid instantiation.44 */45privateLocaleUtil() {
46 }
4748/***49 * Calculate the postfixes along the search path from the base bundle to the50 * bundle specified by baseName and locale. Method copied from51 * java.util.ResourceBundle52 *53 * @param locale The locale.54 * @return a list of postfixes to add to filenames.55 * @since 2.1.056 */57publicstatic List<String> calculatePostfixes(Locale locale) {
58final List<String> result = new ArrayList<String>();
59// The default configuration file must be loaded to allow correct60// definition inheritance.61 result.add("");
6263if (locale == null) {
64return result;
65 }
6667final String language = locale.getLanguage();
68finalint languageLength = language.length();
69final String country = locale.getCountry();
70finalint countryLength = country.length();
71final String variant = locale.getVariant();
72finalint variantLength = variant.length();
7374if (languageLength + countryLength + variantLength == 0) {
75// The locale is "", "", "".76return result;
77 }
7879final StringBuffer temp = new StringBuffer();
80 temp.append('_');
81 temp.append(language);
8283if (languageLength > 0) {
84 result.add(temp.toString());
85 }
8687if (countryLength + variantLength == 0) {
88return result;
89 }
9091 temp.append('_');
92 temp.append(country);
9394if (countryLength > 0) {
95 result.add(temp.toString());
96 }
9798if (variantLength == 0) {
99return result;
100 } else {
101 temp.append('_');
102 temp.append(variant);
103 result.add(temp.toString());
104return result;
105 }
106 }
107108/***109 * Calculate the postfix to append to a filename to load the correct single110 * filename for that Locale.111 *112 * @param locale The locale.113 * @return The postfix to append to the filename.114 * @since 2.1.0115 */116publicstatic String calculatePostfix(Locale locale) {
117if (locale == null) {
118return"";
119 }
120121 StringBuilder builder = new StringBuilder();
122 String language = locale.getLanguage();
123 String country = locale.getCountry();
124 String variant = locale.getVariant();
125if (!"".equals(language)) {
126 builder.append("_");
127 builder.append(language);
128if (!"".equals(country)) {
129 builder.append("_");
130 builder.append(country);
131if (!"".equals(variant)) {
132 builder.append("_");
133 builder.append(variant);
134 }
135 }
136 }
137return builder.toString();
138 }
139140/***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.0149 */150publicstatic String concatPostfix(String name, String postfix) {
151if (postfix == null || "".equals(postfix)) {
152return name;
153 }
154155// Search file name extension.156// take care of Unix files starting with .157int dotIndex = name.lastIndexOf(".");
158int lastNameStart = name.lastIndexOf(java.io.File.pathSeparator);
159if (dotIndex < 1 || dotIndex < lastNameStart) {
160return name + postfix;
161 }
162163 String ext = name.substring(dotIndex);
164 name = name.substring(0, dotIndex);
165return name + postfix + ext;
166 }
167168/***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 */184publicstatic Locale getParentLocale(Locale locale) {
185 Locale retValue = null;
186 String language = locale.getLanguage();
187 String country = locale.getCountry();
188 String variant = locale.getVariant();
189if (!"".equals(variant)) {
190 retValue = new Locale(language, country);
191 } elseif (!"".equals(country)) {
192 retValue = new Locale(language);
193 } elseif (!"".equals(language)) {
194 retValue = NULL_LOCALE;
195 }
196197return retValue;
198 }
199 }