Skip to content

Commit

Permalink
Creating Locale with specified Language script (#250)
Browse files Browse the repository at this point in the history
* Creating a util function to convert Locale string to Locale, but take language script into consideration

* emptz commit to trigger auto build
  • Loading branch information
VeljkoMaksimovic authored Apr 7, 2022
1 parent bbd714c commit b8944fb
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/).


Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ private List<Map<String, Object>> buildLocalesList(List<Locale> selectables) {
private Map<String, Object> buildLocaleMap(Locale locale,
Locale currentLocale) throws FileNotFoundException {
Map<String, Object> 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit b8944fb

Please sign in to comment.