001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.kernel.util;
022:
023: import com.liferay.portal.kernel.log.Log;
024: import com.liferay.portal.kernel.log.LogFactoryUtil;
025:
026: import java.util.HashMap;
027: import java.util.Locale;
028: import java.util.Map;
029:
030: /**
031: * <a href="LocaleUtil.java.html"><b><i>View Source</i></b></a>
032: *
033: * @author Brian Wing Shun Chan
034: *
035: */
036: public class LocaleUtil {
037:
038: public static Locale fromLanguageId(String languageId) {
039: return _instance._fromLanguageId(languageId);
040: }
041:
042: public static Locale[] fromLanguageIds(String[] languageIds) {
043: return _instance._fromLanguageIds(languageIds);
044: }
045:
046: public static Locale getDefault() {
047: return _instance._getDefault();
048: }
049:
050: public static void setDefault(String userLanguage,
051: String userCountry, String userVariant) {
052:
053: _instance._setDefault(userLanguage, userCountry, userVariant);
054: }
055:
056: public static String toLanguageId(Locale locale) {
057: return _instance._toLanguageId(locale);
058: }
059:
060: public static String[] toLanguageIds(Locale[] locales) {
061: return _instance._toLanguageIds(locales);
062: }
063:
064: private LocaleUtil() {
065: _locale = new Locale("en", "US");
066: }
067:
068: private Locale _fromLanguageId(String languageId) {
069: Locale locale = null;
070:
071: try {
072: locale = (Locale) _locales.get(languageId);
073:
074: if (locale == null) {
075: int pos = languageId.indexOf(StringPool.UNDERLINE);
076:
077: if (pos == -1) {
078: locale = new Locale(languageId);
079: } else {
080: String languageCode = languageId.substring(0, pos);
081: String countryCode = languageId.substring(pos + 1,
082: languageId.length());
083:
084: locale = new Locale(languageCode, countryCode);
085: }
086:
087: _locales.put(languageId, locale);
088: }
089: } catch (Exception e) {
090: if (_log.isWarnEnabled()) {
091: _log.warn(languageId + " is not a valid language id");
092: }
093: }
094:
095: if (locale == null) {
096: locale = _locale;
097: }
098:
099: return locale;
100: }
101:
102: private Locale[] _fromLanguageIds(String[] languageIds) {
103: Locale[] locales = new Locale[languageIds.length];
104:
105: for (int i = 0; i < languageIds.length; i++) {
106: locales[i] = _fromLanguageId(languageIds[i]);
107: }
108:
109: return locales;
110: }
111:
112: private Locale _getDefault() {
113: return _locale;
114: }
115:
116: public void _setDefault(String userLanguage, String userCountry,
117: String userVariant) {
118:
119: if (Validator.isNotNull(userLanguage)
120: && Validator.isNull(userCountry)
121: && Validator.isNull(userVariant)) {
122:
123: _locale = new Locale(userLanguage);
124: } else if (Validator.isNotNull(userLanguage)
125: && Validator.isNotNull(userCountry)
126: && Validator.isNull(userVariant)) {
127:
128: _locale = new Locale(userLanguage, userCountry);
129: } else if (Validator.isNotNull(userLanguage)
130: && Validator.isNotNull(userCountry)
131: && Validator.isNotNull(userVariant)) {
132:
133: _locale = new Locale(userLanguage, userCountry, userVariant);
134: }
135: }
136:
137: private String _toLanguageId(Locale locale) {
138: if (locale == null) {
139: locale = _locale;
140: }
141:
142: StringMaker sm = new StringMaker();
143:
144: sm.append(locale.getLanguage());
145:
146: if (Validator.isNotNull(locale.getCountry())) {
147: sm.append(StringPool.UNDERLINE);
148: sm.append(locale.getCountry());
149: }
150:
151: return sm.toString();
152: }
153:
154: private String[] _toLanguageIds(Locale[] locales) {
155: String[] languageIds = new String[locales.length];
156:
157: for (int i = 0; i < locales.length; i++) {
158: languageIds[i] = _toLanguageId(locales[i]);
159: }
160:
161: return languageIds;
162: }
163:
164: private static Log _log = LogFactoryUtil.getLog(LocaleUtil.class);
165:
166: private static LocaleUtil _instance = new LocaleUtil();
167:
168: private Locale _locale;
169: private Map _locales = new HashMap();
170:
171: }
|