1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portal.language;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.PropertiesUtil;
20  import com.liferay.portal.kernel.util.StringBundler;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.kernel.util.StringUtil;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.portal.tools.LangBuilder;
25  
26  import java.io.InputStream;
27  
28  import java.net.URL;
29  
30  import java.util.Collections;
31  import java.util.HashMap;
32  import java.util.Locale;
33  import java.util.Map;
34  import java.util.Properties;
35  import java.util.concurrent.ConcurrentHashMap;
36  
37  /**
38   * <a href="LanguageResources.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Shuyang Zhou
41   */
42  public class LanguageResources {
43  
44      public static String fixValue(String value) {
45          if (value.endsWith(LangBuilder.AUTOMATIC_COPY)) {
46              value = value.substring(
47                  0, value.length() - LangBuilder.AUTOMATIC_COPY.length());
48          }
49  
50          if (value.endsWith(LangBuilder.AUTOMATIC_TRANSLATION)) {
51              value = value.substring(
52                  0,
53                  value.length() - LangBuilder.AUTOMATIC_TRANSLATION.length());
54          }
55  
56          return value;
57      }
58  
59      public static String getMessage(Locale locale, String key) {
60          if (locale == null) {
61              return null;
62          }
63  
64          Map<String, String> languageMap = _languageMaps.get(locale);
65  
66          if (languageMap == null) {
67              languageMap = _loadLocale(locale);
68          }
69  
70          String value = languageMap.get(key);
71  
72          if (value == null) {
73              return getMessage(_getSuperLocale(locale), key);
74          }
75          else {
76              return value;
77          }
78      }
79  
80      public static Map<String, String> putLanguageMap(
81          Locale locale, Map<String, String> languageMap) {
82  
83          Map<String, String> oldLanguageMap = _languageMaps.get(locale);
84  
85          if (oldLanguageMap == null) {
86              _loadLocale(locale);
87              oldLanguageMap = _languageMaps.get(locale);
88          }
89  
90          Map<String, String> newLanguageMap = new HashMap<String, String>();
91  
92          if (oldLanguageMap != null) {
93              newLanguageMap.putAll(oldLanguageMap);
94          }
95  
96          newLanguageMap.putAll(languageMap);
97  
98          _languageMaps.put(locale, newLanguageMap);
99  
100         return oldLanguageMap;
101     }
102 
103     public void setConfig(String config) {
104         _config = config;
105     }
106 
107     private static Locale _getSuperLocale(Locale locale) {
108         if (Validator.isNotNull(locale.getVariant())) {
109             return new Locale(locale.getLanguage(), locale.getCountry());
110         }
111 
112         if (Validator.isNotNull(locale.getCountry())) {
113             return new Locale(locale.getLanguage());
114         }
115 
116         if (Validator.isNotNull(locale.getLanguage())) {
117             return new Locale(StringPool.BLANK);
118         }
119 
120         return null;
121     }
122 
123     private static Map<String, String> _loadLocale(Locale locale) {
124         String[] names = StringUtil.split(
125             _config.replace(StringPool.PERIOD, StringPool.SLASH));
126 
127         Map<String, String> languageMap = null;
128 
129         if (names.length > 0) {
130             String localeName = locale.toString();
131 
132             languageMap = new HashMap<String, String>();
133 
134             for (String name : names) {
135                 StringBundler sb = new StringBundler(4);
136 
137                 sb.append(name);
138 
139                 if (localeName.length() > 0) {
140                     sb.append(StringPool.UNDERLINE);
141                     sb.append(localeName);
142                 }
143 
144                 sb.append(".properties");
145 
146                 Properties properties = _loadProperties(sb.toString());
147 
148                 for (Map.Entry<Object, Object> entry : properties.entrySet()) {
149                     String key = (String)entry.getKey();
150                     String value = (String)entry.getValue();
151 
152                     value = fixValue(value);
153 
154                     languageMap.put(key, value);
155                 }
156             }
157         }
158         else {
159             languageMap = Collections.EMPTY_MAP;
160         }
161 
162         _languageMaps.put(locale, languageMap);
163 
164         return languageMap;
165     }
166 
167     private static Properties _loadProperties(String name) {
168         Properties properties = new Properties();
169 
170         try {
171             ClassLoader classLoader = LanguageResources.class.getClassLoader();
172 
173             URL url = classLoader.getResource(name);
174 
175             if (_log.isInfoEnabled()) {
176                 _log.info("Attempting to load " + name);
177             }
178 
179             if (url != null) {
180                 InputStream inputStream = url.openStream();
181 
182                 properties = PropertiesUtil.load(inputStream, StringPool.UTF8);
183 
184                 inputStream.close();
185 
186                 if (_log.isInfoEnabled()) {
187                     _log.info(
188                         "Loading " + url + " with " + properties.size() +
189                             " values");
190                 }
191             }
192         }
193         catch (Exception e) {
194             if (_log.isWarnEnabled()) {
195                 _log.warn(e, e);
196             }
197         }
198 
199         return properties;
200     }
201 
202     private static Log _log = LogFactoryUtil.getLog(LanguageResources.class);
203 
204     private static String _config;
205     private static Map<Locale, Map<String, String>> _languageMaps =
206         new ConcurrentHashMap<Locale, Map<String, String>>(64);
207 
208 }