summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNarayan Kamath <narayan@google.com>2014-04-11 14:30:59 +0100
committerNarayan Kamath <narayan@google.com>2014-04-11 19:04:38 +0100
commitde0eb683370d789e7bb25673b350b8dbf2ba5d69 (patch)
treebc925fee2285d16c7269ff400e271e95add37c39
parentd689dcb6e42dd1268402dcd385dbe8810afe039a (diff)
downloadlibcore-de0eb683370d789e7bb25673b350b8dbf2ba5d69.zip
libcore-de0eb683370d789e7bb25673b350b8dbf2ba5d69.tar.gz
libcore-de0eb683370d789e7bb25673b350b8dbf2ba5d69.tar.bz2
Set ICU's default locale from Locale.getDefault.
bug: 2541757 Change-Id: I5de58be7e8a701f2c40182ea54bceff85ec1aad2
-rw-r--r--luni/src/main/java/java/util/Locale.java1
-rw-r--r--luni/src/main/java/libcore/icu/ICU.java3
-rw-r--r--luni/src/main/native/libcore_icu_ICU.cpp19
-rw-r--r--luni/src/test/java/libcore/java/util/LocaleTest.java22
4 files changed, 45 insertions, 0 deletions
diff --git a/luni/src/main/java/java/util/Locale.java b/luni/src/main/java/java/util/Locale.java
index 3161769..4fc5675 100644
--- a/luni/src/main/java/java/util/Locale.java
+++ b/luni/src/main/java/java/util/Locale.java
@@ -1337,6 +1337,7 @@ public final class Locale implements Cloneable, Serializable {
throw new NullPointerException("locale == null");
}
defaultLocale = locale;
+ ICU.setDefaultLocale(ICU.localeIdFromLocale(locale));
}
/**
diff --git a/luni/src/main/java/libcore/icu/ICU.java b/luni/src/main/java/libcore/icu/ICU.java
index 078c83a..f60f427 100644
--- a/luni/src/main/java/libcore/icu/ICU.java
+++ b/luni/src/main/java/libcore/icu/ICU.java
@@ -474,4 +474,7 @@ public final class ICU {
public static native String languageTagForLocale(String locale);
static native boolean initLocaleDataNative(String locale, LocaleData result);
+
+ public static native void setDefaultLocale(String locale);
+ public static native String getDefaultLocale();
}
diff --git a/luni/src/main/native/libcore_icu_ICU.cpp b/luni/src/main/native/libcore_icu_ICU.cpp
index d498ebf..9dc1cae 100644
--- a/luni/src/main/native/libcore_icu_ICU.cpp
+++ b/luni/src/main/native/libcore_icu_ICU.cpp
@@ -761,6 +761,23 @@ static jstring ICU_getBestDateTimePatternNative(JNIEnv* env, jclass, jstring jav
return env->NewString(result.getBuffer(), result.length());
}
+static void ICU_setDefaultLocale(JNIEnv* env, jclass, jstring javaLocaleName) {
+ Locale locale = getLocale(env, javaLocaleName);
+ UErrorCode status = U_ZERO_ERROR;
+
+ // TODO: Should we check whether locale.isBogus() here ? ICU will
+ // accept bogus locales as the default without complaint. It
+ // shouldn't make a difference in practice, users that set a bogus
+ // locale as the default shouldn't have any realistic expectation that
+ // things like defaults etc. will work correctly.
+ Locale::setDefault(locale, status);
+ maybeThrowIcuException(env, "Locale::setDefault", status);
+}
+
+static jstring ICU_getDefaultLocale(JNIEnv* env, jclass) {
+ return env->NewStringUTF(Locale::getDefault().getName());
+}
+
static JNINativeMethod gMethods[] = {
NATIVE_METHOD(ICU, addLikelySubtags, "(Ljava/lang/String;)Ljava/lang/String;"),
NATIVE_METHOD(ICU, getAvailableBreakIteratorLocalesNative, "()[Ljava/lang/String;"),
@@ -777,6 +794,7 @@ static JNINativeMethod gMethods[] = {
NATIVE_METHOD(ICU, getCurrencyFractionDigits, "(Ljava/lang/String;)I"),
NATIVE_METHOD(ICU, getCurrencyNumericCode, "(Ljava/lang/String;)I"),
NATIVE_METHOD(ICU, getCurrencySymbol, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"),
+ NATIVE_METHOD(ICU, getDefaultLocale, "()Ljava/lang/String;"),
NATIVE_METHOD(ICU, getDisplayCountryNative, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"),
NATIVE_METHOD(ICU, getDisplayLanguageNative, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"),
NATIVE_METHOD(ICU, getDisplayScriptNative, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"),
@@ -791,6 +809,7 @@ static JNINativeMethod gMethods[] = {
NATIVE_METHOD(ICU, languageTagForLocale, "(Ljava/lang/String;)Ljava/lang/String;"),
NATIVE_METHOD(ICU, localeForLanguageTag, "(Ljava/lang/String;Z)Ljava/lang/String;"),
NATIVE_METHOD(ICU, initLocaleDataNative, "(Ljava/lang/String;Llibcore/icu/LocaleData;)Z"),
+ NATIVE_METHOD(ICU, setDefaultLocale, "(Ljava/lang/String;)V"),
NATIVE_METHOD(ICU, toLowerCase, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"),
NATIVE_METHOD(ICU, toUpperCase, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"),
};
diff --git a/luni/src/test/java/libcore/java/util/LocaleTest.java b/luni/src/test/java/libcore/java/util/LocaleTest.java
index cadd472..025a3ba 100644
--- a/luni/src/test/java/libcore/java/util/LocaleTest.java
+++ b/luni/src/test/java/libcore/java/util/LocaleTest.java
@@ -30,6 +30,7 @@ import java.util.Map;
import java.util.MissingResourceException;
import java.util.Set;
import java.util.TreeMap;
+import libcore.icu.ICU;
public class LocaleTest extends junit.framework.TestCase {
// http://b/2611311; if there's no display language/country/variant, use the raw codes.
@@ -1160,4 +1161,25 @@ public class LocaleTest extends junit.framework.TestCase {
.build();
assertEquals("en-US-u-va-posix", posix.toLanguageTag());
}
+
+ public void test_setDefault_setsICUDefaultLocale() {
+ Locale l = Locale.getDefault();
+
+ try {
+ Locale.setDefault(Locale.GERMANY);
+ assertEquals("de_DE", ICU.getDefaultLocale());
+
+ try {
+ Locale.setDefault(null);
+ fail();
+ } catch (NullPointerException expected) {
+ assertEquals(Locale.GERMANY, Locale.getDefault());
+ }
+
+ Locale.setDefault(new Locale("bogus", "LOCALE"));
+ assertEquals("bogus__LOCALE", ICU.getDefaultLocale());
+ } finally {
+ Locale.setDefault(l);
+ }
+ }
}