• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.util;
18 
19 import java.util.Locale;
20 
21 import android.view.View;
22 import libcore.icu.ICU;
23 
24 /**
25  * Various utilities for Locales
26  *
27  * @hide
28  */
29 public class LocaleUtil {
30 
LocaleUtil()31     private LocaleUtil() { /* cannot be instantiated */ }
32 
33     private static String ARAB_SCRIPT_SUBTAG = "Arab";
34     private static String HEBR_SCRIPT_SUBTAG = "Hebr";
35 
36     /**
37      * Return the layout direction for a given Locale
38      *
39      * @param locale the Locale for which we want the layout direction. Can be null.
40      * @return the layout direction. This may be one of:
41      * {@link View#LAYOUT_DIRECTION_LTR} or
42      * {@link View#LAYOUT_DIRECTION_RTL}.
43      *
44      * Warning: this code does not support vertical scripts.
45      * @hide
46      */
getLayoutDirectionFromLocale(Locale locale)47     public static int getLayoutDirectionFromLocale(Locale locale) {
48         if (locale != null && !locale.equals(Locale.ROOT)) {
49             final String scriptSubtag = ICU.getScript(ICU.addLikelySubtags(locale.toString()));
50             if (scriptSubtag == null) return getLayoutDirectionFromFirstChar(locale);
51 
52             if (scriptSubtag.equalsIgnoreCase(ARAB_SCRIPT_SUBTAG) ||
53                     scriptSubtag.equalsIgnoreCase(HEBR_SCRIPT_SUBTAG)) {
54                 return View.LAYOUT_DIRECTION_RTL;
55             }
56         }
57 
58         return View.LAYOUT_DIRECTION_LTR;
59     }
60 
61     /**
62      * Fallback algorithm to detect the locale direction. Rely on the fist char of the
63      * localized locale name. This will not work if the localized locale name is in English
64      * (this is the case for ICU 4.4 and "Urdu" script)
65      *
66      * @param locale
67      * @return the layout direction. This may be one of:
68      * {@link View#LAYOUT_DIRECTION_LTR} or
69      * {@link View#LAYOUT_DIRECTION_RTL}.
70      *
71      * Warning: this code does not support vertical scripts.
72      * @hide
73      */
getLayoutDirectionFromFirstChar(Locale locale)74     private static int getLayoutDirectionFromFirstChar(Locale locale) {
75         switch(Character.getDirectionality(locale.getDisplayName(locale).charAt(0))) {
76             case Character.DIRECTIONALITY_RIGHT_TO_LEFT:
77             case Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC:
78                 return View.LAYOUT_DIRECTION_RTL;
79 
80             case Character.DIRECTIONALITY_LEFT_TO_RIGHT:
81             default:
82                 return View.LAYOUT_DIRECTION_LTR;
83         }
84     }
85 }
86