diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/LocaleNativeGenerator.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/LocaleNativeGenerator.java index 2ec52ee30..1a8fec60b 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/LocaleNativeGenerator.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/LocaleNativeGenerator.java @@ -34,8 +34,9 @@ public class LocaleNativeGenerator implements Generator, DependencyPlugin { switch (methodRef.getName()) { case "getDisplayCountry": writer.append("var result = ").appendClass("java.util.Locale").append(".$CLDR[$rt_ustr(") - .append(context.getParameterName(1)).append(")].territories[$rt_ustr(") - .append(context.getParameterName(2)).append(")];").softNewLine(); + .append(context.getParameterName(1)).append(")];").softNewLine(); + writer.append("result = result ? result.territories[$rt_ustr(") + .append(context.getParameterName(2)).append(")] : undefined;").softNewLine(); writer.append("return result ? $rt_str(result) : null").softNewLine(); break; case "getDisplayLanguage": diff --git a/teavm-classlib/src/test/java/org/teavm/classlib/java/util/LocaleTest.java b/teavm-classlib/src/test/java/org/teavm/classlib/java/util/LocaleTest.java index 6557f8175..192402fb2 100644 --- a/teavm-classlib/src/test/java/org/teavm/classlib/java/util/LocaleTest.java +++ b/teavm-classlib/src/test/java/org/teavm/classlib/java/util/LocaleTest.java @@ -30,12 +30,30 @@ public class LocaleTest { } @Test - public void localeNamesProvided() { - Locale english = new Locale("en", "US"); + public void languageNamesProvided() { + Locale english = new Locale("en", ""); + Locale usEnglish = new Locale("en", "US"); Locale russian = new Locale("ru", "RU"); assertEquals("English", english.getDisplayLanguage(english)); + assertEquals("English", english.getDisplayLanguage(usEnglish)); assertEquals("Russian", russian.getDisplayLanguage(english)); + assertEquals("English", english.getDisplayLanguage(usEnglish)); + assertEquals("Russian", russian.getDisplayLanguage(usEnglish)); assertEquals("английский", english.getDisplayLanguage(russian)); assertEquals("русский", russian.getDisplayLanguage(russian)); } + + @Test + public void countryNamesProvided() { + Locale usEnglish = new Locale("en", "US"); + Locale gbEnglish = new Locale("en", "GB"); + Locale russian = new Locale("ru", "RU"); + assertEquals("United Kingdom", gbEnglish.getDisplayCountry(usEnglish)); + assertEquals("United States", usEnglish.getDisplayCountry(usEnglish)); + assertEquals("Russia", russian.getDisplayCountry(usEnglish)); + // JVM gives here name that differs to the name provided by CLDR + //assertEquals("Соединенное Королевство", gbEnglish.getDisplayCountry(russian)); + assertEquals("Соединенные Штаты", usEnglish.getDisplayCountry(russian)); + assertEquals("Россия", russian.getDisplayCountry(russian)); + } }