-
Notifications
You must be signed in to change notification settings - Fork 86
/
LocaleSelectionSetup.java
155 lines (124 loc) · 4.24 KB
/
LocaleSelectionSetup.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* $This file is distributed under the terms of the license in LICENSE$ */
package edu.cornell.mannlib.vitro.webapp.i18n.selection;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.commons.lang3.LocaleUtils;
import org.apache.commons.lang3.StringUtils;
import edu.cornell.mannlib.vitro.webapp.config.ConfigurationProperties;
import edu.cornell.mannlib.vitro.webapp.startup.StartupStatus;
/**
* Check the ConfigurationProperties for a forced locale, or for a
* comma-separate list of selectable locales.
*
* Create the appropriate Locale objects and store them in the ServletContext.
*/
public class LocaleSelectionSetup implements ServletContextListener {
/**
* If this is set, the locale is forced. No selection will be offered to the
* user, and browser locales will be ignored.
*/
public static final String PROPERTY_FORCE_LOCALE = "languages.forceLocale";
/**
* This is the list of locales that the user may select. There should be a
* national flag or symbol available for each supported locale.
*/
public static final String PROPERTY_SELECTABLE_LOCALES = "languages.selectableLocales";
private ServletContext ctx;
private StartupStatus ss;
private ConfigurationProperties props;
private String forceString;
private String selectableString;
@Override
public void contextInitialized(ServletContextEvent sce) {
ctx = sce.getServletContext();
ss = StartupStatus.getBean(ctx);
props = ConfigurationProperties.getBean(sce);
readProperties();
if (isForcing() && hasSelectables()) {
warnAboutOverride();
}
if (isForcing()) {
forceLocale();
} else if (hasSelectables()) {
setUpSelections();
} else {
reportNoLocales();
}
}
private void readProperties() {
forceString = props.getProperty(PROPERTY_FORCE_LOCALE, "");
selectableString = props.getProperty(PROPERTY_SELECTABLE_LOCALES, "");
}
private boolean isForcing() {
return StringUtils.isNotBlank(forceString);
}
private boolean hasSelectables() {
return StringUtils.isNotBlank(selectableString);
}
private void warnAboutOverride() {
ss.warning(this, "'" + PROPERTY_FORCE_LOCALE + "' will override '"
+ PROPERTY_SELECTABLE_LOCALES + "'.");
}
private void forceLocale() {
try {
Locale forceLocale = buildLocale(forceString);
SelectedLocale.setForcedLocale(ctx, forceLocale);
ssInfo("Setting forced locale to '" + forceLocale + "'.");
} catch (IllegalArgumentException e) {
ssWarning("Problem in '" + PROPERTY_FORCE_LOCALE + "': "
+ e.getMessage());
}
}
private void setUpSelections() {
List<Locale> locales = new ArrayList<Locale>();
for (String string : splitSelectables()) {
try {
locales.add(buildLocale(string));
} catch (IllegalArgumentException e) {
ssWarning("Problem in '" + PROPERTY_SELECTABLE_LOCALES + "': "
+ e.getMessage());
}
}
SelectedLocale.setSelectableLocales(ctx, locales);
ssInfo("Setting selectable locales to '" + locales + "'.");
}
private String[] splitSelectables() {
return selectableString.split("\\s*,\\s*");
}
private void reportNoLocales() {
ssInfo("There is no Locale information.");
}
private void ssInfo(String message) {
ss.info(this, message + showPropertyValues());
}
private void ssWarning(String message) {
ss.warning(this, message + showPropertyValues());
}
private String showPropertyValues() {
return " In runtime.properties, '" + PROPERTY_FORCE_LOCALE
+ "' is set to '" + forceString + "', '"
+ PROPERTY_SELECTABLE_LOCALES + "' is set to '"
+ selectableString + "'";
}
private Locale buildLocale(String localeString)
throws IllegalArgumentException {
// Replicate exception from lang2 with empty strings
if ("".equals(localeString)) {
throw new IllegalArgumentException("Invalid locale format");
}
Locale locale = LocaleUtils.toLocale(localeString);
if (!"es_GO".equals(localeString) && // No complaint about bogus locale
!LocaleUtils.isAvailableLocale(locale)) {
ssWarning("'" + locale + "' is not a recognized locale.");
}
return locale;
}
@Override
public void contextDestroyed(ServletContextEvent arg0) {
// Nothing to do at shutdown.
}
}