1   /**
2    * Copyright (c) 2000-2008 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 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  
74      private Locale _fromLanguageId(String languageId) {
75          Locale locale = null;
76  
77          try {
78              locale = _locales.get(languageId);
79  
80              if (locale == null) {
81                  int pos = languageId.indexOf(StringPool.UNDERLINE);
82  
83                  if (pos == -1) {
84                      locale = new Locale(languageId);
85                  }
86                  else {
87                      String languageCode = languageId.substring(0, pos);
88                      String countryCode = languageId.substring(
89                          pos + 1, languageId.length());
90  
91                      locale = new Locale(languageCode, countryCode);
92                  }
93  
94                  _locales.put(languageId, locale);
95              }
96          }
97          catch (Exception e) {
98              if (_log.isWarnEnabled()) {
99                  _log.warn(languageId + " is not a valid language id");
100             }
101         }
102 
103         if (locale == null) {
104             locale = _locale;
105         }
106 
107         return locale;
108     }
109 
110     private Locale[] _fromLanguageIds(String[] languageIds) {
111         Locale[] locales = new Locale[languageIds.length];
112 
113         for (int i = 0; i < languageIds.length; i++) {
114             locales[i] = _fromLanguageId(languageIds[i]);
115         }
116 
117         return locales;
118     }
119 
120     private Locale _getDefault() {
121         return _locale;
122     }
123 
124     public void _setDefault(
125         String userLanguage, String userCountry, String userVariant) {
126 
127         if (Validator.isNotNull(userLanguage) &&
128             Validator.isNull(userCountry) && Validator.isNull(userVariant)) {
129 
130             _locale = new Locale(userLanguage);
131         }
132         else if (Validator.isNotNull(userLanguage) &&
133                  Validator.isNotNull(userCountry) &&
134                  Validator.isNull(userVariant)) {
135 
136             _locale = new Locale(userLanguage, userCountry);
137         }
138         else if (Validator.isNotNull(userLanguage) &&
139                  Validator.isNotNull(userCountry) &&
140                  Validator.isNotNull(userVariant)) {
141 
142             _locale = new Locale(userLanguage, userCountry, userVariant);
143         }
144     }
145 
146     private String _toLanguageId(Locale locale) {
147         if (locale == null) {
148             locale = _locale;
149         }
150 
151         StringBuilder sb = new StringBuilder();
152 
153         sb.append(locale.getLanguage());
154 
155         if (Validator.isNotNull(locale.getCountry())) {
156             sb.append(StringPool.UNDERLINE);
157             sb.append(locale.getCountry());
158         }
159 
160         return sb.toString();
161     }
162 
163     private String[] _toLanguageIds(Locale[] locales) {
164         String[] languageIds = new String[locales.length];
165 
166         for (int i = 0; i < locales.length; i++) {
167             languageIds[i] = _toLanguageId(locales[i]);
168         }
169 
170         return languageIds;
171     }
172 
173     private static Log _log = LogFactoryUtil.getLog(LocaleUtil.class);
174 
175     private static LocaleUtil _instance = new LocaleUtil();
176 
177     private Locale _locale;
178     private Map<String, Locale> _locales = new HashMap<String, Locale>();
179 
180 }