• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 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 package org.chromium.ui.base;
6 
7 import android.content.res.Configuration;
8 import android.view.View;
9 
10 import org.chromium.base.ApiCompatibilityUtils;
11 import org.chromium.base.ApplicationStatus;
12 import org.chromium.base.CalledByNative;
13 import org.chromium.base.JNINamespace;
14 
15 import java.util.Locale;
16 
17 /**
18  * This class provides the locale related methods for the native library.
19  */
20 @JNINamespace("l10n_util")
21 public class LocalizationUtils {
22 
23     // This is mirrored from base/i18n/rtl.h. Please keep in sync.
24     public static final int UNKNOWN_DIRECTION = 0;
25     public static final int RIGHT_TO_LEFT = 1;
26     public static final int LEFT_TO_RIGHT = 2;
27 
28     private static Boolean sIsLayoutRtl;
29 
LocalizationUtils()30     private LocalizationUtils() { /* cannot be instantiated */ }
31 
32     /**
33      * @return the default locale, translating Android deprecated
34      * language codes into the modern ones used by Chromium.
35      */
36     @CalledByNative
getDefaultLocale()37     public static String getDefaultLocale() {
38         Locale locale = Locale.getDefault();
39         String language = locale.getLanguage();
40         String country = locale.getCountry();
41 
42         // Android uses deprecated lanuages codes for Hebrew and Indonesian but Chromium uses the
43         // updated codes. Also, Android uses "tl" while Chromium uses "fil" for Tagalog/Filipino.
44         // So apply a mapping.
45         // See http://developer.android.com/reference/java/util/Locale.html
46         if ("iw".equals(language)) {
47             language = "he";
48         } else if ("in".equals(language)) {
49             language = "id";
50         } else if ("tl".equals(language)) {
51             language = "fil";
52         }
53         return country.isEmpty() ? language : language + "-" + country;
54     }
55 
56     @CalledByNative
getJavaLocale(String language, String country, String variant)57     private static Locale getJavaLocale(String language, String country, String variant) {
58         return new Locale(language, country, variant);
59     }
60 
61     @CalledByNative
getDisplayNameForLocale(Locale locale, Locale displayLocale)62     private static String getDisplayNameForLocale(Locale locale, Locale displayLocale) {
63         return locale.getDisplayName(displayLocale);
64     }
65 
66     /**
67      * Returns whether the Android layout direction is RTL.
68      *
69      * Note that the locale direction can be different from layout direction. Two known cases:
70      * - RTL languages on Android 4.1, due to the lack of RTL layout support on 4.1.
71      * - When user turned on force RTL layout option under developer options.
72      *
73      * Therefore, only this function should be used to query RTL for layout purposes.
74      */
75     @CalledByNative
isLayoutRtl()76     public static boolean isLayoutRtl() {
77         if (sIsLayoutRtl == null) {
78             Configuration configuration =
79                     ApplicationStatus.getApplicationContext().getResources().getConfiguration();
80             sIsLayoutRtl = Boolean.valueOf(
81                     ApiCompatibilityUtils.getLayoutDirection(configuration) ==
82                     View.LAYOUT_DIRECTION_RTL);
83         }
84 
85         return sIsLayoutRtl.booleanValue();
86     }
87 
88     /**
89      * Jni binding to base::i18n::GetFirstStrongCharacterDirection
90      * @param string String to decide the direction.
91      * @return One of the UNKNOWN_DIRECTION, RIGHT_TO_LEFT, and LEFT_TO_RIGHT.
92      */
getFirstStrongCharacterDirection(String string)93     public static int getFirstStrongCharacterDirection(String string) {
94         return nativeGetFirstStrongCharacterDirection(string);
95     }
96 
97     /**
98      * Jni binding to ui::TimeFormat::TimeRemaining. Converts milliseconds to
99      * time remaining format : "3 mins left", "2 days left".
100      * @param timeInMillis time in milliseconds
101      * @return time remaining
102      */
getDurationString(long timeInMillis)103     public static String getDurationString(long timeInMillis) {
104         return nativeGetDurationString(timeInMillis);
105     }
106 
nativeGetFirstStrongCharacterDirection(String string)107     private static native int nativeGetFirstStrongCharacterDirection(String string);
108 
nativeGetDurationString(long timeInMillis)109     private static native String nativeGetDurationString(long timeInMillis);
110 }
111