comparison flys-artifacts/src/main/java/org/dive4elements/river/artifacts/resources/Resources.java @ 5831:bd047b71ab37

Repaired internal references
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 12:06:39 +0200
parents flys-artifacts/src/main/java/de/intevation/flys/artifacts/resources/Resources.java@04309ca24614
children
comparison
equal deleted inserted replaced
5830:160f53ee0870 5831:bd047b71ab37
1 package org.dive4elements.river.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 org.dive4elements.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 ensureInstance();
89
90 Locale[] locales = INSTANCE.getLocales();
91 Locale locale = meta.getPreferredLocale(locales);
92
93 return getMsg(locale, key, def);
94 }
95
96 public static String getMsg(
97 CallMeta meta,
98 String key,
99 Object[] args
100 ) {
101 return getMsg(meta, key, key, args);
102 }
103
104 /**
105 * Returns a translated message based on a template specified by <i>key</i>
106 * that has necessary values to be filled in.
107 *
108 * @param meta The CallMeta object.
109 * @param key The key of the template in the resource bundle.
110 * @param def the default value if no template was found with <i>key</i>.
111 * @param args The arguments that are necessary for the template.
112 *
113 * @return a translated string.
114 */
115 public static String getMsg(
116 CallMeta meta,
117 String key,
118 String def,
119 Object[] args)
120 {
121 String template = getMsg(meta, key, (String)null);
122
123 if (template == null) {
124 return def;
125 }
126
127 return format(meta, template, args);
128 }
129
130 public static String format(
131 CallMeta meta,
132 String key,
133 String def,
134 Object ... args
135 ) {
136 String template = getMsg(meta, key, (String)null);
137
138 if (template == null) {
139 template = def;
140 }
141
142 return format(meta, template, args);
143 }
144
145 /**
146 * Formats the given template using the arguments with respect of the
147 * appropriate locale given by the CallMeta instance.
148 */
149 public static String format(CallMeta meta, String templ, Object ... args) {
150 Locale locale = getLocale(meta);
151 MessageFormat mf = new MessageFormat(templ, locale);
152
153 return mf.format(args, new StringBuffer(), null).toString();
154 }
155
156 /**
157 * This method returns the translated value for <i>key</i> or <i>def</i> if
158 * <i>key</i> is not existing in the resource bundle.
159 *
160 * @param locale The locale.
161 * @param key The key that should be translated.
162 * @param def A default value that is returned, if <i>key</i> was not found.
163 *
164 * @return the translated message.
165 */
166 public static String getMsg(Locale locale, String key, String def) {
167 ResourceBundle bundle = ResourceBundle.getBundle("messages", locale);
168
169 try {
170 return bundle.getString(key);
171 }
172 catch (MissingResourceException mre) {
173 logger.warn("No message found for key: " + key);
174
175 return def;
176 }
177 }
178 }
179 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org