comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/resources/Resources.java @ 125:25593857b8f8

Implemented a singleton instance that provides i18n strings. flys-artifacts/trunk@1461 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 14 Mar 2011 14:16:37 +0000
parents
children 8a4c219fd0ee
comparison
equal deleted inserted replaced
124:b7a9557957d1 125:25593857b8f8
1 package de.intevation.flys.artifacts.resources;
2
3 import java.util.Locale;
4 import java.util.MissingResourceException;
5 import java.util.ResourceBundle;
6
7 import org.apache.log4j.Logger;
8
9 import de.intevation.artifacts.CallMeta;
10
11 /**
12 * This class provides methods for i18n.
13 *
14 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
15 */
16 public class Resources {
17
18 /** The logger that is used in this class.*/
19 private static Logger logger = Logger.getLogger(Resources.class);
20
21 /** The singleton instance.*/
22 private static Resources INSTANCE;
23
24 /** The locales supported by this server.*/
25 protected Locale[] locales;
26
27 /**
28 * No instance of this class is necessary.
29 */
30 private Resources() {
31 }
32
33
34 /**
35 * Returns the locales supported by this server.
36 *
37 * @return the supported locales.
38 */
39 public Locale[] getLocales() {
40 if (locales == null) {
41 readLocales();
42 }
43
44 return locales;
45 }
46
47
48 /**
49 * Read the locales configured for this server.
50 */
51 protected void readLocales() {
52 // TODO IMPLEMENT ME
53
54 locales = new Locale[2];
55 locales[0] = Locale.GERMANY;
56 locales[1] = Locale.ENGLISH;
57 }
58
59
60 /**
61 * This method returns the translated value for <i>key</i> or <i>def</i> if
62 * <i>key</i> is not existing in the resource bundle.
63 *
64 * @param meta The CallMeta object of the request that contains the
65 * preferred locale.
66 * @param key The key that should be translated.
67 * @param def A default value that is returned, if <i>key</i> was not found.
68 *
69 * @return the translated message.
70 */
71 public static String getMsg(CallMeta meta, String key, String def) {
72 if (INSTANCE == null) {
73 INSTANCE = new Resources();
74 }
75
76 Locale[] locales = INSTANCE.getLocales();
77 Locale locale = meta.getPreferredLocale(locales);
78
79 return getMsg(locale, key, def);
80 }
81
82
83 /**
84 * This method returns the translated value for <i>key</i> or <i>def</i> if
85 * <i>key</i> is not existing in the resource bundle.
86 *
87 * @param locale The locale.
88 * @param key The key that should be translated.
89 * @param def A default value that is returned, if <i>key</i> was not found.
90 *
91 * @return the translated message.
92 */
93 public static String getMsg(Locale locale, String key, String def) {
94 ResourceBundle bundle = ResourceBundle.getBundle("messages", locale);
95
96 try {
97 return bundle.getString(key);
98 }
99 catch (MissingResourceException mre) {
100 logger.warn("No message found for key: " + key);
101
102 return def;
103 }
104 }
105 }
106 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org