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