1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.HashMap;
29  import java.util.Locale;
30  import java.util.Map;
31  
32  /**
33   * <a href="LocaleUtil.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Brian Wing Shun Chan
36   *
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 void setDefault(
53          String userLanguage, String userCountry, String userVariant) {
54  
55          _instance._setDefault(userLanguage, userCountry, userVariant);
56      }
57  
58      public static String toLanguageId(Locale locale) {
59          return _instance._toLanguageId(locale);
60      }
61  
62      public static String[] toLanguageIds(Locale[] locales) {
63          return _instance._toLanguageIds(locales);
64      }
65  
66      private LocaleUtil() {
67          _locale = new Locale("en", "US");
68      }
69  
70      private Locale _fromLanguageId(String languageId) {
71          Locale locale = null;
72  
73          try {
74              locale = (Locale)_locales.get(languageId);
75  
76              if (locale == null) {
77                  int pos = languageId.indexOf(StringPool.UNDERLINE);
78  
79                  if (pos == -1) {
80                      locale = new Locale(languageId);
81                  }
82                  else {
83                      String languageCode = languageId.substring(0, pos);
84                      String countryCode = languageId.substring(
85                          pos + 1, languageId.length());
86  
87                      locale = new Locale(languageCode, countryCode);
88                  }
89  
90                  _locales.put(languageId, locale);
91              }
92          }
93          catch (Exception e) {
94              if (_log.isWarnEnabled()) {
95                  _log.warn(languageId + " is not a valid language id");
96              }
97          }
98  
99          if (locale == null) {
100             locale = _locale;
101         }
102 
103         return locale;
104     }
105 
106     private Locale[] _fromLanguageIds(String[] languageIds) {
107         Locale[] locales = new Locale[languageIds.length];
108 
109         for (int i = 0; i < languageIds.length; i++) {
110             locales[i] = _fromLanguageId(languageIds[i]);
111         }
112 
113         return locales;
114     }
115 
116     private Locale _getDefault() {
117         return _locale;
118     }
119 
120     public void _setDefault(
121         String userLanguage, String userCountry, String userVariant) {
122 
123         if (Validator.isNotNull(userLanguage) &&
124             Validator.isNull(userCountry) && Validator.isNull(userVariant)) {
125 
126             _locale = new Locale(userLanguage);
127         }
128         else if (Validator.isNotNull(userLanguage) &&
129                  Validator.isNotNull(userCountry) &&
130                  Validator.isNull(userVariant)) {
131 
132             _locale = new Locale(userLanguage, userCountry);
133         }
134         else if (Validator.isNotNull(userLanguage) &&
135                  Validator.isNotNull(userCountry) &&
136                  Validator.isNotNull(userVariant)) {
137 
138             _locale = new Locale(userLanguage, userCountry, userVariant);
139         }
140     }
141 
142     private String _toLanguageId(Locale locale) {
143         if (locale == null) {
144             locale = _locale;
145         }
146 
147         StringMaker sm = new StringMaker();
148 
149         sm.append(locale.getLanguage());
150 
151         if (Validator.isNotNull(locale.getCountry())) {
152             sm.append(StringPool.UNDERLINE);
153             sm.append(locale.getCountry());
154         }
155 
156         return sm.toString();
157     }
158 
159     private String[] _toLanguageIds(Locale[] locales) {
160         String[] languageIds = new String[locales.length];
161 
162         for (int i = 0; i < locales.length; i++) {
163             languageIds[i] = _toLanguageId(locales[i]);
164         }
165 
166         return languageIds;
167     }
168 
169     private static Log _log = LogFactoryUtil.getLog(LocaleUtil.class);
170 
171     private static LocaleUtil _instance = new LocaleUtil();
172 
173     private Locale _locale;
174     private Map _locales = new HashMap();
175 
176 }