Skip to content

Commit

Permalink
TRUNK-6216: LocaleUtility#fromSpecification should be updated to supp…
Browse files Browse the repository at this point in the history
…ort BCP-47 language tags. (#4523)
  • Loading branch information
mherman22 authored Mar 1, 2024
1 parent 11cbda0 commit 8ed43b6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
22 changes: 19 additions & 3 deletions api/src/main/java/org/openmrs/util/LocaleUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.MissingResourceException;
import java.util.Set;

import org.apache.commons.lang3.LocaleUtils;
import org.openmrs.GlobalProperty;
import org.openmrs.api.GlobalPropertyListener;
import org.openmrs.api.context.Context;
Expand Down Expand Up @@ -127,18 +128,33 @@ public static boolean areCompatible(Locale lhs, Locale rhs) {
* <strong>Should</strong> get locale from language code country code and variant
*/
public static Locale fromSpecification(String localeSpecification) {
Locale createdLocale = null;
Locale createdLocale;

localeSpecification = localeSpecification.trim();

try {
createdLocale = LocaleUtils.toLocale(localeSpecification);
} catch (IllegalArgumentException e) {
if (localeSpecification.matches("[a-zA-Z]{2}[-_][a-zA-Z]{2,}")) {
return null;
} else {
createdLocale = generateLocaleFromLegacyFormat(localeSpecification);
}
}

return createdLocale;
}

private static Locale generateLocaleFromLegacyFormat(String localeSpecification) {
Locale createdLocale = null;

String[] localeComponents = localeSpecification.split("_");
if (localeComponents.length == 1) {
createdLocale = new Locale(localeComponents[0]);
} else if (localeComponents.length == 2) {
createdLocale = new Locale(localeComponents[0], localeComponents[1]);
} else if (localeComponents.length > 2) {
String variant = localeSpecification.substring(localeSpecification.indexOf(localeComponents[2])); // gets everything after the
// second underscore
String variant = localeSpecification.substring(localeSpecification.indexOf(localeComponents[2]));
createdLocale = new Locale(localeComponents[0], localeComponents[1], variant);
}

Expand Down
21 changes: 20 additions & 1 deletion api/src/test/java/org/openmrs/util/LocaleUtilityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
Expand Down Expand Up @@ -178,6 +178,25 @@ public void fromSpecification_shouldGetLocaleFromLanguageCodeAndCountryCode() {
assertEquals(Locale.UK, LocaleUtility.fromSpecification("en_GB"));
}

/**
* @see LocaleUtility#fromSpecification(String)
*/
@Test
public void fromSpecification_shouldGetLocaleFromBCP47Format() {
assertEquals(Locale.UK, LocaleUtility.fromSpecification("en-GB"));
}

/**
* @see LocaleUtility#fromSpecification(String)
*/
@Test
public void fromSpecification_shouldReturnNullWhenLocaleFormatIsIncorrectLocaleFromBCP47Format() {
LocaleUtility.fromSpecification("en-USA");
LocaleUtility.fromSpecification("en_USA");
assertEquals(LocaleUtility.fromSpecification("en-USA"), null);
assertEquals(LocaleUtility.fromSpecification("en_USA"), null);
}

/**
* @see LocaleUtility#fromSpecification(String)
*/
Expand Down

0 comments on commit 8ed43b6

Please sign in to comment.