comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/resources/Resources.java @ 3468:f37e7e8907cb

merged flys-artifacts/2.8.1
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:39 +0200
parents 7f9fe694f8d1
children 04309ca24614
comparison
equal deleted inserted replaced
3387:5ffad8bde8ad 3468:f37e7e8907cb
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 synchronized 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 private static synchronized void ensureInstance() {
62 if (INSTANCE == null) {
63 INSTANCE = new Resources();
64 }
65 }
66
67
68 public static Locale getLocale(CallMeta meta) {
69 ensureInstance();
70
71 Locale[] locales = INSTANCE.getLocales();
72 return meta.getPreferredLocale(locales);
73 }
74
75
76 /**
77 * This method returns the translated value for <i>key</i> or <i>def</i> if
78 * <i>key</i> is not existing in the resource bundle.
79 *
80 * @param meta The CallMeta object of the request that contains the
81 * preferred locale.
82 * @param key The key that should be translated.
83 * @param def A default value that is returned, if <i>key</i> was not found.
84 *
85 * @return the translated message.
86 */
87 public static String getMsg(CallMeta meta, String key, String def) {
88
89 ensureInstance();
90
91 Locale[] locales = INSTANCE.getLocales();
92 Locale locale = meta.getPreferredLocale(locales);
93
94 return getMsg(locale, key, def);
95 }
96
97 public static String getMsg(
98 CallMeta meta,
99 String key,
100 Object[] args
101 ) {
102 return getMsg(meta, key, key, args);
103 }
104
105 /**
106 * Returns a translated message based on a template specified by <i>key</i>
107 * that has necessary values to be filled in.
108 *
109 * @param meta The CallMeta object.
110 * @param key The key of the template in the resource bundle.
111 * @param def the default value if no template was found with <i>key</i>.
112 * @param args The arguments that are necessary for the template.
113 *
114 * @return a translated string.
115 */
116 public static String getMsg(
117 CallMeta meta,
118 String key,
119 String def,
120 Object[] args)
121 {
122 String template = getMsg(meta, key, (String)null);
123
124 if (template == null) {
125 return def;
126 }
127
128 return MessageFormat.format(template, args);
129 }
130
131 public static String format(
132 CallMeta meta,
133 String key,
134 String def,
135 Object ... args
136 ) {
137 String template = getMsg(meta, key, (String)null);
138
139 if (template == null) {
140 template = def;
141 }
142
143 return MessageFormat.format(template, args);
144 }
145
146 /**
147 * This method returns the translated value for <i>key</i> or <i>def</i> if
148 * <i>key</i> is not existing in the resource bundle.
149 *
150 * @param locale The locale.
151 * @param key The key that should be translated.
152 * @param def A default value that is returned, if <i>key</i> was not found.
153 *
154 * @return the translated message.
155 */
156 public static String getMsg(Locale locale, String key, String def) {
157 ResourceBundle bundle = ResourceBundle.getBundle("messages", locale);
158
159 try {
160 return bundle.getString(key);
161 }
162 catch (MissingResourceException mre) {
163 logger.warn("No message found for key: " + key);
164
165 return def;
166 }
167 }
168 }
169 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org