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.
5 #include "ui/base/l10n/l10n_util_android.h"
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"
20 jint
GetFirstStrongCharacterDirection(JNIEnv
* env
, jclass clazz
,
22 return base::i18n::GetFirstStrongCharacterDirection(
23 base::android::ConvertJavaStringToUTF16(env
, string
));
26 std::string
GetDefaultLocale() {
27 JNIEnv
* env
= base::android::AttachCurrentThread();
28 ScopedJavaLocalRef
<jstring
> locale
= Java_LocalizationUtils_getDefaultLocale(
30 return ConvertJavaStringToUTF8(locale
);
34 static bool is_layout_rtl_cached
= false;
35 static bool layout_rtl_cache
;
37 if (!is_layout_rtl_cached
) {
38 is_layout_rtl_cached
= true;
39 JNIEnv
* env
= base::android::AttachCurrentThread();
41 static_cast<bool>(Java_LocalizationUtils_isLayoutRtl(env
));
44 return layout_rtl_cache
;
49 // Common prototype of ICU uloc_getXXX() functions.
50 typedef int32_t (*UlocGetComponentFunc
)(const char*, char*, int32_t,
53 std::string
GetLocaleComponent(const std::string
& locale
,
54 UlocGetComponentFunc uloc_func
,
55 int32_t max_capacity
) {
57 UErrorCode error
= U_ZERO_ERROR
;
58 int32_t actual_length
= uloc_func(locale
.c_str(),
59 WriteInto(&result
, max_capacity
),
62 DCHECK(U_SUCCESS(error
));
63 DCHECK(actual_length
< max_capacity
);
64 result
.resize(actual_length
);
68 ScopedJavaLocalRef
<jobject
> NewJavaLocale(
70 const std::string
& locale
) {
71 // TODO(wangxianzhu): Use new Locale API once Android supports scripts.
72 std::string language
= GetLocaleComponent(
73 locale
, uloc_getLanguage
, ULOC_LANG_CAPACITY
);
74 std::string country
= GetLocaleComponent(
75 locale
, uloc_getCountry
, ULOC_COUNTRY_CAPACITY
);
76 std::string variant
= GetLocaleComponent(
77 locale
, uloc_getVariant
, ULOC_FULLNAME_CAPACITY
);
78 return Java_LocalizationUtils_getJavaLocale(env
,
79 base::android::ConvertUTF8ToJavaString(env
, language
).obj(),
80 base::android::ConvertUTF8ToJavaString(env
, country
).obj(),
81 base::android::ConvertUTF8ToJavaString(env
, variant
).obj());
86 base::string16
GetDisplayNameForLocale(const std::string
& locale
,
87 const std::string
& display_locale
) {
88 JNIEnv
* env
= base::android::AttachCurrentThread();
89 ScopedJavaLocalRef
<jobject
> java_locale
=
90 NewJavaLocale(env
, locale
);
91 ScopedJavaLocalRef
<jobject
> java_display_locale
=
92 NewJavaLocale(env
, display_locale
);
94 ScopedJavaLocalRef
<jstring
> java_result(
95 Java_LocalizationUtils_getDisplayNameForLocale(
98 java_display_locale
.obj()));
99 return ConvertJavaStringToUTF16(java_result
);
102 jstring
GetDurationString(JNIEnv
* env
, jclass clazz
, jlong timeInMillis
) {
103 ScopedJavaLocalRef
<jstring
> jtime_remaining
=
104 base::android::ConvertUTF16ToJavaString(
106 ui::TimeFormat::Simple(
107 ui::TimeFormat::FORMAT_REMAINING
, ui::TimeFormat::LENGTH_SHORT
,
108 base::TimeDelta::FromMilliseconds(timeInMillis
)));
109 return jtime_remaining
.Release();
112 bool RegisterLocalizationUtil(JNIEnv
* env
) {
113 return RegisterNativesImpl(env
);
116 } // namespace l10n_util