annotate flys-artifacts/src/main/java/de/intevation/flys/artifacts/resources/Resources.java @ 340:b36fd8f21e6a

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

http://dive4elements.wald.intevation.org