root / branches / v2_0_0_prep / libraries / libInternationalization / src / org / gvsig / i18n / Messages.java @ 38608
History | View | Annotate | Download (24 KB)
1 | 4780 | cesar | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|---|---|
2 | *
|
||
3 | 13649 | cesar | * Copyright (C) 2006-2007 IVER T.I. and Generalitat Valenciana.
|
4 | 4780 | cesar | *
|
5 | * This program is free software; you can redistribute it and/or
|
||
6 | * modify it under the terms of the GNU General Public License
|
||
7 | * as published by the Free Software Foundation; either version 2
|
||
8 | * of the License, or (at your option) any later version.
|
||
9 | *
|
||
10 | * This program is distributed in the hope that it will be useful,
|
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
13 | * GNU General Public License for more details.
|
||
14 | *
|
||
15 | * You should have received a copy of the GNU General Public License
|
||
16 | * along with this program; if not, write to the Free Software
|
||
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
18 | *
|
||
19 | * For more information, contact:
|
||
20 | *
|
||
21 | * Generalitat Valenciana
|
||
22 | * Conselleria d'Infraestructures i Transport
|
||
23 | * Av. Blasco Ib??ez, 50
|
||
24 | * 46010 VALENCIA
|
||
25 | * SPAIN
|
||
26 | *
|
||
27 | * +34 963862235
|
||
28 | * gvsig@gva.es
|
||
29 | * www.gvsig.gva.es
|
||
30 | *
|
||
31 | * or
|
||
32 | *
|
||
33 | * IVER T.I. S.A
|
||
34 | * Salamanca 50
|
||
35 | * 46005 Valencia
|
||
36 | * Spain
|
||
37 | *
|
||
38 | * +34 963163400
|
||
39 | * dac@iver.es
|
||
40 | */
|
||
41 | |||
42 | 4460 | cesar | package org.gvsig.i18n; |
43 | |||
44 | 5767 | cesar | import java.io.File; |
45 | 13649 | cesar | import java.io.IOException; |
46 | import java.io.InputStream; |
||
47 | 4587 | cesar | import java.net.MalformedURLException; |
48 | import java.net.URL; |
||
49 | 13649 | cesar | import java.text.MessageFormat; |
50 | 5767 | cesar | import java.util.ArrayList; |
51 | import java.util.Enumeration; |
||
52 | 28469 | jmvivo | import java.util.HashSet; |
53 | 13649 | cesar | import java.util.IllegalFormatException; |
54 | 8010 | cesar | import java.util.Iterator; |
55 | 5767 | cesar | import java.util.Locale; |
56 | 13649 | cesar | import java.util.Properties; |
57 | import java.util.Set; |
||
58 | 4587 | cesar | |
59 | 25650 | vcaballero | import org.slf4j.Logger; |
60 | import org.slf4j.LoggerFactory; |
||
61 | 4460 | cesar | |
62 | /**
|
||
63 | 4728 | cesar | * <p>This class offers some methods to provide internationalization services
|
64 | * to other projects. All the methods are static.</p>
|
||
65 | 25650 | vcaballero | *
|
66 | 4728 | cesar | * <p>The most useful method is {@link #getText(String) getText(key)} (and family),
|
67 | * which returns the translation associated
|
||
68 | * with the provided key. The class must be initialized before getText can be
|
||
69 | * used.</p>
|
||
70 | 25650 | vcaballero | *
|
71 | 4728 | cesar | * <p>The typical usage sequence would be:</p>
|
72 | * <ul>
|
||
73 | * <li>Add some locale to the prefered locales list: <code>Messages.addLocale(new Locale("es"))</code></li>
|
||
74 | * <li>Add some resource file containing translations: <code>Messages.addResourceFamily("text", new File("."))</code></li>
|
||
75 | * <li>And finaly getText can be used: <code>Messages.getText("aceptar")</code></li>
|
||
76 | * </ul>
|
||
77 | 25650 | vcaballero | *
|
78 | 4728 | cesar | * <p>The resource files are Java properties files, which contain <code>key=translation</code>
|
79 | * pairs, one
|
||
80 | * pair per line. These files must be encoded using iso-8859-1 encoding, and unicode escaped
|
||
81 | * sequences must be used to include characters outside the former encoding.
|
||
82 | * There are several ways to specify the property file to load, see the different
|
||
83 | 25650 | vcaballero | * addResourceFamily methods for details.</p>
|
84 | *
|
||
85 | 6294 | cesar | * @author Cesar Martinez Izquierdo (cesar.martinez@iver.es)
|
86 | 4460 | cesar | *
|
87 | */
|
||
88 | public class Messages { |
||
89 | 25650 | vcaballero | private static Logger logger = LoggerFactory.getLogger("Messages"); |
90 | 4516 | cesar | private static String _CLASSNAME = "org.gvsig.i18n.Messages"; |
91 | 25650 | vcaballero | |
92 | 5767 | cesar | /* Each entry will contain a hashmap with translations. Each hasmap
|
93 | * contains the translations for one language, indexed by the
|
||
94 | * translation key. The translations for language (i) in the preferred locales
|
||
95 | * list are contained in the position (i) of the localeResources list */
|
||
96 | 25650 | vcaballero | private static ArrayList localeResources = new ArrayList(); |
97 | 5767 | cesar | private static ArrayList preferredLocales = new ArrayList(); // contains the ordered list of prefered languages/locales (class Locale) |
98 | 25650 | vcaballero | |
99 | 28469 | jmvivo | |
100 | /* Set of resource families and classloaders used to load i18n resources. */
|
||
101 | private static Set resourceFamilies = new HashSet(); |
||
102 | private static Set classLoaders = new HashSet(); |
||
103 | |||
104 | 13649 | cesar | /*
|
105 | * The language considered the origin of translations, which will
|
||
106 | * (possibly) be stored in a property file without language suffix
|
||
107 | * (ie: text.properties instead of text_es.properties).
|
||
108 | */
|
||
109 | private static String baseLanguage = "es"; |
||
110 | 28469 | jmvivo | private static Locale baseLocale = new Locale(baseLanguage); |
111 | 25650 | vcaballero | |
112 | 4460 | cesar | /**
|
113 | 4728 | cesar | * <p>Gets the localized message associated with the provided key.
|
114 | * If the key is not in the dictionary, return the key and register
|
||
115 | * the failure in the log.</p>
|
||
116 | 25650 | vcaballero | *
|
117 | 4728 | cesar | * <p>The <code>callerName</code> parameter is only
|
118 | * used as a label when logging, so any String can be used. However, a
|
||
119 | * meaningful String should be used, such as the name of the class requiring
|
||
120 | * the translation services, in order to identify the source of the failure
|
||
121 | 25650 | vcaballero | * in the log.</p>
|
122 | *
|
||
123 | * @param key An String which identifies the translation that we want to get.
|
||
124 | 4516 | cesar | * @param callerName A symbolic name given to the caller of this method, to
|
125 | 4728 | cesar | * show it in the log if the key was not found
|
126 | * @return an String with the message associated with the provided key.
|
||
127 | * If the key is not in the dictionary, return the key. If the key
|
||
128 | * is null, return null.
|
||
129 | 4460 | cesar | */
|
130 | 4516 | cesar | public static String getText(String key, String callerName) { |
131 | 28469 | jmvivo | if (key==null) { |
132 | return null; |
||
133 | } |
||
134 | 5767 | cesar | for (int numLocale=0; numLocale<localeResources.size(); numLocale++) { |
135 | // try to get the translation for any of the languagues in the preferred languages list
|
||
136 | 13649 | cesar | String translation = ((Properties)localeResources.get(numLocale)).getProperty(key); |
137 | 28469 | jmvivo | if (translation!=null && !translation.equals("")) { |
138 | 5767 | cesar | return translation;
|
139 | 28469 | jmvivo | } |
140 | 5767 | cesar | } |
141 | 13649 | cesar | logger.warn(callerName+ " -- Cannot find translation for "+key);
|
142 | 4460 | cesar | return key;
|
143 | } |
||
144 | 25650 | vcaballero | |
145 | 13649 | cesar | public static String getText(String key, String[] arguments, String callerName) { |
146 | String translation = getText(key, callerName);
|
||
147 | 38608 | jjdelcerro | if (translation!=null && arguments!=null ) { |
148 | 13649 | cesar | try {
|
149 | translation = MessageFormat.format(translation, arguments);
|
||
150 | } |
||
151 | catch (IllegalFormatException ex) { |
||
152 | logger.error(callerName+" -- Error formating key: "+key+" -- "+translation); |
||
153 | } |
||
154 | } |
||
155 | return translation;
|
||
156 | } |
||
157 | 25650 | vcaballero | |
158 | 4460 | cesar | /**
|
159 | 4728 | cesar | * <p>Gets the localized message associated with the provided key.
|
160 | 6664 | cesar | * If the key is not in the dictionary or the translation is empty,
|
161 | * return the key and register the failure in the log.</p>
|
||
162 | 25650 | vcaballero | *
|
163 | 4728 | cesar | * @param key An String which identifies the translation that we want to get.
|
164 | * @return an String with the message associated with the provided key.
|
||
165 | 6664 | cesar | * If the key is not in the dictionary or the translation is empty,
|
166 | * return the key. If the key is null, return null.
|
||
167 | 4518 | cesar | */
|
168 | public static String getText(String key) { |
||
169 | 5767 | cesar | return getText(key, _CLASSNAME);
|
170 | 4518 | cesar | } |
171 | 25650 | vcaballero | |
172 | 13649 | cesar | public static String getText(String key, String[] arguments) { |
173 | return getText(key, arguments, _CLASSNAME);
|
||
174 | } |
||
175 | 25650 | vcaballero | |
176 | 4518 | cesar | /**
|
177 | 4728 | cesar | * <p>Gets the localized message associated with the provided key.
|
178 | 6664 | cesar | * If the key is not in the dictionary or the translation is empty,
|
179 | * it returns null and the failure is only registered in the log if
|
||
180 | * the param log is true.</p>
|
||
181 | 25650 | vcaballero | *
|
182 | 6664 | cesar | * @param key An String which identifies the translation that we want
|
183 | * to get.
|
||
184 | * @param log Determines whether log a key failure or not
|
||
185 | * @return an String with the message associated with the provided key,
|
||
186 | * or null if the key is not in the dictionary or the
|
||
187 | * translation is empty.
|
||
188 | 4460 | cesar | */
|
189 | 4728 | cesar | public static String getText(String key, boolean log) { |
190 | 5767 | cesar | return getText(key, _CLASSNAME, log);
|
191 | 4736 | cesar | } |
192 | 25650 | vcaballero | |
193 | 13649 | cesar | public static String getText(String key, String[] arguments, boolean log) { |
194 | String translation = getText(key, _CLASSNAME, log);
|
||
195 | 38608 | jjdelcerro | if (translation!=null && arguments!=null ) { |
196 | 13649 | cesar | try {
|
197 | translation = MessageFormat.format(translation, arguments);
|
||
198 | 38608 | jjdelcerro | } catch (IllegalFormatException ex) { |
199 | 13649 | cesar | if (log) {
|
200 | logger.error(_CLASSNAME+" -- Error formating key: "+key+" -- "+translation); |
||
201 | } |
||
202 | } |
||
203 | } |
||
204 | return translation;
|
||
205 | } |
||
206 | 25650 | vcaballero | |
207 | 4736 | cesar | /**
|
208 | * <p>Gets the localized message associated with the provided key.
|
||
209 | * If the key is not in the dictionary, it returns null and the failure
|
||
210 | * is only registered in the log if the param log is true.</p>
|
||
211 | 25650 | vcaballero | *
|
212 | 4736 | cesar | * @param key An String which identifies the translation that we want to get.
|
213 | * @param callerName A symbolic name given to the caller of this method, to
|
||
214 | * show it in the log if the key was not found
|
||
215 | * @param log Determines whether log a key failure or not
|
||
216 | * @return an String with the message associated with the provided key,
|
||
217 | * or null if the key is not in the dictionary.
|
||
218 | */
|
||
219 | public static String getText(String key, String callerName, boolean log) { |
||
220 | 28469 | jmvivo | if (key==null) { |
221 | return null; |
||
222 | } |
||
223 | 5767 | cesar | for (int numLocale=0; numLocale<localeResources.size(); numLocale++) { |
224 | // try to get the translation for any of the languagues in the preferred languages list
|
||
225 | 13649 | cesar | String translation = ((Properties)localeResources.get(numLocale)).getProperty(key); |
226 | 28469 | jmvivo | if (translation!=null && !translation.equals("")) { |
227 | 5767 | cesar | return translation;
|
228 | 28469 | jmvivo | } |
229 | 5767 | cesar | } |
230 | 5599 | cesar | if (log) {
|
231 | 13649 | cesar | logger.warn(callerName+" -- Cannot find translation for "+key);
|
232 | 5599 | cesar | } |
233 | 5767 | cesar | return null; |
234 | 4736 | cesar | } |
235 | 25650 | vcaballero | |
236 | 13649 | cesar | public static String getText(String key, String[] arguments, String callerName, boolean log) { |
237 | String translation = getText(key, callerName, log);
|
||
238 | if (translation!=null) { |
||
239 | try {
|
||
240 | translation = MessageFormat.format(translation, arguments);
|
||
241 | } |
||
242 | catch (IllegalFormatException ex) { |
||
243 | if (log) {
|
||
244 | logger.error(callerName+" -- Error formating key: "+key+" -- "+translation); |
||
245 | } |
||
246 | } |
||
247 | } |
||
248 | return translation;
|
||
249 | } |
||
250 | 4516 | cesar | |
251 | 4460 | cesar | /**
|
252 | 4728 | cesar | * <p>Adds an additional family of resource files containing some translations.
|
253 | 4460 | cesar | * A family is a group of files with a common baseName.
|
254 | 4728 | cesar | * The file must be an iso-8859-1 encoded file, which can contain any unicode
|
255 | * character using unicode escaped sequences, and following the syntax:
|
||
256 | * <code>key1=value1
|
||
257 | * key2=value2</code>
|
||
258 | 4460 | cesar | * where 'key1' is the key used to identify the string and must not
|
259 | 4728 | cesar | * contain the '=' symbol, and 'value1' is the associated translation.</p>
|
260 | * <p<For example:</p>
|
||
261 | * <code>cancel=Cancelar
|
||
262 | * accept=Aceptar</code>
|
||
263 | * <p>Only one pair key-value is allowed per line.</p>
|
||
264 | 25650 | vcaballero | *
|
265 | 4728 | cesar | * <p>The actual name of the resource file to load is determined using the rules
|
266 | * explained in the class java.util.ResourceBundle. Summarizing, for each language
|
||
267 | * in the specified preferred locales list it will try to load a file with
|
||
268 | * the following structure: <code>family_locale.properties</code></p>
|
||
269 | *
|
||
270 | * <p>For example, if the preferred locales list contains {"fr", "es", "en"}, and
|
||
271 | * the family name is "text", it will try to load the files "text_fr.properties",
|
||
272 | * "text_es.properties" and finally "text_en.properties".</p>
|
||
273 | 25650 | vcaballero | *
|
274 | 4728 | cesar | * <p>Locales might be more specific, such us "es_AR" (meaning Spanish from Argentina)
|
275 | * or "es_AR_linux" (meaning Linux system preferring Spanish from Argentina). In the
|
||
276 | * later case, it will try to load "text_es_AR_linux.properties", then
|
||
277 | * "text_es_AR.properties" if the former fails, and finally "text_es.properties".</p>
|
||
278 | 25650 | vcaballero | *
|
279 | 4728 | cesar | * <p>The directory used to locate the resource file is determining by using the
|
280 | * getResource method from the provided ClassLoader.</p>
|
||
281 | 25650 | vcaballero | *
|
282 | 4728 | cesar | * @param family The family name (or base name) which is used to search
|
283 | * actual properties files.
|
||
284 | * @param loader A ClassLoader which is able to find a property file matching
|
||
285 | * the specified family name and the preferred locales
|
||
286 | * @see <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/ResourceBundle.html">ResourceBundle</a>
|
||
287 | 4460 | cesar | */
|
288 | 4516 | cesar | public static void addResourceFamily(String family, ClassLoader loader) { |
289 | 5767 | cesar | addResourceFamily(family, loader, "");
|
290 | 4516 | cesar | } |
291 | 25650 | vcaballero | |
292 | 4728 | cesar | /**
|
293 | * <p>Adds an additional family of resource files containing some translations.
|
||
294 | * The search path to locate the files is provided by the dirList parameter.</p>
|
||
295 | 25650 | vcaballero | *
|
296 | 4728 | cesar | * <p>See {@link addResourceFamily(String, ClassLoader)} for a discussion about the
|
297 | * format of the property files and the way to determine the candidat files
|
||
298 | 25650 | vcaballero | * to load. Note that those methods are different in the way to locate the
|
299 | 4728 | cesar | * candidat files. This method searches in the provided paths (<code>dirList</code>
|
300 | * parameter), while the referred method searches using the getResource method
|
||
301 | * of the provided ClassLoader.</p>
|
||
302 | 25650 | vcaballero | *
|
303 | 4728 | cesar | * @param family The family name (or base name) which is used to search
|
304 | * actual properties files.
|
||
305 | * @param dirList A list of search paths to locate the property files
|
||
306 | * @throws MalformedURLException
|
||
307 | * @see <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/ResourceBundle.html">ResourceBundle</a>
|
||
308 | */
|
||
309 | 4587 | cesar | public static void addResourceFamily(String family, File[] dirList) throws MalformedURLException{ |
310 | 4597 | cesar | // use our own classloader
|
311 | 25650 | vcaballero | URL[] urls = new URL[dirList.length]; |
312 | |||
313 | 4587 | cesar | int i;
|
314 | for (i=0; i<urls.length; i++) { |
||
315 | urls[i] = dirList[i].toURL(); |
||
316 | } |
||
317 | 25650 | vcaballero | |
318 | 5868 | cesar | ClassLoader loader = new MessagesClassLoader(urls); |
319 | 5767 | cesar | addResourceFamily(family, loader, "");
|
320 | 4587 | cesar | } |
321 | 4516 | cesar | |
322 | 4728 | cesar | /**
|
323 | * <p>Adds an additional family of resource files containing some translations.
|
||
324 | * The search path to locate the files is provided by the dir parameter.</p>
|
||
325 | 25650 | vcaballero | *
|
326 | 4728 | cesar | * <p>See {@link addResourceFamily(String, ClassLoader)} for a discussion about the
|
327 | * format of the property files and the way to determine the candidat files
|
||
328 | 25650 | vcaballero | * to load. Note that those methods are different in the way to locate the
|
329 | 4728 | cesar | * candidat files. This method searches in the provided path (<code>dir</code>
|
330 | * parameter), while the referred method searches using the getResource method
|
||
331 | * of the provided ClassLoader.</p>
|
||
332 | 25650 | vcaballero | *
|
333 | 4728 | cesar | * @param family The family name (or base name) which is used to search
|
334 | * actual properties files.
|
||
335 | * @param dir The search path to locate the property files
|
||
336 | * @throws MalformedURLException
|
||
337 | * @see <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/ResourceBundle.html">ResourceBundle</a>
|
||
338 | */
|
||
339 | 4587 | cesar | public static void addResourceFamily(String family, File dir) throws MalformedURLException{ |
340 | 4597 | cesar | // use our own classloader
|
341 | 25650 | vcaballero | URL[] urls = new URL[1]; |
342 | 4587 | cesar | urls[0] = dir.toURL();
|
343 | 5868 | cesar | ClassLoader loader = new MessagesClassLoader(urls); |
344 | 5767 | cesar | addResourceFamily(family, loader, "");
|
345 | 4587 | cesar | } |
346 | |||
347 | |||
348 | 4516 | cesar | /**
|
349 | 4728 | cesar | * <p>Adds an additional family of resource files containing some translations.
|
350 | * The search path is determined by the getResource method from the
|
||
351 | * provided ClassLoader.</p>
|
||
352 | 25650 | vcaballero | *
|
353 | 4728 | cesar | * <p>This method is identical to {@link addResourceFamily(String, ClassLoader)},
|
354 | * except that it adds a <pode>callerName</code> parameter to show in the log.</p>
|
||
355 | 25650 | vcaballero | *
|
356 | 4728 | cesar | * <p>See {@link addResourceFamily(String, ClassLoader)} for a discussion about the
|
357 | * format of the property files andthe way to determine the candidat files
|
||
358 | * to load.</p>
|
||
359 | 25650 | vcaballero | *
|
360 | 4728 | cesar | * @param family The family name (or base name) which is used to search
|
361 | * actual properties files.
|
||
362 | * @param loader A ClassLoader which is able to find a property file matching
|
||
363 | * the specified family name and the preferred locales
|
||
364 | * @param callerName A symbolic name given to the caller of this method, to
|
||
365 | * show it in the log if there is an error
|
||
366 | * @see <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/ResourceBundle.html">ResourceBundle</a>
|
||
367 | 4516 | cesar | */
|
368 | public static void addResourceFamily(String family, ClassLoader loader, String callerName) { |
||
369 | String currentKey;
|
||
370 | 13649 | cesar | Enumeration keys;
|
371 | 28469 | jmvivo | Locale lang;
|
372 | 13649 | cesar | Properties properties, translations;
|
373 | 5767 | cesar | int totalLocales = preferredLocales.size();
|
374 | |||
375 | if (totalLocales == 0) { |
||
376 | 4728 | cesar | // if it's empty, warn about that
|
377 | 13649 | cesar | logger.warn("There is not preferred languages list. Maybe the Messages class was not initialized");
|
378 | 4780 | cesar | } |
379 | 13649 | cesar | |
380 | 28469 | jmvivo | resourceFamilies.add(family); |
381 | classLoaders.add(loader); |
||
382 | 13649 | cesar | |
383 | 5767 | cesar | for (int numLocale=0; numLocale<totalLocales; numLocale++) { // for each language |
384 | 13649 | cesar | properties = new Properties(); |
385 | 28469 | jmvivo | |
386 | lang = (Locale) preferredLocales.get(numLocale);
|
||
387 | translations = (Properties) localeResources.get(numLocale);
|
||
388 | |||
389 | addResourceFamily(lang, translations, family, loader, callerName); |
||
390 | } |
||
391 | } |
||
392 | |||
393 | private static void addResourceFamily(Locale lang, Properties translations, |
||
394 | String family, ClassLoader loader, String callerName) { |
||
395 | Properties properties = new Properties(); |
||
396 | String resource = family.replace('.', '/') + "_" + lang.toString() |
||
397 | + ".properties";
|
||
398 | InputStream is = loader.getResourceAsStream(resource);
|
||
399 | if (is != null) { |
||
400 | try {
|
||
401 | properties.load(is); |
||
402 | } catch (IOException e) { |
||
403 | } |
||
404 | } else if (lang.equals(baseLocale)) { |
||
405 | // try also "text.properties" for the base language
|
||
406 | is = loader.getResourceAsStream(family.replace('.', '/') |
||
407 | + ".properties");
|
||
408 | |||
409 | |||
410 | 13649 | cesar | if (is != null) { |
411 | try {
|
||
412 | properties.load(is); |
||
413 | } catch (IOException e) { |
||
414 | 12863 | cesar | } |
415 | 13649 | cesar | } |
416 | |||
417 | 28469 | jmvivo | } |
418 | Enumeration keys = properties.keys();
|
||
419 | while (keys.hasMoreElements()) {
|
||
420 | String currentKey = (String) keys.nextElement(); |
||
421 | if (!translations.containsKey(currentKey)) {
|
||
422 | translations |
||
423 | .put(currentKey, properties.getProperty(currentKey)); |
||
424 | 4597 | cesar | } |
425 | 4460 | cesar | } |
426 | 28469 | jmvivo | |
427 | 4460 | cesar | } |
428 | 25650 | vcaballero | |
429 | 4728 | cesar | /**
|
430 | * <p>Adds an additional family of resource files containing some translations.</p>
|
||
431 | 25650 | vcaballero | *
|
432 | 4728 | cesar | * <p>This method is identical to {@link addResourceFamily(String, ClassLoader, String)},
|
433 | * except that it uses the caller's class loader.</p>
|
||
434 | 25650 | vcaballero | *
|
435 | 4728 | cesar | * <p>See {@link addResourceFamily(String, ClassLoader)} for a discussion about the
|
436 | * format of the property files and the way to determine the candidat files
|
||
437 | * to load.</p>
|
||
438 | 25650 | vcaballero | *
|
439 | 4728 | cesar | * @param family The family name (or base name) which is used to search
|
440 | * actual properties files.
|
||
441 | * @param callerName A symbolic name given to the caller of this method, to
|
||
442 | * show it in the log if there is an error. This is only used
|
||
443 | * to show
|
||
444 | * something meaningful in the log, so you can use any string
|
||
445 | * @see <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/ResourceBundle.html">ResourceBundle</a>
|
||
446 | */
|
||
447 | public static void addResourceFamily(String family, String callerName) { |
||
448 | 13649 | cesar | addResourceFamily(family, Messages.class.getClassLoader(), callerName); |
449 | 4728 | cesar | } |
450 | 8010 | cesar | |
451 | 25650 | vcaballero | |
452 | 4460 | cesar | /**
|
453 | 5599 | cesar | * Returns an ArrayList containing the ordered list of prefered Locales
|
454 | * Each element of the ArrayList is a Locale object.
|
||
455 | 25650 | vcaballero | *
|
456 | 5599 | cesar | * @return an ArrayList containing the ordered list of prefered Locales
|
457 | * Each element of the ArrayList is a Locale object.
|
||
458 | 4460 | cesar | */
|
459 | 5599 | cesar | public static ArrayList getPreferredLocales() { |
460 | 5767 | cesar | return preferredLocales;
|
461 | 4460 | cesar | } |
462 | 25650 | vcaballero | |
463 | 4460 | cesar | /**
|
464 | 5767 | cesar | * <p>Sets the ordered list of preferred locales.
|
465 | * Each element of the ArrayList is a Locale object.</p>
|
||
466 | 25650 | vcaballero | *
|
467 | 5767 | cesar | * <p>Note that calling this method does not load any translation, it just
|
468 | * adds the language to the preferred locales list, so this method must
|
||
469 | * be always called before the translations are loaded using
|
||
470 | * the addResourceFamily() methods.</p>
|
||
471 | 25650 | vcaballero | *
|
472 | 8010 | cesar | * <p>It there was any language in the preferred locale list, the language
|
473 | * and its associated translations are deleted.</p>
|
||
474 | 25650 | vcaballero | *
|
475 | *
|
||
476 | 5599 | cesar | * @param preferredLocales an ArrayList containing Locale objects.
|
477 | * The ArrayList represents an ordered list of preferred locales
|
||
478 | 4460 | cesar | */
|
479 | 5767 | cesar | public static void setPreferredLocales(ArrayList preferredLocalesList) { |
480 | 8010 | cesar | // delete all existing locales
|
481 | Iterator oldLocales = preferredLocales.iterator();
|
||
482 | while (oldLocales.hasNext()) {
|
||
483 | removeLocale((Locale) oldLocales.next());
|
||
484 | } |
||
485 | 25650 | vcaballero | |
486 | 8010 | cesar | // add the new locales now
|
487 | 5767 | cesar | for (int numLocale=0; numLocale < preferredLocalesList.size(); numLocale++) { |
488 | addLocale((Locale) preferredLocalesList.get(numLocale));
|
||
489 | } |
||
490 | 4460 | cesar | } |
491 | 4728 | cesar | |
492 | /**
|
||
493 | * Adds a Locale at the end of the ordered list of preferred locales.
|
||
494 | 5767 | cesar | * Note that calling this method does not load any translation, it just
|
495 | * adds the language to the preferred locales list, so this method must
|
||
496 | * be always called before the translations are loaded using
|
||
497 | * the addResourceFamily() methods.
|
||
498 | 25650 | vcaballero | *
|
499 | 4728 | cesar | * @param lang A Locale object specifying the locale to add
|
500 | */
|
||
501 | 4516 | cesar | public static void addLocale(Locale lang) { |
502 | 5767 | cesar | if (!preferredLocales.contains(lang)) { // avoid duplicates |
503 | preferredLocales.add(lang); // add the lang to the ordered list of preferred locales
|
||
504 | 25650 | vcaballero | Properties dict = new Properties(); |
505 | 8010 | cesar | localeResources.add(dict); // add a hashmap which will contain the translation for this language
|
506 | 4516 | cesar | } |
507 | } |
||
508 | 4728 | cesar | |
509 | /**
|
||
510 | 5767 | cesar | * Removes the specified Locale from the list of preferred locales and the
|
511 | * translations associated with this locale.
|
||
512 | 25650 | vcaballero | *
|
513 | 4728 | cesar | * @param lang A Locale object specifying the locale to remove
|
514 | 5599 | cesar | * @return True if the locale was in the preferred locales list, false otherwise
|
515 | 4728 | cesar | */
|
516 | 4587 | cesar | public static boolean removeLocale(Locale lang) { |
517 | 5767 | cesar | int numLocale = preferredLocales.indexOf(lang);
|
518 | if (numLocale!=-1) { // we found the locale in the list |
||
519 | try {
|
||
520 | preferredLocales.remove(numLocale); |
||
521 | localeResources.remove(numLocale); |
||
522 | } |
||
523 | catch (IndexOutOfBoundsException ex) { |
||
524 | logger.warn(_CLASSNAME + "." + "removeLocale: " + ex.getLocalizedMessage()); |
||
525 | } |
||
526 | return true; |
||
527 | 5599 | cesar | } |
528 | 5767 | cesar | return false; |
529 | 4587 | cesar | } |
530 | 4728 | cesar | |
531 | 4460 | cesar | /**
|
532 | 5767 | cesar | * Cleans the translation tables (removes all the translations from memory).
|
533 | 4728 | cesar | */
|
534 | public static void removeResources() { |
||
535 | 5767 | cesar | for (int numLocale=0; numLocale<localeResources.size(); numLocale++) { |
536 | 13649 | cesar | ((Properties)localeResources.get(numLocale)).clear();
|
537 | 5767 | cesar | } |
538 | 4728 | cesar | } |
539 | |||
540 | /**
|
||
541 | 25650 | vcaballero | * The number of translation keys which have been loaded till now
|
542 | 4728 | cesar | * (In other words: the number of available translation strings).
|
543 | 25650 | vcaballero | *
|
544 | 5767 | cesar | * @param lang The language for which we want to know the number of translation keys
|
545 | * return The number of translation keys for the provided language.
|
||
546 | 4728 | cesar | */
|
547 | 5767 | cesar | protected static int size(Locale lang) { |
548 | int numLocale = preferredLocales.indexOf(lang);
|
||
549 | if (numLocale!=-1) { |
||
550 | 13649 | cesar | return ((Properties)localeResources.get(numLocale)).size(); |
551 | 5767 | cesar | }; |
552 | return 0; |
||
553 | 4728 | cesar | } |
554 | 25650 | vcaballero | |
555 | 13649 | cesar | protected static Set keySet(Locale lang) { |
556 | int numLocale = preferredLocales.indexOf(lang);
|
||
557 | if (numLocale!=-1) { |
||
558 | return ((Properties)localeResources.get(numLocale)).keySet(); |
||
559 | 28469 | jmvivo | } else {
|
560 | return null; |
||
561 | 13649 | cesar | } |
562 | } |
||
563 | 25650 | vcaballero | |
564 | 4728 | cesar | /**
|
565 | 7258 | cesar | * Checks if some locale has been added to the preferred locales
|
566 | * list, which is necessary before loading any translation because
|
||
567 | * only the translations for the preferred locales are loaded.
|
||
568 | 25650 | vcaballero | *
|
569 | 7258 | cesar | * @return
|
570 | */
|
||
571 | public static boolean hasLocales() { |
||
572 | return preferredLocales.size()>0; |
||
573 | } |
||
574 | 25650 | vcaballero | |
575 | 7258 | cesar | /**
|
576 | 13649 | cesar | * Gets the base language, the language considered the origin of
|
577 | 25650 | vcaballero | * translations, which will be (possibly) stored in a property
|
578 | 13649 | cesar | * file without language suffix
|
579 | * (ie: text.properties instead of text_es.properties).
|
||
580 | 25650 | vcaballero | */
|
581 | 13649 | cesar | public static String getBaseLanguage() { |
582 | return baseLanguage;
|
||
583 | } |
||
584 | 25650 | vcaballero | |
585 | 13649 | cesar | /**
|
586 | * Sets the base language, the language considered the origin of
|
||
587 | * translations, which will be (possibly)
|
||
588 | * stored in a property file without language suffix
|
||
589 | * (ie: text.properties instead of text_es.properties).
|
||
590 | 25650 | vcaballero | *
|
591 | 13649 | cesar | * @param lang The base language to be set
|
592 | */
|
||
593 | public static void setBaseLanguage(String lang) { |
||
594 | baseLanguage = lang; |
||
595 | 28469 | jmvivo | baseLocale = new Locale(baseLanguage); |
596 | 13649 | cesar | } |
597 | 25650 | vcaballero | |
598 | 13649 | cesar | /*
|
599 | 4460 | cesar | * Searches the subdirectories of the provided directory, finding
|
600 | * all the translation files, and constructing a list of available translations.
|
||
601 | * It reports different country codes or variants, if available.
|
||
602 | * For example, if there is an en_US translation and an en_GB translation, both
|
||
603 | * locales will be present in the Vector.
|
||
604 | 25650 | vcaballero | *
|
605 | 4460 | cesar | * @return
|
606 | */
|
||
607 | 25650 | vcaballero | |
608 | 4460 | cesar | /**
|
609 | 25650 | vcaballero | *
|
610 | 4460 | cesar | * @return A Vector containing the available locales. Each element is a Locale object
|
611 | */
|
||
612 | 4728 | cesar | /*public static Vector getAvailableLocales() {
|
613 | 4460 | cesar | return _availableLocales;
|
614 | 4728 | cesar | }*/
|
615 | 25650 | vcaballero | |
616 | 4460 | cesar | /**
|
617 | 25650 | vcaballero | *
|
618 | 4460 | cesar | * @return A Vector containing the available languages. Each element is an String object
|
619 | */
|
||
620 | 4728 | cesar | /*public static Vector getAvailableLanguages() {
|
621 | 4460 | cesar | Vector availableLanguages = new Vector();
|
622 | Locale lang;
|
||
623 | Enumeration locales = _availableLocales.elements();
|
||
624 | while (locales.hasMoreElements()) {
|
||
625 | lang = (Locale) locales.nextElement();
|
||
626 | availableLanguages.add(lang.getLanguage());
|
||
627 | }
|
||
628 | return availableLanguages;
|
||
629 | 4728 | cesar | }*/
|
630 | 28469 | jmvivo | |
631 | public static Properties getAllTexts(Locale lang) { |
||
632 | Properties texts = new Properties(); |
||
633 | getAllTexts(lang, null, texts);
|
||
634 | for (Iterator iterator = classLoaders.iterator(); iterator.hasNext();) { |
||
635 | getAllTexts(lang, (ClassLoader) iterator.next(), texts);
|
||
636 | } |
||
637 | return texts;
|
||
638 | } |
||
639 | |||
640 | private static void getAllTexts(Locale lang, ClassLoader classLoader, |
||
641 | Properties texts) {
|
||
642 | ClassLoader loader = classLoader == null ? Messages.class |
||
643 | .getClassLoader() : classLoader; |
||
644 | |||
645 | for (Iterator iterator = resourceFamilies.iterator(); iterator |
||
646 | .hasNext();) { |
||
647 | String family = (String) iterator.next(); |
||
648 | addResourceFamily(lang, texts, family, loader, |
||
649 | "Messages.getAllTexts");
|
||
650 | } |
||
651 | } |
||
652 | |||
653 | 5767 | cesar | } |