1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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.StringBundler;
20  import com.liferay.portal.kernel.util.StringPool;
21  import com.liferay.portal.kernel.util.StringUtil;
22  import com.liferay.portal.kernel.util.Validator;
23  
24  import java.io.InputStream;
25  
26  import java.net.URL;
27  
28  import java.util.Collections;
29  import java.util.HashMap;
30  import java.util.Locale;
31  import java.util.Map;
32  import java.util.Properties;
33  import java.util.concurrent.ConcurrentHashMap;
34  
35  /**
36   * <a href="LanguageResources.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Shuyang Zhou
39   */
40  public class LanguageResources {
41  
42      public static String getMessage(Locale locale, String key) {
43          if (locale == null) {
44              return null;
45          }
46  
47          Map<String, String> languageMap = _languageMaps.get(locale);
48  
49          if (languageMap == null) {
50              languageMap = loadLocale(locale);
51          }
52  
53          String value = languageMap.get(key);
54  
55          if (value == null) {
56              return getMessage(getSuperLocale(locale), key);
57          }
58          else {
59              return value;
60          }
61      }
62  
63      public static Map<String, String> putLanguageMap(
64          Locale locale, Map<String, String> languageMap) {
65  
66          return _languageMaps.put(locale, languageMap);
67      }
68  
69      public void setConfig(String config) {
70          _config = config;
71      }
72  
73      private static Locale getSuperLocale(Locale locale) {
74          if (Validator.isNotNull(locale.getVariant())) {
75              return new Locale(locale.getLanguage(), locale.getCountry());
76          }
77  
78          if (Validator.isNotNull(locale.getCountry())) {
79              return new Locale(locale.getLanguage());
80          }
81  
82          if (Validator.isNotNull(locale.getLanguage())) {
83              return new Locale(StringPool.BLANK);
84          }
85  
86          return null;
87      }
88  
89      private static Map<String, String> loadLocale(Locale locale) {
90          String[] names = StringUtil.split(
91              _config.replace(StringPool.PERIOD, StringPool.SLASH));
92  
93          Map<String, String> languageMap = null;
94  
95          if (names.length > 0) {
96              String localeName = locale.toString();
97  
98              languageMap = new HashMap<String, String>();
99  
100             for (String name : names) {
101                 StringBundler sb = new StringBundler(4);
102 
103                 sb.append(name);
104 
105                 if (localeName.length() > 0) {
106                     sb.append(StringPool.UNDERLINE);
107                     sb.append(localeName);
108                 }
109 
110                 sb.append(".properties");
111 
112                 Properties properties = loadProperties(sb.toString());
113 
114                 for (Map.Entry<Object, Object> entry : properties.entrySet()) {
115                     String key = (String)entry.getKey();
116                     String value = (String)entry.getValue();
117 
118                     languageMap.put(key, value);
119                 }
120             }
121         }
122         else {
123             languageMap = Collections.EMPTY_MAP;
124         }
125 
126         _languageMaps.put(locale, languageMap);
127 
128         return languageMap;
129     }
130 
131     private static Properties loadProperties(String name) {
132         Properties properties = new Properties();
133 
134         try {
135             ClassLoader classLoader = LanguageResources.class.getClassLoader();
136 
137             URL url = classLoader.getResource(name);
138 
139             if (_log.isInfoEnabled()) {
140                 _log.info("Attempting to load " + name);
141             }
142 
143             if (url != null) {
144                 InputStream inputStream = url.openStream();
145 
146                 properties.load(inputStream);
147 
148                 inputStream.close();
149 
150                 if (_log.isInfoEnabled()) {
151                     _log.info(
152                         "Loading " + url + " with " + properties.size() +
153                             " values");
154                 }
155             }
156         }
157         catch (Exception e) {
158             if (_log.isWarnEnabled()) {
159                 _log.warn(e, e);
160             }
161         }
162 
163         return properties;
164     }
165 
166     private static Log _log = LogFactoryUtil.getLog(LanguageResources.class);
167 
168     private static String _config;
169     private static Map<Locale, Map<String, String>> _languageMaps =
170         new ConcurrentHashMap<Locale, Map<String, String>>(64);
171 
172 }