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
,
21 const JavaParamRef
<jclass
>& clazz
,
22 const JavaParamRef
<jstring
>& string
) {
23 return base::i18n::GetFirstStrongCharacterDirection(
24 base::android::ConvertJavaStringToUTF16(env
, string
));
28 static bool is_layout_rtl_cached
= false;
29 static bool layout_rtl_cache
;
31 if (!is_layout_rtl_cached
) {
32 is_layout_rtl_cached
= true;
33 JNIEnv
* env
= base::android::AttachCurrentThread();
35 static_cast<bool>(Java_LocalizationUtils_isLayoutRtl(env
));
38 return layout_rtl_cache
;
43 // Common prototype of ICU uloc_getXXX() functions.
44 typedef int32_t (*UlocGetComponentFunc
)(const char*, char*, int32_t,
47 std::string
GetLocaleComponent(const std::string
& locale
,
48 UlocGetComponentFunc uloc_func
,
49 int32_t max_capacity
) {
51 UErrorCode error
= U_ZERO_ERROR
;
52 int32_t actual_length
= uloc_func(locale
.c_str(),
53 base::WriteInto(&result
, max_capacity
),
56 DCHECK(U_SUCCESS(error
));
57 DCHECK(actual_length
< max_capacity
);
58 result
.resize(actual_length
);
62 ScopedJavaLocalRef
<jobject
> NewJavaLocale(
64 const std::string
& locale
) {
65 // TODO(wangxianzhu): Use new Locale API once Android supports scripts.
66 std::string language
= GetLocaleComponent(
67 locale
, uloc_getLanguage
, ULOC_LANG_CAPACITY
);
68 std::string country
= GetLocaleComponent(
69 locale
, uloc_getCountry
, ULOC_COUNTRY_CAPACITY
);
70 std::string variant
= GetLocaleComponent(
71 locale
, uloc_getVariant
, ULOC_FULLNAME_CAPACITY
);
72 return Java_LocalizationUtils_getJavaLocale(env
,
73 base::android::ConvertUTF8ToJavaString(env
, language
).obj(),
74 base::android::ConvertUTF8ToJavaString(env
, country
).obj(),
75 base::android::ConvertUTF8ToJavaString(env
, variant
).obj());
80 base::string16
GetDisplayNameForLocale(const std::string
& locale
,
81 const std::string
& display_locale
) {
82 JNIEnv
* env
= base::android::AttachCurrentThread();
83 ScopedJavaLocalRef
<jobject
> java_locale
=
84 NewJavaLocale(env
, locale
);
85 ScopedJavaLocalRef
<jobject
> java_display_locale
=
86 NewJavaLocale(env
, display_locale
);
88 ScopedJavaLocalRef
<jstring
> java_result(
89 Java_LocalizationUtils_getDisplayNameForLocale(
92 java_display_locale
.obj()));
93 return ConvertJavaStringToUTF16(java_result
);
96 ScopedJavaLocalRef
<jstring
> GetDurationString(JNIEnv
* env
,
97 const JavaParamRef
<jclass
>& clazz
,
99 ScopedJavaLocalRef
<jstring
> jtime_remaining
=
100 base::android::ConvertUTF16ToJavaString(
102 ui::TimeFormat::Simple(
103 ui::TimeFormat::FORMAT_REMAINING
, ui::TimeFormat::LENGTH_SHORT
,
104 base::TimeDelta::FromMilliseconds(timeInMillis
)));
105 return jtime_remaining
;
108 bool RegisterLocalizationUtil(JNIEnv
* env
) {
109 return RegisterNativesImpl(env
);
112 } // namespace l10n_util