diff --git a/api/src/main/java/org/openmrs/util/LocaleUtility.java b/api/src/main/java/org/openmrs/util/LocaleUtility.java index 54ba421e1a8c..547c1e0384d4 100644 --- a/api/src/main/java/org/openmrs/util/LocaleUtility.java +++ b/api/src/main/java/org/openmrs/util/LocaleUtility.java @@ -135,7 +135,11 @@ public static Locale fromSpecification(String localeSpecification) { try { createdLocale = LocaleUtils.toLocale(localeSpecification); } catch (IllegalArgumentException e) { - createdLocale = generateLocaleFromLegacyFormat(localeSpecification); + if (localeSpecification.matches("[a-zA-Z]{2}[-_][a-zA-Z]{2,}")) { + return null; + } else { + createdLocale = generateLocaleFromLegacyFormat(localeSpecification); + } } return createdLocale; diff --git a/api/src/test/java/org/openmrs/util/LocaleUtilityTest.java b/api/src/test/java/org/openmrs/util/LocaleUtilityTest.java index a7a06cfae37d..140b9f263a17 100644 --- a/api/src/test/java/org/openmrs/util/LocaleUtilityTest.java +++ b/api/src/test/java/org/openmrs/util/LocaleUtilityTest.java @@ -185,6 +185,17 @@ public void fromSpecification_shouldGetLocaleFromLanguageCodeAndCountryCode() { 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)