Skip to content

Commit

Permalink
TRUNK-6216LocaleUtility#fromSpecification should be updated to suppor…
Browse files Browse the repository at this point in the history
…t BCP-47 language tags
  • Loading branch information
mherman22 committed Jan 1, 2024
1 parent 4fa07b8 commit 54f5ce4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
18 changes: 15 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,29 @@ 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) {
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
10 changes: 9 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,14 @@ 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)
*/
Expand Down

0 comments on commit 54f5ce4

Please sign in to comment.