7002320: Locale.forLanguageTag()/toLanguageTag() not working properly with ja_JP_JP locale
authornaoto
Mon, 13 Dec 2010 13:16:08 -0800
changeset 7513 7091fccbc86d
parent 7512 1b4801f0c6ff
child 7514 110025c97f09
child 7777 1241aaaec247
7002320: Locale.forLanguageTag()/toLanguageTag() not working properly with ja_JP_JP locale Reviewed-by: dougfelt
jdk/src/share/classes/java/util/Locale.java
jdk/test/java/util/Locale/LocaleEnhanceTest.java
--- a/jdk/src/share/classes/java/util/Locale.java	Fri Dec 10 14:02:33 2010 -0800
+++ b/jdk/src/share/classes/java/util/Locale.java	Mon Dec 13 13:16:08 2010 -0800
@@ -1426,7 +1426,12 @@
         LanguageTag tag = LanguageTag.parse(languageTag, null);
         InternalLocaleBuilder bldr = new InternalLocaleBuilder();
         bldr.setLanguageTag(tag);
-        return getInstance(bldr.getBaseLocale(), bldr.getLocaleExtensions());
+        BaseLocale base = bldr.getBaseLocale();
+        LocaleExtensions exts = bldr.getLocaleExtensions();
+        if (exts.isEmpty() && base.getVariant().length() > 0) {
+            exts = getCompatibilityExtensions(base.getLanguage(), base.getScript(), base.getRegion(), base.getVariant());
+        }
+        return getInstance(base, exts);
     }
 
     /**
@@ -2485,6 +2490,10 @@
         public Locale build() {
             BaseLocale baseloc = _locbld.getBaseLocale();
             LocaleExtensions extensions = _locbld.getLocaleExtensions();
+            if (extensions.isEmpty() && baseloc.getVariant().length() > 0) {
+                extensions = getCompatibilityExtensions(baseloc.getLanguage(), baseloc.getScript(),
+                        baseloc.getRegion(), baseloc.getVariant());
+            }
             return Locale.getInstance(baseloc, extensions);
         }
     }
--- a/jdk/test/java/util/Locale/LocaleEnhanceTest.java	Fri Dec 10 14:02:33 2010 -0800
+++ b/jdk/test/java/util/Locale/LocaleEnhanceTest.java	Mon Dec 13 13:16:08 2010 -0800
@@ -1201,6 +1201,49 @@
         }
     }
 
+    public void testBug7002320() {
+        // forLanguageTag() and Builder.setLanguageTag(String)
+        // should add a location extension for following two cases.
+        //
+        // 1. language/country are "ja"/"JP" and the resolved variant (x-lvariant-*)
+        //    is exactly "JP" and no BCP 47 extensions are available, then add
+        //    a Unicode locale extension "ca-japanese".
+        // 2. language/country are "th"/"TH" and the resolved variant is exactly
+        //    "TH" and no BCP 47 extensions are available, then add a Unicode locale
+        //    extension "nu-thai".
+        //
+        String[][] testdata = {
+            {"ja-JP-x-lvariant-JP", "ja-JP-u-ca-japanese-x-lvariant-JP"},   // special case 1
+            {"ja-JP-x-lvariant-JP-XXX"},
+            {"ja-JP-u-ca-japanese-x-lvariant-JP"},
+            {"ja-JP-u-ca-gregory-x-lvariant-JP"},
+            {"ja-JP-u-cu-jpy-x-lvariant-JP"},
+            {"ja-x-lvariant-JP"},
+            {"th-TH-x-lvariant-TH", "th-TH-u-nu-thai-x-lvariant-TH"},   // special case 2
+            {"th-TH-u-nu-thai-x-lvariant-TH"},
+            {"en-US-x-lvariant-JP"},
+        };
+
+        Builder bldr = new Builder();
+
+        for (String[] data : testdata) {
+            String in = data[0];
+            String expected = (data.length == 1) ? data[0] : data[1];
+
+            // forLanguageTag
+            Locale loc = Locale.forLanguageTag(in);
+            String out = loc.toLanguageTag();
+            assertEquals("Language tag roundtrip by forLanguageTag with input: " + in, expected, out);
+
+            // setLanguageTag
+            bldr.clear();
+            bldr.setLanguageTag(in);
+            loc = bldr.build();
+            out = loc.toLanguageTag();
+            assertEquals("Language tag roundtrip by Builder.setLanguageTag with input: " + in, expected, out);
+        }
+    }
+
     ///
     /// utility asserts
     ///