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 "ui/base/l10n/l10n_util_android.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/i18n/rtl.h" 11 #include "base/logging.h" 12 #include "base/strings/string_util.h" 13 #include "base/time/time.h" 14 #include "jni/LocalizationUtils_jni.h" 15 #include "third_party/icu/source/common/unicode/uloc.h" 16 #include "ui/base/l10n/time_format.h" 17 18 namespace l10n_util { 19 20 jboolean IsRTL(JNIEnv* env, jclass clazz) { 21 return base::i18n::IsRTL(); 22 } 23 24 jint GetFirstStrongCharacterDirection(JNIEnv* env, jclass clazz, 25 jstring string) { 26 return base::i18n::GetFirstStrongCharacterDirection( 27 base::android::ConvertJavaStringToUTF16(env, string)); 28 } 29 30 std::string GetDefaultLocale() { 31 JNIEnv* env = base::android::AttachCurrentThread(); 32 ScopedJavaLocalRef<jstring> locale = Java_LocalizationUtils_getDefaultLocale( 33 env); 34 return ConvertJavaStringToUTF8(locale); 35 } 36 37 namespace { 38 39 // Common prototype of ICU uloc_getXXX() functions. 40 typedef int32_t (*UlocGetComponentFunc)(const char*, char*, int32_t, 41 UErrorCode*); 42 43 std::string GetLocaleComponent(const std::string& locale, 44 UlocGetComponentFunc uloc_func, 45 int32_t max_capacity) { 46 std::string result; 47 UErrorCode error = U_ZERO_ERROR; 48 int32_t actual_length = uloc_func(locale.c_str(), 49 WriteInto(&result, max_capacity), 50 max_capacity, 51 &error); 52 DCHECK(U_SUCCESS(error)); 53 DCHECK(actual_length < max_capacity); 54 result.resize(actual_length); 55 return result; 56 } 57 58 ScopedJavaLocalRef<jobject> NewJavaLocale( 59 JNIEnv* env, 60 const std::string& locale) { 61 // TODO(wangxianzhu): Use new Locale API once Android supports scripts. 62 std::string language = GetLocaleComponent( 63 locale, uloc_getLanguage, ULOC_LANG_CAPACITY); 64 std::string country = GetLocaleComponent( 65 locale, uloc_getCountry, ULOC_COUNTRY_CAPACITY); 66 std::string variant = GetLocaleComponent( 67 locale, uloc_getVariant, ULOC_FULLNAME_CAPACITY); 68 return Java_LocalizationUtils_getJavaLocale(env, 69 base::android::ConvertUTF8ToJavaString(env, language).obj(), 70 base::android::ConvertUTF8ToJavaString(env, country).obj(), 71 base::android::ConvertUTF8ToJavaString(env, variant).obj()); 72 } 73 74 } // namespace 75 76 string16 GetDisplayNameForLocale(const std::string& locale, 77 const std::string& display_locale) { 78 JNIEnv* env = base::android::AttachCurrentThread(); 79 ScopedJavaLocalRef<jobject> java_locale = 80 NewJavaLocale(env, locale); 81 ScopedJavaLocalRef<jobject> java_display_locale = 82 NewJavaLocale(env, display_locale); 83 84 ScopedJavaLocalRef<jstring> java_result( 85 Java_LocalizationUtils_getDisplayNameForLocale( 86 env, 87 java_locale.obj(), 88 java_display_locale.obj())); 89 return ConvertJavaStringToUTF16(java_result); 90 } 91 92 jstring GetDurationString(JNIEnv* env, jclass clazz, jlong timeInMillis) { 93 ScopedJavaLocalRef<jstring> jtime_remaining = 94 base::android::ConvertUTF16ToJavaString( 95 env, 96 ui::TimeFormat::TimeRemaining( 97 base::TimeDelta::FromMilliseconds(timeInMillis))); 98 return jtime_remaining.Release(); 99 } 100 101 bool RegisterLocalizationUtil(JNIEnv* env) { 102 return RegisterNativesImpl(env); 103 } 104 105 } // namespace l10n_util 106