diff --git a/README.md b/README.md index dbbc2abf86..3df0e02f13 100644 --- a/README.md +++ b/README.md @@ -20,3 +20,4 @@ research and scholarship portal, [VIVO](https://lyrasis.org/vivo/). For more information, contact the [VIVO community](https://lyrasis.org/vivo/resources/contact/). + diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/i18n/selection/LocaleSelectionController.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/i18n/selection/LocaleSelectionController.java index de9d52c883..aa013c965d 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/i18n/selection/LocaleSelectionController.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/i18n/selection/LocaleSelectionController.java @@ -12,6 +12,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import edu.cornell.mannlib.vitro.webapp.utils.LocaleUtility; import org.apache.commons.lang3.LocaleUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; @@ -74,7 +75,7 @@ private void processSelectedLocale(HttpServletRequest req, Locale locale = null; try { - locale = LocaleUtils.toLocale(selectedLocale.trim()); + locale = LocaleUtility.languageStringToLocale(selectedLocale); log.debug("Locale selection is " + locale); } catch (IllegalArgumentException e) { log.error("Failed to convert the selection to a Locale", e); diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/i18n/selection/LocaleSelectionDataGetter.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/i18n/selection/LocaleSelectionDataGetter.java index d0bff6349f..17fa89fed2 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/i18n/selection/LocaleSelectionDataGetter.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/i18n/selection/LocaleSelectionDataGetter.java @@ -88,7 +88,8 @@ private List> buildLocalesList(List selectables) { private Map buildLocaleMap(Locale locale, Locale currentLocale) throws FileNotFoundException { Map map = new HashMap<>(); - map.put("code", locale.toString()); + + map.put("code", locale.toLanguageTag().replace('-','_')); map.put("label", locale.getDisplayLanguage(locale)); map.put("country", locale.getDisplayCountry(locale)); map.put("selected", currentLocale.equals(locale)); diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/i18n/selection/LocaleSelectionSetup.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/i18n/selection/LocaleSelectionSetup.java index 596880edd5..98ff8b02f9 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/i18n/selection/LocaleSelectionSetup.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/i18n/selection/LocaleSelectionSetup.java @@ -10,12 +10,16 @@ import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; +import edu.cornell.mannlib.vitro.webapp.utils.LocaleUtility; 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.i18n.I18n; import edu.cornell.mannlib.vitro.webapp.startup.StartupStatus; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + /** * Check the ConfigurationProperties for a forced locale, or for a @@ -24,6 +28,10 @@ * Create the appropriate Locale objects and store them in the ServletContext. */ public class LocaleSelectionSetup implements ServletContextListener { + + private static final Log log = LogFactory + .getLog(LocaleSelectionSetup.class); + /** * If this is set, the locale is forced. No selection will be offered to the * user, and browser locales will be ignored. @@ -143,12 +151,13 @@ private Locale buildLocale(String localeString) throw new IllegalArgumentException("Invalid locale format"); } - Locale locale = LocaleUtils.toLocale(localeString); + Locale locale = LocaleUtility.languageStringToLocale(localeString); if (!"es_GO".equals(localeString) && // No complaint about bogus locale !LocaleUtils.isAvailableLocale(locale)) { ssWarning("'" + locale + "' is not a recognized locale."); } + return locale; } diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/utils/LocaleUtility.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/utils/LocaleUtility.java new file mode 100644 index 0000000000..efbe512888 --- /dev/null +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/utils/LocaleUtility.java @@ -0,0 +1,19 @@ +package edu.cornell.mannlib.vitro.webapp.utils; + +import org.apache.commons.lang3.LocaleUtils; + +import java.util.Locale; + +public final class LocaleUtility { + + private LocaleUtility(){} + + public static Locale languageStringToLocale(String localeString){ + String[] parsedLoc = localeString.trim().split("_", -1); + //regex pattern for locale tag with script specified + Locale locale = localeString.matches("^[a-z]{1,3}_[A-Z][a-z]{3}_[A-Z]{2}") ? + new Locale.Builder().setLanguage(parsedLoc[0]).setRegion(parsedLoc[2]).setScript(parsedLoc[1]).build() : + LocaleUtils.toLocale(localeString); + return locale; + } +}