Merge lp:~slub.team/goobi-production/bug-1038910 into lp:goobi-production/1.8

Proposed by Henning Gerhardt
Status: Merged
Merged at revision: 98
Proposed branch: lp:~slub.team/goobi-production/bug-1038910
Merge into: lp:goobi-production/1.8
Diff against target: 124 lines (+57/-25)
1 file modified
src/de/sub/goobi/helper/Messages.java (+57/-25)
To merge this branch: bzr merge lp:~slub.team/goobi-production/bug-1038910
Reviewer Review Type Date Requested Status
Ralf Claussnitzer (community) Approve
Review via email: mp+120380@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Ralf Claussnitzer (ralf-claussnitzer-deactivatedaccount) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/de/sub/goobi/helper/Messages.java'
2--- src/de/sub/goobi/helper/Messages.java 2012-07-04 14:55:34 +0000
3+++ src/de/sub/goobi/helper/Messages.java 2012-08-20 13:03:35 +0000
4@@ -22,20 +22,18 @@
5
6 package de.sub.goobi.helper;
7
8+import de.sub.goobi.config.ConfigMain;
9+import org.apache.commons.collections.iterators.ArrayIterator;
10+import org.apache.log4j.Logger;
11+
12+import javax.faces.context.FacesContext;
13 import java.io.File;
14 import java.net.URL;
15 import java.net.URLClassLoader;
16-import java.util.HashMap;
17-import java.util.Iterator;
18-import java.util.Locale;
19-import java.util.Map;
20-import java.util.ResourceBundle;
21-
22-import javax.faces.context.FacesContext;
23-
24-import org.apache.log4j.Logger;
25-
26-import de.sub.goobi.config.ConfigMain;
27+import java.util.*;
28+
29+import static java.util.Locale.getAvailableLocales;
30+import static java.util.ResourceBundle.getBundle;
31
32 public class Messages {
33 private static final Logger logger = Logger.getLogger(Messages.class);
34@@ -44,15 +42,38 @@
35 protected static Map<Locale, ResourceBundle> localMessages = new HashMap<Locale, ResourceBundle>();
36
37 static{
38- @SuppressWarnings("unchecked")
39- Iterator<Locale> polyglot = FacesContext.getCurrentInstance().getApplication().getSupportedLocales();
40+ Iterator polyglot = getSupportedLocalesIterator();
41 while (polyglot.hasNext()) {
42- Locale language = polyglot.next();
43- commonMessages.put(language, ResourceBundle.getBundle("messages", language));
44- ResourceBundle local = loadLocalMessageBundleIfAvailable(language);
45- if (local != null)
46- localMessages.put(language, local);
47- }
48+ Locale language = (Locale) polyglot.next();
49+
50+ ResourceBundle commonMessageBundle = localCommonMessageBundleIfAvailable(language);
51+ if (commonMessageBundle != null) {
52+ commonMessages.put(language, commonMessageBundle);
53+ }
54+
55+ ResourceBundle localMessageBundle = loadLocalMessageBundleIfAvailable(language);
56+ if (localMessageBundle != null) {
57+ localMessages.put(language, localMessageBundle);
58+ }
59+ }
60+ }
61+
62+ private static ResourceBundle localCommonMessageBundleIfAvailable(Locale language) {
63+ try {
64+ return getBundle("messages", language);
65+ } catch (NullPointerException npe) {
66+ logger.error("Attempt to load message bundle, but no locale information given.");
67+ } catch (MissingResourceException mre) {
68+ logger.error("Cannot load common message bundle for language " + language.toString());
69+ }
70+ return null;
71+ }
72+
73+ private static Iterator getSupportedLocalesIterator() {
74+ if (FacesContext.getCurrentInstance() != null && FacesContext.getCurrentInstance().getApplication() != null) {
75+ return FacesContext.getCurrentInstance().getApplication().getSupportedLocales();
76+ }
77+ return new ArrayIterator(getAvailableLocales());
78 }
79
80 /**
81@@ -75,7 +96,7 @@
82 try {
83 URL pathURL = path.toURI().toURL();
84 URLClassLoader urlLoader = new URLClassLoader(new URL[] { pathURL });
85- return ResourceBundle.getBundle("messages", variant, urlLoader);
86+ return getBundle("messages", variant, urlLoader);
87 } catch (java.net.MalformedURLException e) {
88 logger.error("Error reading local message commonMessages", e);
89 }
90@@ -84,6 +105,18 @@
91 return null;
92 }
93
94+ private static Locale getCurrentUsedLanguage() {
95+ Locale result;
96+
97+ if (FacesContext.getCurrentInstance() != null && FacesContext.getCurrentInstance().getViewRoot() != null) {
98+ result = FacesContext.getCurrentInstance().getViewRoot().getLocale();
99+ } else {
100+ result = Locale.getDefault();
101+ }
102+
103+ return result;
104+ }
105+
106 /**
107 * The function getString() returns the translated key in the language
108 * currently configured in the front end.
109@@ -94,11 +127,10 @@
110 * @return The verbalisation for the given key in the language chosen
111 */
112 public static String getString(String key) {
113- Locale desiredLanguage = null;
114- try {
115- desiredLanguage = FacesContext.getCurrentInstance().getViewRoot().getLocale();
116- } catch (NullPointerException skip) {
117- }
118+ Locale desiredLanguage;
119+
120+ desiredLanguage = getCurrentUsedLanguage();
121+
122 if (desiredLanguage != null)
123 return getString(desiredLanguage, key);
124 else

Subscribers

People subscribed via source and target branches

to all changes: