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

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

Issue 10380018: Revert 135484 - Use Android API for GetDisplayNameForLocale(). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: 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
« no previous file with comments | « base/android/locale_utils.h ('k') | base/base.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 // TODO(wangxianzhu): Use new Locale API once Android supports scripts.
51 std::string language = GetLocaleComponent(
52 locale, uloc_getLanguage, ULOC_LANG_CAPACITY);
53 std::string country = GetLocaleComponent(
54 locale, uloc_getCountry, ULOC_COUNTRY_CAPACITY);
55 std::string variant = GetLocaleComponent(
56 locale, uloc_getVariant, ULOC_FULLNAME_CAPACITY);
57 return ScopedJavaLocalRef<jobject>(
58 env, env->NewObject(
59 locale_class.obj(), constructor_id,
60 ConvertUTF8ToJavaString(env, language).obj(),
61 ConvertUTF8ToJavaString(env, country).obj(),
62 ConvertUTF8ToJavaString(env, variant).obj()));
63 }
64
65 } // namespace
66
67 string16 GetDisplayNameForLocale(const std::string& locale,
68 const std::string& display_locale) {
69 JNIEnv* env = AttachCurrentThread();
70
71 ScopedJavaLocalRef<jclass> locale_class = GetClass(env, "java/util/Locale");
72 jmethodID constructor_id = GetMethodID(
73 env, locale_class, "<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 = GetMethodID(env, locale_class, "getDisplayName",
81 "(Ljava/util/Locale;)Ljava/lang/String;");
82 ScopedJavaLocalRef<jstring> java_result(
83 env,
84 static_cast<jstring>(env->CallObjectMethod(java_locale.obj(), method_id,
85 java_display_locale.obj())));
86 return ConvertJavaStringToUTF16(java_result);
87 }
88
89 bool RegisterLocaleUtils(JNIEnv* env) {
90 return RegisterNativesImpl(env);
91 }
92
93 } // namespace android
94 } // namespace base
OLDNEW
« no previous file with comments | « base/android/locale_utils.h ('k') | base/base.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698