Mercurial > dive4elements > river
annotate flys-artifacts/src/main/java/de/intevation/flys/artifacts/resources/Resources.java @ 4196:e10d60d4f35b
Don't always redirect if user isn't authenticated
Only redirect a user if he is accessing the redirect url (FLYS.html by default).
In other cases only send a 403 Forbidden to indicate that a user isn't allowed
to access the url.
Should improve flys/issue856 (Timeout in der Authentifizierung führt zu Fehlern)
author | Björn Ricks <bjoern.ricks@intevation.de> |
---|---|
date | Mon, 22 Oct 2012 10:35:49 +0200 |
parents | 04309ca24614 |
children |
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 |
413
8a4c219fd0ee
Added a method to Resources that returns translated compound messages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
125
diff
changeset
|
3 import java.text.MessageFormat; |
125
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
4 import java.util.Locale; |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
5 import java.util.MissingResourceException; |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
6 import java.util.ResourceBundle; |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
7 |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
8 import org.apache.log4j.Logger; |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
9 |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
10 import de.intevation.artifacts.CallMeta; |
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 /** |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
13 * This class provides methods for i18n. |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
14 * |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
15 * @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
|
16 */ |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
17 public class Resources { |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
18 |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
19 /** 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
|
20 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
|
21 |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
22 /** The singleton instance.*/ |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
23 private static Resources INSTANCE; |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
24 |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
25 /** The locales supported by this server.*/ |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
26 protected Locale[] locales; |
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 /** |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
29 * 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
|
30 */ |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
31 private Resources() { |
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 /** |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
36 * 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
|
37 * |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
38 * @return the supported locales. |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
39 */ |
2290
6f4a1f513e89
Calculation error report: i18n the km of the error report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2166
diff
changeset
|
40 public synchronized Locale [] getLocales() { |
125
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
41 if (locales == null) { |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
42 readLocales(); |
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 |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
45 return locales; |
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 /** |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
50 * 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
|
51 */ |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
52 protected void readLocales() { |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
53 // TODO IMPLEMENT ME |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
54 |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
55 locales = new Locale[2]; |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
56 locales[0] = Locale.GERMANY; |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
57 locales[1] = Locale.ENGLISH; |
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 |
2290
6f4a1f513e89
Calculation error report: i18n the km of the error report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2166
diff
changeset
|
61 private static synchronized void ensureInstance() { |
418
e0fec407a280
ISSUE-53 Formatted number values of CSV exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
413
diff
changeset
|
62 if (INSTANCE == null) { |
e0fec407a280
ISSUE-53 Formatted number values of CSV exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
413
diff
changeset
|
63 INSTANCE = new Resources(); |
e0fec407a280
ISSUE-53 Formatted number values of CSV exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
413
diff
changeset
|
64 } |
2290
6f4a1f513e89
Calculation error report: i18n the km of the error report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2166
diff
changeset
|
65 } |
6f4a1f513e89
Calculation error report: i18n the km of the error report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2166
diff
changeset
|
66 |
6f4a1f513e89
Calculation error report: i18n the km of the error report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2166
diff
changeset
|
67 |
6f4a1f513e89
Calculation error report: i18n the km of the error report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2166
diff
changeset
|
68 public static Locale getLocale(CallMeta meta) { |
6f4a1f513e89
Calculation error report: i18n the km of the error report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2166
diff
changeset
|
69 ensureInstance(); |
418
e0fec407a280
ISSUE-53 Formatted number values of CSV exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
413
diff
changeset
|
70 |
e0fec407a280
ISSUE-53 Formatted number values of CSV exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
413
diff
changeset
|
71 Locale[] locales = INSTANCE.getLocales(); |
e0fec407a280
ISSUE-53 Formatted number values of CSV exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
413
diff
changeset
|
72 return meta.getPreferredLocale(locales); |
e0fec407a280
ISSUE-53 Formatted number values of CSV exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
413
diff
changeset
|
73 } |
e0fec407a280
ISSUE-53 Formatted number values of CSV exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
413
diff
changeset
|
74 |
e0fec407a280
ISSUE-53 Formatted number values of CSV exports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
413
diff
changeset
|
75 |
125
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
76 /** |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
77 * 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
|
78 * <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
|
79 * |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
80 * @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
|
81 * preferred locale. |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
82 * @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
|
83 * @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
|
84 * |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
85 * @return the translated message. |
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 public static String getMsg(CallMeta meta, String key, String def) { |
2290
6f4a1f513e89
Calculation error report: i18n the km of the error report.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2166
diff
changeset
|
88 ensureInstance(); |
125
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
89 |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
90 Locale[] locales = INSTANCE.getLocales(); |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
91 Locale locale = meta.getPreferredLocale(locales); |
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 return getMsg(locale, key, def); |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
94 } |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
95 |
2166
2898b1ff6013
I18N for WINFO calculation result messages.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
418
diff
changeset
|
96 public static String getMsg( |
3771 | 97 CallMeta meta, |
98 String key, | |
99 Object[] args | |
100 ) { | |
2166
2898b1ff6013
I18N for WINFO calculation result messages.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
418
diff
changeset
|
101 return getMsg(meta, key, key, args); |
2898b1ff6013
I18N for WINFO calculation result messages.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
418
diff
changeset
|
102 } |
125
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 /** |
413
8a4c219fd0ee
Added a method to Resources that returns translated compound messages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
125
diff
changeset
|
105 * Returns a translated message based on a template specified by <i>key</i> |
8a4c219fd0ee
Added a method to Resources that returns translated compound messages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
125
diff
changeset
|
106 * that has necessary values to be filled in. |
8a4c219fd0ee
Added a method to Resources that returns translated compound messages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
125
diff
changeset
|
107 * |
8a4c219fd0ee
Added a method to Resources that returns translated compound messages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
125
diff
changeset
|
108 * @param meta The CallMeta object. |
8a4c219fd0ee
Added a method to Resources that returns translated compound messages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
125
diff
changeset
|
109 * @param key The key of the template in the resource bundle. |
8a4c219fd0ee
Added a method to Resources that returns translated compound messages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
125
diff
changeset
|
110 * @param def the default value if no template was found with <i>key</i>. |
8a4c219fd0ee
Added a method to Resources that returns translated compound messages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
125
diff
changeset
|
111 * @param args The arguments that are necessary for the template. |
8a4c219fd0ee
Added a method to Resources that returns translated compound messages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
125
diff
changeset
|
112 * |
8a4c219fd0ee
Added a method to Resources that returns translated compound messages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
125
diff
changeset
|
113 * @return a translated string. |
8a4c219fd0ee
Added a method to Resources that returns translated compound messages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
125
diff
changeset
|
114 */ |
8a4c219fd0ee
Added a method to Resources that returns translated compound messages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
125
diff
changeset
|
115 public static String getMsg( |
3771 | 116 CallMeta meta, |
117 String key, | |
118 String def, | |
119 Object[] args) | |
413
8a4c219fd0ee
Added a method to Resources that returns translated compound messages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
125
diff
changeset
|
120 { |
2166
2898b1ff6013
I18N for WINFO calculation result messages.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
418
diff
changeset
|
121 String template = getMsg(meta, key, (String)null); |
413
8a4c219fd0ee
Added a method to Resources that returns translated compound messages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
125
diff
changeset
|
122 |
8a4c219fd0ee
Added a method to Resources that returns translated compound messages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
125
diff
changeset
|
123 if (template == null) { |
8a4c219fd0ee
Added a method to Resources that returns translated compound messages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
125
diff
changeset
|
124 return def; |
8a4c219fd0ee
Added a method to Resources that returns translated compound messages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
125
diff
changeset
|
125 } |
8a4c219fd0ee
Added a method to Resources that returns translated compound messages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
125
diff
changeset
|
126 |
3771 | 127 return format(meta, template, args); |
413
8a4c219fd0ee
Added a method to Resources that returns translated compound messages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
125
diff
changeset
|
128 } |
8a4c219fd0ee
Added a method to Resources that returns translated compound messages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
125
diff
changeset
|
129 |
3170
79e7bba0f593
FixA: I18N for chart km service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2290
diff
changeset
|
130 public static String format( |
3771 | 131 CallMeta meta, |
132 String key, | |
133 String def, | |
134 Object ... args | |
135 ) { | |
3170
79e7bba0f593
FixA: I18N for chart km service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2290
diff
changeset
|
136 String template = getMsg(meta, key, (String)null); |
79e7bba0f593
FixA: I18N for chart km service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2290
diff
changeset
|
137 |
79e7bba0f593
FixA: I18N for chart km service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2290
diff
changeset
|
138 if (template == null) { |
3398
7f9fe694f8d1
FixA: Added time range column to Delta W(t) CSV export.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3170
diff
changeset
|
139 template = def; |
3170
79e7bba0f593
FixA: I18N for chart km service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2290
diff
changeset
|
140 } |
79e7bba0f593
FixA: I18N for chart km service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2290
diff
changeset
|
141 |
3771 | 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(); | |
3170
79e7bba0f593
FixA: I18N for chart km service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2290
diff
changeset
|
154 } |
413
8a4c219fd0ee
Added a method to Resources that returns translated compound messages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
125
diff
changeset
|
155 |
8a4c219fd0ee
Added a method to Resources that returns translated compound messages.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
125
diff
changeset
|
156 /** |
125
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
157 * 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
|
158 * <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
|
159 * |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
160 * @param locale The locale. |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
161 * @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
|
162 * @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
|
163 * |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
164 * @return the translated message. |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
165 */ |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
166 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
|
167 ResourceBundle bundle = ResourceBundle.getBundle("messages", locale); |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
168 |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
169 try { |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
170 return bundle.getString(key); |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
171 } |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
172 catch (MissingResourceException mre) { |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
173 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
|
174 |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
175 return def; |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
176 } |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
177 } |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
178 } |
25593857b8f8
Implemented a singleton instance that provides i18n strings.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff
changeset
|
179 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |