1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.util;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  
28  import java.util.Arrays;
29  import java.util.HashMap;
30  import java.util.Locale;
31  import java.util.Map;
32  
33  /**
34   * <a href="LocaleUtil.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   */
38  public class LocaleUtil {
39  
40      public static Locale fromLanguageId(String languageId) {
41          return _instance._fromLanguageId(languageId);
42      }
43  
44      public static Locale[] fromLanguageIds(String[] languageIds) {
45          return _instance._fromLanguageIds(languageIds);
46      }
47  
48      public static Locale getDefault() {
49          return _instance._getDefault();
50      }
51  
52      public static LocaleUtil getInstance() {
53          return _instance;
54      }
55  
56      public static void setDefault(
57          String userLanguage, String userCountry, String userVariant) {
58  
59          _instance._setDefault(userLanguage, userCountry, userVariant);
60      }
61  
62      public static String toLanguageId(Locale locale) {
63          return _instance._toLanguageId(locale);
64      }
65  
66      public static String[] toLanguageIds(Locale[] locales) {
67          return _instance._toLanguageIds(locales);
68      }
69  
70      private LocaleUtil() {
71          _locale = new Locale("en", "US");
72  
73          _isoCountries = Locale.getISOCountries().clone();
74  
75          for (int i = 0; i < _isoCountries.length; i++) {
76              _isoCountries[i] = _isoCountries[i].toUpperCase();
77          }
78  
79          Arrays.sort(_isoCountries);
80  
81          _isoLanguages = Locale.getISOLanguages().clone();
82  
83          for (int i = 0; i < _isoLanguages.length; i++) {
84              _isoLanguages[i] = _isoLanguages[i].toLowerCase();
85          }
86  
87          Arrays.sort(_isoLanguages);
88      }
89  
90      private Locale _fromLanguageId(String languageId) {
91          if (languageId == null) {
92              return _locale;
93          }
94  
95          Locale locale = null;
96  
97          try {
98              locale = _locales.get(languageId);
99  
100             if (locale == null) {
101                 int pos = languageId.indexOf(StringPool.UNDERLINE);
102 
103                 if (pos == -1) {
104                     if (Arrays.binarySearch(_isoLanguages, languageId) < 0) {
105                         return _getDefault();
106                     }
107 
108                     locale = new Locale(languageId);
109                 }
110                 else {
111                     String languageCode = languageId.substring(0, pos);
112                     String countryCode = languageId.substring(
113                         pos + 1, languageId.length());
114 
115                     if ((Arrays.binarySearch(
116                             _isoLanguages, languageCode) < 0) ||
117                         (Arrays.binarySearch(_isoCountries, countryCode) < 0)) {
118 
119                         return _getDefault();
120                     }
121 
122                     locale = new Locale(languageCode, countryCode);
123                 }
124 
125                 _locales.put(languageId, locale);
126             }
127         }
128         catch (Exception e) {
129             if (_log.isWarnEnabled()) {
130                 _log.warn(languageId + " is not a valid language id");
131             }
132         }
133 
134         if (locale == null) {
135             locale = _locale;
136         }
137 
138         return locale;
139     }
140 
141     private Locale[] _fromLanguageIds(String[] languageIds) {
142         Locale[] locales = new Locale[languageIds.length];
143 
144         for (int i = 0; i < languageIds.length; i++) {
145             locales[i] = _fromLanguageId(languageIds[i]);
146         }
147 
148         return locales;
149     }
150 
151     private Locale _getDefault() {
152         return _locale;
153     }
154 
155     public void _setDefault(
156         String userLanguage, String userCountry, String userVariant) {
157 
158         if (Validator.isNotNull(userLanguage) &&
159             Validator.isNull(userCountry) && Validator.isNull(userVariant)) {
160 
161             _locale = new Locale(userLanguage);
162         }
163         else if (Validator.isNotNull(userLanguage) &&
164                  Validator.isNotNull(userCountry) &&
165                  Validator.isNull(userVariant)) {
166 
167             _locale = new Locale(userLanguage, userCountry);
168         }
169         else if (Validator.isNotNull(userLanguage) &&
170                  Validator.isNotNull(userCountry) &&
171                  Validator.isNotNull(userVariant)) {
172 
173             _locale = new Locale(userLanguage, userCountry, userVariant);
174         }
175     }
176 
177     private String _toLanguageId(Locale locale) {
178         if (locale == null) {
179             locale = _locale;
180         }
181 
182         StringBuilder sb = new StringBuilder();
183 
184         sb.append(locale.getLanguage());
185 
186         if (Validator.isNotNull(locale.getCountry())) {
187             sb.append(StringPool.UNDERLINE);
188             sb.append(locale.getCountry());
189         }
190 
191         return sb.toString();
192     }
193 
194     private String[] _toLanguageIds(Locale[] locales) {
195         String[] languageIds = new String[locales.length];
196 
197         for (int i = 0; i < locales.length; i++) {
198             languageIds[i] = _toLanguageId(locales[i]);
199         }
200 
201         return languageIds;
202     }
203 
204     private static Log _log = LogFactoryUtil.getLog(LocaleUtil.class);
205 
206     private static LocaleUtil _instance = new LocaleUtil();
207 
208     private String[] _isoCountries;
209     private String[] _isoLanguages;
210     private Locale _locale;
211     private Map<String, Locale> _locales = new HashMap<String, Locale>();
212 
213 }