VIVO-533 When loading properties files, mimic the language filtering of the RDFService.
This commit is contained in:
parent
4e9e4216da
commit
cd0cb50798
3 changed files with 172 additions and 4 deletions
|
@ -3,10 +3,14 @@
|
|||
package edu.cornell.mannlib.vitro.webapp.i18n;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Comparator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.MissingResourceException;
|
||||
import java.util.ResourceBundle;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
|
@ -16,6 +20,7 @@ import org.apache.commons.logging.Log;
|
|||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
||||
import edu.cornell.mannlib.vitro.webapp.i18n.selection.SelectedLocale;
|
||||
import edu.cornell.mannlib.vitro.webapp.utils.developer.DeveloperSettings;
|
||||
import edu.cornell.mannlib.vitro.webapp.utils.developer.DeveloperSettings.Keys;
|
||||
|
||||
|
@ -128,7 +133,7 @@ public class I18n {
|
|||
* If we are in development mode, clear the cache on each request.
|
||||
*/
|
||||
private void checkDevelopmentMode(HttpServletRequest req) {
|
||||
if (DeveloperSettings.getBean(req).getBoolean(Keys.I18N_DEFEAT_CACHE) ) {
|
||||
if (DeveloperSettings.getBean(req).getBoolean(Keys.I18N_DEFEAT_CACHE)) {
|
||||
log.debug("In development mode - clearing the cache.");
|
||||
clearCacheOnRequest(req);
|
||||
}
|
||||
|
@ -167,7 +172,7 @@ public class I18n {
|
|||
* Instead of looking in the classpath, look in the theme i18n directory and
|
||||
* the application i18n directory.
|
||||
*/
|
||||
private static class ThemeBasedControl extends ResourceBundle.Control {
|
||||
static class ThemeBasedControl extends ResourceBundle.Control {
|
||||
private static final String BUNDLE_DIRECTORY = "i18n/";
|
||||
private final ServletContext ctx;
|
||||
private final String themeDirectory;
|
||||
|
@ -215,6 +220,52 @@ public class I18n {
|
|||
themeI18nPath, this);
|
||||
}
|
||||
|
||||
/**
|
||||
* When creating the chain of acceptable Locales, include approximate
|
||||
* matches before giving up and using the root Locale.
|
||||
*
|
||||
* Check the list of supported Locales to see if any have the same
|
||||
* language but different region. If we find any, sort them and insert
|
||||
* them into the usual result list, just before the root Locale.
|
||||
*/
|
||||
@Override
|
||||
public List<Locale> getCandidateLocales(String baseName, Locale locale) {
|
||||
// Find the list of Locales that would normally be returned.
|
||||
List<Locale> usualList = super
|
||||
.getCandidateLocales(baseName, locale);
|
||||
|
||||
// If our "selectable locales" include no approximate matches that
|
||||
// are not already in the list, we're done.
|
||||
SortedSet<Locale> approximateMatches = findApproximateMatches(locale);
|
||||
approximateMatches.removeAll(usualList);
|
||||
if (approximateMatches.isEmpty()) {
|
||||
return usualList;
|
||||
}
|
||||
|
||||
// Otherwise, insert those approximate matches into the list just
|
||||
// before the ROOT locale.
|
||||
List<Locale> mergedList = new LinkedList<>(usualList);
|
||||
int rootLocaleHere = mergedList.indexOf(Locale.ROOT);
|
||||
if (rootLocaleHere == -1) {
|
||||
mergedList.addAll(approximateMatches);
|
||||
} else {
|
||||
mergedList.addAll(rootLocaleHere, approximateMatches);
|
||||
}
|
||||
return mergedList;
|
||||
}
|
||||
|
||||
private SortedSet<Locale> findApproximateMatches(Locale locale) {
|
||||
SortedSet<Locale> set = new TreeSet<>(new LocaleComparator());
|
||||
|
||||
for (Locale l : SelectedLocale.getSelectableLocales(ctx)) {
|
||||
if (locale.getLanguage().equals(l.getLanguage())) {
|
||||
set.add(l);
|
||||
}
|
||||
}
|
||||
|
||||
return set;
|
||||
}
|
||||
|
||||
/**
|
||||
* The documentation for ResourceBundle.Control.newBundle() says I
|
||||
* should throw these exceptions.
|
||||
|
@ -235,5 +286,20 @@ public class I18n {
|
|||
"format must be one of these: " + FORMAT_DEFAULT);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class LocaleComparator implements Comparator<Locale> {
|
||||
@Override
|
||||
public int compare(Locale o1, Locale o2) {
|
||||
int c = o1.getLanguage().compareTo(o2.getLanguage());
|
||||
if (c == 0) {
|
||||
c = o1.getCountry().compareTo(o2.getCountry());
|
||||
if (c == 0) {
|
||||
c = o1.getVariant().compareTo(o2.getVariant());
|
||||
}
|
||||
}
|
||||
return c;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
package edu.cornell.mannlib.vitro.webapp.i18n.selection;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
@ -124,7 +125,14 @@ public abstract class SelectedLocale {
|
|||
* an empty list, but never returns null.
|
||||
*/
|
||||
public static List<Locale> getSelectableLocales(HttpServletRequest req) {
|
||||
ServletContext ctx = req.getSession().getServletContext();
|
||||
return getSelectableLocales(req.getSession().getServletContext());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of selectable Locales from the servlet context. May return
|
||||
* an empty list, but never returns null.
|
||||
*/
|
||||
public static List<Locale> getSelectableLocales(ServletContext ctx) {
|
||||
Object ctxInfo = ctx.getAttribute(ATTRIBUTE_NAME);
|
||||
if (ctxInfo instanceof ContextSelectedLocale) {
|
||||
List<Locale> selectableLocales = ((ContextSelectedLocale) ctxInfo)
|
||||
|
@ -164,7 +172,8 @@ public abstract class SelectedLocale {
|
|||
}
|
||||
|
||||
this.forcedLocale = null;
|
||||
this.selectableLocales = selectableLocales;
|
||||
this.selectableLocales = Collections
|
||||
.unmodifiableList(new ArrayList<>(selectableLocales));
|
||||
}
|
||||
|
||||
public Locale getForcedLocale() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue