Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(917)

Side by Side Diff: base/android/locale_utils.cc

Issue 10224004: Use Android API for GetDisplayNameForLocale(). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "base/android/locale_utils.h"
6
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "base/android/scoped_java_ref.h"
10 #include "base/string_util.h"
11 #include "jni/locale_utils_jni.h"
12 #include "unicode/uloc.h"
13
14 namespace base {
15 namespace android {
16
17 std::string GetDefaultLocale() {
18 JNIEnv* env = AttachCurrentThread();
19 ScopedJavaLocalRef<jstring> locale = Java_LocaleUtils_getDefaultLocale(env);
20 return ConvertJavaStringToUTF8(locale);
21 }
22
23 namespace {
24
25 std::string GetLocaleComponent(
26 const std::string& locale,
27 int32_t (*uloc_func)(const char*, char*, int32_t, UErrorCode*),
John Grabowski 2012/04/25 20:32:27 consider a typedef for this func pointer
Xianzhu 2012/04/25 22:31:46 Done.
28 int32_t max_capacity) {
29 std::string result;
30 UErrorCode error = U_ZERO_ERROR;
31 int32_t actual_length = uloc_func(locale.c_str(),
32 WriteInto(&result, max_capacity),
33 max_capacity,
34 &error);
35 DCHECK(U_SUCCESS(error));
Mark Mentovai 2012/04/25 20:00:20 #include "base/logging.h" for this.
Xianzhu 2012/04/25 22:31:46 Done.
36 DCHECK(actual_length < max_capacity);
37 result.resize(actual_length);
38 return result;
39 }
40
41 ScopedJavaLocalRef<jobject> NewJavaLocale(
42 JNIEnv* env,
43 ScopedJavaLocalRef<jclass> locale_class,
44 jmethodID constructor_id,
45 const std::string& locale) {
46 std::string language = GetLocaleComponent(
47 locale, uloc_getLanguage, ULOC_LANG_CAPACITY);
48 std::string country = GetLocaleComponent(
49 locale, uloc_getCountry, ULOC_COUNTRY_CAPACITY);
50 std::string variant = GetLocaleComponent(
51 locale, uloc_getVariant, ULOC_FULLNAME_CAPACITY);
52 return ScopedJavaLocalRef<jobject>(
53 env, env->NewObject(
54 locale_class.obj(), constructor_id,
55 ConvertUTF8ToJavaString(env, language).obj(),
56 ConvertUTF8ToJavaString(env, country).obj(),
57 ConvertUTF8ToJavaString(env, variant).obj()));
58 }
59
60 } // namespace
61
62 string16 GetDisplayNameForLocale(const std::string& locale,
63 const std::string& display_locale) {
64 JNIEnv* env = AttachCurrentThread();
65
66 ScopedJavaLocalRef<jclass> locale_class = GetClass(env, "java/util/Locale");
67 jmethodID constructor_id = GetMethodID(
68 env, locale_class, "<init>",
69 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
70 ScopedJavaLocalRef<jobject> java_locale = NewJavaLocale(
71 env, locale_class, constructor_id, locale);
72 ScopedJavaLocalRef<jobject> java_display_locale = NewJavaLocale(
73 env, locale_class, constructor_id, display_locale);
74
75 jmethodID method_id = GetMethodID(env, locale_class, "getDisplayName",
76 "(Ljava/util/Locale;)Ljava/lang/String;");
77 ScopedJavaLocalRef<jstring> java_result(
78 env,
79 static_cast<jstring>(env->CallObjectMethod(java_locale.obj(), method_id,
80 java_display_locale.obj())));
81 return ConvertJavaStringToUTF16(java_result);
82 }
83
84 bool RegisterLocaleUtils(JNIEnv* env) {
85 return RegisterNativesImpl(env);
86 }
87
88 } // namespace android
89 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698