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

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: jungshik@google.com->jshin@chromium.org in OWNERS Created 8 years, 7 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/logging.h"
11 #include "base/string_util.h"
12 #include "jni/locale_utils_jni.h"
13 #include "unicode/uloc.h"
14
15 namespace base {
16 namespace android {
17
18 std::string GetDefaultLocale() {
19 JNIEnv* env = AttachCurrentThread();
20 ScopedJavaLocalRef<jstring> locale = Java_LocaleUtils_getDefaultLocale(env);
21 return ConvertJavaStringToUTF8(locale);
22 }
23
24 namespace {
25
26 // Common prototype of ICU uloc_getXXX() functions.
27 typedef int32_t (*UlocGetComponentFunc)(const char*, char*, int32_t,
28 UErrorCode*);
29
30 std::string GetLocaleComponent(const std::string& locale,
31 UlocGetComponentFunc uloc_func,
32 int32_t max_capacity) {
33 std::string result;
34 UErrorCode error = U_ZERO_ERROR;
35 int32_t actual_length = uloc_func(locale.c_str(),
36 WriteInto(&result, max_capacity),
37 max_capacity,
38 &error);
39 DCHECK(U_SUCCESS(error));
40 DCHECK(actual_length < max_capacity);
41 result.resize(actual_length);
42 return result;
43 }
44
45 ScopedJavaLocalRef<jobject> NewJavaLocale(
46 JNIEnv* env,
47 ScopedJavaLocalRef<jclass> locale_class,
48 jmethodID constructor_id,
49 const std::string& locale) {
50 std::string language = GetLocaleComponent(
51 locale, uloc_getLanguage, ULOC_LANG_CAPACITY);
52 std::string country = GetLocaleComponent(
53 locale, uloc_getCountry, ULOC_COUNTRY_CAPACITY);
54 std::string variant = GetLocaleComponent(
55 locale, uloc_getVariant, ULOC_FULLNAME_CAPACITY);
56 return ScopedJavaLocalRef<jobject>(
57 env, env->NewObject(
58 locale_class.obj(), constructor_id,
59 ConvertUTF8ToJavaString(env, language).obj(),
60 ConvertUTF8ToJavaString(env, country).obj(),
61 ConvertUTF8ToJavaString(env, variant).obj()));
jungshik at Google 2012/05/04 17:46:03 Does "Java on Android" support Java 7 locale class
wangxianzhu 2012/05/04 18:13:42 Thanks for the information. They are very useful.
62 }
63
64 } // namespace
65
66 string16 GetDisplayNameForLocale(const std::string& locale,
67 const std::string& display_locale) {
68 JNIEnv* env = AttachCurrentThread();
69
70 ScopedJavaLocalRef<jclass> locale_class = GetClass(env, "java/util/Locale");
71 jmethodID constructor_id = GetMethodID(
72 env, locale_class, "<init>",
73 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
74 ScopedJavaLocalRef<jobject> java_locale = NewJavaLocale(
75 env, locale_class, constructor_id, locale);
76 ScopedJavaLocalRef<jobject> java_display_locale = NewJavaLocale(
77 env, locale_class, constructor_id, display_locale);
78
79 jmethodID method_id = GetMethodID(env, locale_class, "getDisplayName",
80 "(Ljava/util/Locale;)Ljava/lang/String;");
81 ScopedJavaLocalRef<jstring> java_result(
82 env,
83 static_cast<jstring>(env->CallObjectMethod(java_locale.obj(), method_id,
84 java_display_locale.obj())));
85 return ConvertJavaStringToUTF16(java_result);
86 }
87
88 bool RegisterLocaleUtils(JNIEnv* env) {
89 return RegisterNativesImpl(env);
90 }
91
92 } // namespace android
93 } // namespace base
OLDNEW
« no previous file with comments | « base/android/locale_utils.h ('k') | base/base.gyp » ('j') | ui/base/l10n/l10n_util.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698