Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/resources/Resources.java @ 3814:8083f6384023
merged flys-artifacts/pre2.6-2012-01-04
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:56 +0200 |
parents | e0fec407a280 |
children | 2898b1ff6013 |
comparison
equal
deleted
inserted
replaced
1491:2a00f4849738 | 3814:8083f6384023 |
---|---|
1 package de.intevation.flys.artifacts.resources; | |
2 | |
3 import java.text.MessageFormat; | |
4 import java.util.Locale; | |
5 import java.util.MissingResourceException; | |
6 import java.util.ResourceBundle; | |
7 | |
8 import org.apache.log4j.Logger; | |
9 | |
10 import de.intevation.artifacts.CallMeta; | |
11 | |
12 /** | |
13 * This class provides methods for i18n. | |
14 * | |
15 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
16 */ | |
17 public class Resources { | |
18 | |
19 /** The logger that is used in this class.*/ | |
20 private static Logger logger = Logger.getLogger(Resources.class); | |
21 | |
22 /** The singleton instance.*/ | |
23 private static Resources INSTANCE; | |
24 | |
25 /** The locales supported by this server.*/ | |
26 protected Locale[] locales; | |
27 | |
28 /** | |
29 * No instance of this class is necessary. | |
30 */ | |
31 private Resources() { | |
32 } | |
33 | |
34 | |
35 /** | |
36 * Returns the locales supported by this server. | |
37 * | |
38 * @return the supported locales. | |
39 */ | |
40 public Locale[] getLocales() { | |
41 if (locales == null) { | |
42 readLocales(); | |
43 } | |
44 | |
45 return locales; | |
46 } | |
47 | |
48 | |
49 /** | |
50 * Read the locales configured for this server. | |
51 */ | |
52 protected void readLocales() { | |
53 // TODO IMPLEMENT ME | |
54 | |
55 locales = new Locale[2]; | |
56 locales[0] = Locale.GERMANY; | |
57 locales[1] = Locale.ENGLISH; | |
58 } | |
59 | |
60 | |
61 public static Locale getLocale(CallMeta meta) { | |
62 if (INSTANCE == null) { | |
63 INSTANCE = new Resources(); | |
64 } | |
65 | |
66 Locale[] locales = INSTANCE.getLocales(); | |
67 return meta.getPreferredLocale(locales); | |
68 } | |
69 | |
70 | |
71 /** | |
72 * This method returns the translated value for <i>key</i> or <i>def</i> if | |
73 * <i>key</i> is not existing in the resource bundle. | |
74 * | |
75 * @param meta The CallMeta object of the request that contains the | |
76 * preferred locale. | |
77 * @param key The key that should be translated. | |
78 * @param def A default value that is returned, if <i>key</i> was not found. | |
79 * | |
80 * @return the translated message. | |
81 */ | |
82 public static String getMsg(CallMeta meta, String key, String def) { | |
83 if (INSTANCE == null) { | |
84 INSTANCE = new Resources(); | |
85 } | |
86 | |
87 Locale[] locales = INSTANCE.getLocales(); | |
88 Locale locale = meta.getPreferredLocale(locales); | |
89 | |
90 return getMsg(locale, key, def); | |
91 } | |
92 | |
93 | |
94 /** | |
95 * Returns a translated message based on a template specified by <i>key</i> | |
96 * that has necessary values to be filled in. | |
97 * | |
98 * @param meta The CallMeta object. | |
99 * @param key The key of the template in the resource bundle. | |
100 * @param def the default value if no template was found with <i>key</i>. | |
101 * @param args The arguments that are necessary for the template. | |
102 * | |
103 * @return a translated string. | |
104 */ | |
105 public static String getMsg( | |
106 CallMeta meta, | |
107 String key, | |
108 String def, | |
109 Object[] args) | |
110 { | |
111 String template = getMsg(meta, key, null); | |
112 | |
113 if (template == null) { | |
114 return def; | |
115 } | |
116 | |
117 return MessageFormat.format(template, args); | |
118 } | |
119 | |
120 | |
121 /** | |
122 * This method returns the translated value for <i>key</i> or <i>def</i> if | |
123 * <i>key</i> is not existing in the resource bundle. | |
124 * | |
125 * @param locale The locale. | |
126 * @param key The key that should be translated. | |
127 * @param def A default value that is returned, if <i>key</i> was not found. | |
128 * | |
129 * @return the translated message. | |
130 */ | |
131 public static String getMsg(Locale locale, String key, String def) { | |
132 ResourceBundle bundle = ResourceBundle.getBundle("messages", locale); | |
133 | |
134 try { | |
135 return bundle.getString(key); | |
136 } | |
137 catch (MissingResourceException mre) { | |
138 logger.warn("No message found for key: " + key); | |
139 | |
140 return def; | |
141 } | |
142 } | |
143 } | |
144 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |