diff options
Diffstat (limited to 'base')
-rw-r--r-- | base/android/java/org/chromium/base/LocaleUtils.java | 26 | ||||
-rw-r--r-- | base/android/locale_utils.cc | 94 | ||||
-rw-r--r-- | base/android/locale_utils.h | 29 | ||||
-rw-r--r-- | base/base.gyp | 2 | ||||
-rw-r--r-- | base/base.gypi | 3 |
5 files changed, 154 insertions, 0 deletions
diff --git a/base/android/java/org/chromium/base/LocaleUtils.java b/base/android/java/org/chromium/base/LocaleUtils.java new file mode 100644 index 0000000..6bca1f7 --- /dev/null +++ b/base/android/java/org/chromium/base/LocaleUtils.java @@ -0,0 +1,26 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +package org.chromium.base; + +import java.util.Locale; + +/** + * This class provides the locale related methods for the native library. + */ +class LocaleUtils { + + private LocaleUtils() { /* cannot be instantiated */ } + + /** + * @return the default locale. + */ + @CalledByNative + public static String getDefaultLocale() { + Locale locale = Locale.getDefault(); + String language = locale.getLanguage(); + String country = locale.getCountry(); + return country.isEmpty() ? language : language + "-" + country; + } +} diff --git a/base/android/locale_utils.cc b/base/android/locale_utils.cc new file mode 100644 index 0000000..c2a88cf --- /dev/null +++ b/base/android/locale_utils.cc @@ -0,0 +1,94 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/android/locale_utils.h" + +#include "base/android/jni_android.h" +#include "base/android/jni_string.h" +#include "base/android/scoped_java_ref.h" +#include "base/logging.h" +#include "base/string_util.h" +#include "jni/locale_utils_jni.h" +#include "unicode/uloc.h" + +namespace base { +namespace android { + +std::string GetDefaultLocale() { + JNIEnv* env = AttachCurrentThread(); + ScopedJavaLocalRef<jstring> locale = Java_LocaleUtils_getDefaultLocale(env); + return ConvertJavaStringToUTF8(locale); +} + +namespace { + +// Common prototype of ICU uloc_getXXX() functions. +typedef int32_t (*UlocGetComponentFunc)(const char*, char*, int32_t, + UErrorCode*); + +std::string GetLocaleComponent(const std::string& locale, + UlocGetComponentFunc uloc_func, + int32_t max_capacity) { + std::string result; + UErrorCode error = U_ZERO_ERROR; + int32_t actual_length = uloc_func(locale.c_str(), + WriteInto(&result, max_capacity), + max_capacity, + &error); + DCHECK(U_SUCCESS(error)); + DCHECK(actual_length < max_capacity); + result.resize(actual_length); + return result; +} + +ScopedJavaLocalRef<jobject> NewJavaLocale( + JNIEnv* env, + ScopedJavaLocalRef<jclass> locale_class, + jmethodID constructor_id, + const std::string& locale) { + // TODO(wangxianzhu): Use new Locale API once Android supports scripts. + std::string language = GetLocaleComponent( + locale, uloc_getLanguage, ULOC_LANG_CAPACITY); + std::string country = GetLocaleComponent( + locale, uloc_getCountry, ULOC_COUNTRY_CAPACITY); + std::string variant = GetLocaleComponent( + locale, uloc_getVariant, ULOC_FULLNAME_CAPACITY); + return ScopedJavaLocalRef<jobject>( + env, env->NewObject( + locale_class.obj(), constructor_id, + ConvertUTF8ToJavaString(env, language).obj(), + ConvertUTF8ToJavaString(env, country).obj(), + ConvertUTF8ToJavaString(env, variant).obj())); +} + +} // namespace + +string16 GetDisplayNameForLocale(const std::string& locale, + const std::string& display_locale) { + JNIEnv* env = AttachCurrentThread(); + + ScopedJavaLocalRef<jclass> locale_class = GetClass(env, "java/util/Locale"); + jmethodID constructor_id = GetMethodID( + env, locale_class, "<init>", + "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"); + ScopedJavaLocalRef<jobject> java_locale = NewJavaLocale( + env, locale_class, constructor_id, locale); + ScopedJavaLocalRef<jobject> java_display_locale = NewJavaLocale( + env, locale_class, constructor_id, display_locale); + + jmethodID method_id = GetMethodID(env, locale_class, "getDisplayName", + "(Ljava/util/Locale;)Ljava/lang/String;"); + ScopedJavaLocalRef<jstring> java_result( + env, + static_cast<jstring>(env->CallObjectMethod(java_locale.obj(), method_id, + java_display_locale.obj()))); + return ConvertJavaStringToUTF16(java_result); +} + +bool RegisterLocaleUtils(JNIEnv* env) { + return RegisterNativesImpl(env); +} + +} // namespace android +} // namespace base diff --git a/base/android/locale_utils.h b/base/android/locale_utils.h new file mode 100644 index 0000000..35d0015 --- /dev/null +++ b/base/android/locale_utils.h @@ -0,0 +1,29 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef BASE_ANDROID_LOCALE_UTILS_H_ +#define BASE_ANDROID_LOCALE_UTILS_H_ +#pragma once + +#include <jni.h> + +#include <string> + +#include "base/string16.h" + +namespace base { +namespace android { + +// Return the current default locale of the device. +std::string GetDefaultLocale(); + +string16 GetDisplayNameForLocale(const std::string& locale, + const std::string& display_locale); + +bool RegisterLocaleUtils(JNIEnv* env); + +} // namespace android +} // namespace base + +#endif // BASE_ANDROID_LOCALE_UTILS_H_ diff --git a/base/base.gyp b/base/base.gyp index e048d09..8de65df 100644 --- a/base/base.gyp +++ b/base/base.gyp @@ -538,11 +538,13 @@ 'variables': { 'java_sources': [ 'android/java/org/chromium/base/BuildInfo.java', + 'android/java/org/chromium/base/LocaleUtils.java', 'android/java/org/chromium/base/PathUtils.java', 'android/java/org/chromium/base/SystemMessageHandler.java', ], 'jni_headers': [ '<(SHARED_INTERMEDIATE_DIR)/base/jni/build_info_jni.h', + '<(SHARED_INTERMEDIATE_DIR)/base/jni/locale_utils_jni.h', '<(SHARED_INTERMEDIATE_DIR)/base/jni/path_utils_jni.h', '<(SHARED_INTERMEDIATE_DIR)/base/jni/system_message_handler_jni.h', ], diff --git a/base/base.gypi b/base/base.gypi index 4c96ebd..c19658f 100644 --- a/base/base.gypi +++ b/base/base.gypi @@ -39,6 +39,8 @@ 'android/jni_registrar.h', 'android/jni_string.cc', 'android/jni_string.h', + 'android/locale_utils.cc', + 'android/locale_utils.h', 'android/path_utils.cc', 'android/path_utils.h', 'at_exit.cc', @@ -696,6 +698,7 @@ 'dependencies': [ 'symbolize', '../third_party/ashmem/ashmem.gyp:ashmem', + '../third_party/icu/icu.gyp:icuuc', 'base_java', 'base_jni_headers', ], |