This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git


The following commit(s) were added to refs/heads/master by this push:
     new 03e7c36d2 `LocaleUtils.toLocale(String)` for a 2 letter country code 
now returns a value instead of throwing an `IllegalArgumentException`
03e7c36d2 is described below

commit 03e7c36d2dafca70e1f0e2c27a77ed4816cecf3f
Author: Gary D. Gregory <[email protected]>
AuthorDate: Sun Oct 5 14:27:44 2025 -0400

    `LocaleUtils.toLocale(String)` for a 2 letter country code now returns a
    value instead of throwing an `IllegalArgumentException`
---
 src/changes/changes.xml                              |  1 +
 .../java/org/apache/commons/lang3/LocaleUtils.java   |  3 +++
 .../org/apache/commons/lang3/LocaleUtilsTest.java    | 20 ++++++++++++++++++++
 3 files changed, 24 insertions(+)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 26ca4f666..520f87c3d 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -50,6 +50,7 @@ The <action> type attribute can be add,update,fix,remove.
     <action                   type="fix" dev="ggregory" due-to="Marcono1234, 
Gary Gregory">[javadoc] Improve StringUtils Javadoc #1450.</action>
     <action                   type="fix" dev="ggregory" due-to="mayuming, Gary 
Gregory">Fix internal inverted logic in private isEnum() method and correct its 
usage in getFirstEnum() #1454.</action>
     <action                   type="fix" dev="ggregory" due-to="William 
Degrange, Gary Gregory, Rob Spoor">Use accessors in ToStringStyle so subclasses 
can effectively override them.</action>
+    <action                   type="fix" dev="ggregory" due-to="aaaaaa, Gary 
Gregory">`LocaleUtils.toLocale(String)` for a 2 letter country code now returns 
a value instead of throwing an `IllegalArgumentException`.</action>
     <!-- ADD -->
     <action                   type="add" dev="ggregory" due-to="Gary 
Gregory">Add SystemProperties.getPath(String, Supplier&lt;Path&gt;).</action>
     <action                   type="add" dev="ggregory" due-to="Gary 
Gregory">Add JavaVersion.JAVA_25.</action>
diff --git a/src/main/java/org/apache/commons/lang3/LocaleUtils.java 
b/src/main/java/org/apache/commons/lang3/LocaleUtils.java
index d813f7490..481b80823 100644
--- a/src/main/java/org/apache/commons/lang3/LocaleUtils.java
+++ b/src/main/java/org/apache/commons/lang3/LocaleUtils.java
@@ -334,6 +334,9 @@ private static Locale parseLocale(final String str) {
                 return new Locale(language, country, variant);
             }
         }
+        if (ArrayUtils.contains(Locale.getISOCountries(), str)) {
+            return new Locale(StringUtils.EMPTY, str);
+        }
         throw new IllegalArgumentException("Invalid locale format: " + str);
     }
 
diff --git a/src/test/java/org/apache/commons/lang3/LocaleUtilsTest.java 
b/src/test/java/org/apache/commons/lang3/LocaleUtilsTest.java
index 3b9663ab4..b5c16983d 100644
--- a/src/test/java/org/apache/commons/lang3/LocaleUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/LocaleUtilsTest.java
@@ -41,6 +41,7 @@
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.params.ParameterizedTest;
 import org.junit.jupiter.params.provider.MethodSource;
+import org.junitpioneer.jupiter.DefaultLocale;
 
 /**
  * Tests for {@link LocaleUtils}.
@@ -516,6 +517,25 @@ void testToLocale_Locale_defaults() {
         assertEquals(Locale.getDefault(), 
LocaleUtils.toLocale(Locale.getDefault()));
     }
 
+    @Test
+    void testToLocaleGetIso3Country() {
+        assertEquals("USA", LocaleUtils.toLocale("US").getISO3Country());
+        assertEquals("GBR", LocaleUtils.toLocale("GB").getISO3Country());
+        assertEquals("PAK", LocaleUtils.toLocale("PK").getISO3Country());
+        assertEquals("IND", LocaleUtils.toLocale("IN").getISO3Country());
+        assertEquals("FRA", LocaleUtils.toLocale("FR").getISO3Country());
+    }
+
+    @Test
+    @DefaultLocale(country = "US", language = "en")
+    void testToLocaleGetIso3Language() {
+        assertEquals("United States", 
LocaleUtils.toLocale("US").getDisplayCountry());
+        assertEquals("United Kingdom", 
LocaleUtils.toLocale("GB").getDisplayCountry());
+        assertEquals("Pakistan", 
LocaleUtils.toLocale("PK").getDisplayCountry());
+        assertEquals("India", LocaleUtils.toLocale("IN").getDisplayCountry());
+        assertEquals("France", LocaleUtils.toLocale("FR").getDisplayCountry());
+    }
+
     /**
      * Test toLocale(Locale) method.
      */

Reply via email to