OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/android/locale_utils.h" | 5 #include "base/android/locale_utils.h" |
6 | 6 |
7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
8 #include "base/android/jni_string.h" | 8 #include "base/android/jni_string.h" |
9 #include "base/android/scoped_java_ref.h" | 9 #include "base/android/scoped_java_ref.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 26 matching lines...) Expand all Loading... |
37 max_capacity, | 37 max_capacity, |
38 &error); | 38 &error); |
39 DCHECK(U_SUCCESS(error)); | 39 DCHECK(U_SUCCESS(error)); |
40 DCHECK(actual_length < max_capacity); | 40 DCHECK(actual_length < max_capacity); |
41 result.resize(actual_length); | 41 result.resize(actual_length); |
42 return result; | 42 return result; |
43 } | 43 } |
44 | 44 |
45 ScopedJavaLocalRef<jobject> NewJavaLocale( | 45 ScopedJavaLocalRef<jobject> NewJavaLocale( |
46 JNIEnv* env, | 46 JNIEnv* env, |
47 ScopedJavaLocalRef<jclass> locale_class, | |
48 jmethodID constructor_id, | |
49 const std::string& locale) { | 47 const std::string& locale) { |
50 // TODO(wangxianzhu): Use new Locale API once Android supports scripts. | 48 // TODO(wangxianzhu): Use new Locale API once Android supports scripts. |
51 std::string language = GetLocaleComponent( | 49 std::string language = GetLocaleComponent( |
52 locale, uloc_getLanguage, ULOC_LANG_CAPACITY); | 50 locale, uloc_getLanguage, ULOC_LANG_CAPACITY); |
53 std::string country = GetLocaleComponent( | 51 std::string country = GetLocaleComponent( |
54 locale, uloc_getCountry, ULOC_COUNTRY_CAPACITY); | 52 locale, uloc_getCountry, ULOC_COUNTRY_CAPACITY); |
55 std::string variant = GetLocaleComponent( | 53 std::string variant = GetLocaleComponent( |
56 locale, uloc_getVariant, ULOC_FULLNAME_CAPACITY); | 54 locale, uloc_getVariant, ULOC_FULLNAME_CAPACITY); |
57 return ScopedJavaLocalRef<jobject>( | 55 return Java_LocaleUtils_getJavaLocale(env, |
58 env, env->NewObject( | |
59 locale_class.obj(), constructor_id, | |
60 ConvertUTF8ToJavaString(env, language).obj(), | 56 ConvertUTF8ToJavaString(env, language).obj(), |
61 ConvertUTF8ToJavaString(env, country).obj(), | 57 ConvertUTF8ToJavaString(env, country).obj(), |
62 ConvertUTF8ToJavaString(env, variant).obj())); | 58 ConvertUTF8ToJavaString(env, variant).obj()); |
63 } | 59 } |
64 | 60 |
65 } // namespace | 61 } // namespace |
66 | 62 |
67 string16 GetDisplayNameForLocale(const std::string& locale, | 63 string16 GetDisplayNameForLocale(const std::string& locale, |
68 const std::string& display_locale) { | 64 const std::string& display_locale) { |
69 JNIEnv* env = AttachCurrentThread(); | 65 JNIEnv* env = AttachCurrentThread(); |
| 66 ScopedJavaLocalRef<jobject> java_locale = |
| 67 NewJavaLocale(env, locale); |
| 68 ScopedJavaLocalRef<jobject> java_display_locale = |
| 69 NewJavaLocale(env, display_locale); |
70 | 70 |
71 ScopedJavaLocalRef<jclass> locale_class = GetClass(env, "java/util/Locale"); | |
72 jmethodID constructor_id = MethodID::Get<MethodID::TYPE_INSTANCE>( | |
73 env, locale_class.obj(), "<init>", | |
74 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"); | |
75 ScopedJavaLocalRef<jobject> java_locale = NewJavaLocale( | |
76 env, locale_class, constructor_id, locale); | |
77 ScopedJavaLocalRef<jobject> java_display_locale = NewJavaLocale( | |
78 env, locale_class, constructor_id, display_locale); | |
79 | |
80 jmethodID method_id = MethodID::Get<MethodID::TYPE_INSTANCE>( | |
81 env, locale_class.obj(), "getDisplayName", | |
82 "(Ljava/util/Locale;)Ljava/lang/String;"); | |
83 ScopedJavaLocalRef<jstring> java_result( | 71 ScopedJavaLocalRef<jstring> java_result( |
84 env, | 72 Java_LocaleUtils_getDisplayNameForLocale(env, |
85 static_cast<jstring>(env->CallObjectMethod(java_locale.obj(), method_id, | 73 java_locale.obj(), |
86 java_display_locale.obj()))); | 74 java_display_locale.obj())); |
87 return ConvertJavaStringToUTF16(java_result); | 75 return ConvertJavaStringToUTF16(java_result); |
88 } | 76 } |
89 | 77 |
90 bool RegisterLocaleUtils(JNIEnv* env) { | 78 bool RegisterLocaleUtils(JNIEnv* env) { |
91 return RegisterNativesImpl(env); | 79 return RegisterNativesImpl(env); |
92 } | 80 } |
93 | 81 |
94 } // namespace android | 82 } // namespace android |
95 } // namespace base | 83 } // namespace base |
OLD | NEW |