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.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  public class LocaleUtil {
38  
39      public static Locale fromLanguageId(String languageId) {
40          return _instance._fromLanguageId(languageId);
41      }
42  
43      public static Locale[] fromLanguageIds(String[] languageIds) {
44          return _instance._fromLanguageIds(languageIds);
45      }
46  
47      public static Locale getDefault() {
48          return _instance._getDefault();
49      }
50  
51      public static LocaleUtil getInstance() {
52          return _instance;
53      }
54  
55      public static void setDefault(
56          String userLanguage, String userCountry, String userVariant) {
57  
58          _instance._setDefault(userLanguage, userCountry, userVariant);
59      }
60  
61      public static String toLanguageId(Locale locale) {
62          return _instance._toLanguageId(locale);
63      }
64  
65      public static String[] toLanguageIds(Locale[] locales) {
66          return _instance._toLanguageIds(locales);
67      }
68  
69      private LocaleUtil() {
70          _locale = new Locale("en", "US");
71      }
72  
73      private Locale _fromLanguageId(String languageId) {
74          if (languageId == null) {
75              return _locale;
76          }
77  
78          Locale locale = null;
79  
80          try {
81              locale = _locales.get(languageId);
82  
83              if (locale == null) {
84                  int pos = languageId.indexOf(StringPool.UNDERLINE);
85  
86                  if (pos == -1) {
87                      locale = new Locale(languageId);
88                  }
89                  else {
90                      String languageCode = languageId.substring(0, pos);
91                      String countryCode = languageId.substring(
92                          pos + 1, languageId.length());
93  
94                      locale = new Locale(languageCode, countryCode);
95                  }
96  
97                  _locales.put(languageId, locale);
98              }
99          }
100         catch (Exception e) {
101             if (_log.isWarnEnabled()) {
102                 _log.warn(languageId + " is not a valid language id");
103             }
104         }
105 
106         if (locale == null) {
107             locale = _locale;
108         }
109 
110         return locale;
111     }
112 
113     private Locale[] _fromLanguageIds(String[] languageIds) {
114         Locale[] locales = new Locale[languageIds.length];
115 
116         for (int i = 0; i < languageIds.length; i++) {
117             locales[i] = _fromLanguageId(languageIds[i]);
118         }
119 
120         return locales;
121     }
122 
123     private Locale _getDefault() {
124         return _locale;
125     }
126 
127     public void _setDefault(
128         String userLanguage, String userCountry, String userVariant) {
129 
130         if (Validator.isNotNull(userLanguage) &&
131             Validator.isNull(userCountry) && Validator.isNull(userVariant)) {
132 
133             _locale = new Locale(userLanguage);
134         }
135         else if (Validator.isNotNull(userLanguage) &&
136                  Validator.isNotNull(userCountry) &&
137                  Validator.isNull(userVariant)) {
138 
139             _locale = new Locale(userLanguage, userCountry);
140         }
141         else if (Validator.isNotNull(userLanguage) &&
142                  Validator.isNotNull(userCountry) &&
143                  Validator.isNotNull(userVariant)) {
144 
145             _locale = new Locale(userLanguage, userCountry, userVariant);
146         }
147     }
148 
149     private String _toLanguageId(Locale locale) {
150         if (locale == null) {
151             locale = _locale;
152         }
153 
154         StringBuilder sb = new StringBuilder();
155 
156         sb.append(locale.getLanguage());
157 
158         if (Validator.isNotNull(locale.getCountry())) {
159             sb.append(StringPool.UNDERLINE);
160             sb.append(locale.getCountry());
161         }
162 
163         return sb.toString();
164     }
165 
166     private String[] _toLanguageIds(Locale[] locales) {
167         String[] languageIds = new String[locales.length];
168 
169         for (int i = 0; i < locales.length; i++) {
170             languageIds[i] = _toLanguageId(locales[i]);
171         }
172 
173         return languageIds;
174     }
175 
176     private static Log _log = LogFactoryUtil.getLog(LocaleUtil.class);
177 
178     private static LocaleUtil _instance = new LocaleUtil();
179 
180     private Locale _locale;
181     private Map<String, Locale> _locales = new HashMap<String, Locale>();
182 
183 }