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 libcore.icu.ICU; 22 23 /** 24 * Various utilities for Locales 25 * 26 * @hide 27 */ 28 public class LocaleUtil { 29 LocaleUtil()30 private LocaleUtil() { /* cannot be instantiated */ } 31 32 /** 33 * @hide Do not use. Implementation not finished. 34 */ 35 public static final int TEXT_LAYOUT_DIRECTION_LTR_DO_NOT_USE = 0; 36 37 /** 38 * @hide Do not use. Implementation not finished. 39 */ 40 public static final int TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE = 1; 41 42 private static final char UNDERSCORE_CHAR = '_'; 43 44 private static String ARAB_SCRIPT_SUBTAG = "Arab"; 45 private static String HEBR_SCRIPT_SUBTAG = "Hebr"; 46 47 /** 48 * Return the layout direction for a given Locale 49 * 50 * @param locale the Locale for which we want the layout direction. Can be null. 51 * @return the layout direction. This may be one of: 52 * {@link #TEXT_LAYOUT_DIRECTION_LTR_DO_NOT_USE} or 53 * {@link #TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE}. 54 * 55 * Be careful: this code will need to be changed when vertical scripts will be supported 56 * 57 * @hide 58 */ getLayoutDirectionFromLocale(Locale locale)59 public static int getLayoutDirectionFromLocale(Locale locale) { 60 if (locale != null && !locale.equals(Locale.ROOT)) { 61 final String scriptSubtag = ICU.getScript(ICU.addLikelySubtags(locale.toString())); 62 if (scriptSubtag == null) return getLayoutDirectionFromFirstChar(locale); 63 64 if (scriptSubtag.equalsIgnoreCase(ARAB_SCRIPT_SUBTAG) || 65 scriptSubtag.equalsIgnoreCase(HEBR_SCRIPT_SUBTAG)) { 66 return TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE; 67 } 68 } 69 70 return TEXT_LAYOUT_DIRECTION_LTR_DO_NOT_USE; 71 } 72 73 /** 74 * Fallback algorithm to detect the locale direction. Rely on the fist char of the 75 * localized locale name. This will not work if the localized locale name is in English 76 * (this is the case for ICU 4.4 and "Urdu" script) 77 * 78 * @param locale 79 * @return the layout direction. This may be one of: 80 * {@link #TEXT_LAYOUT_DIRECTION_LTR_DO_NOT_USE} or 81 * {@link #TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE}. 82 * 83 * Be careful: this code will need to be changed when vertical scripts will be supported 84 * 85 * @hide 86 */ getLayoutDirectionFromFirstChar(Locale locale)87 private static int getLayoutDirectionFromFirstChar(Locale locale) { 88 switch(Character.getDirectionality(locale.getDisplayName(locale).charAt(0))) { 89 case Character.DIRECTIONALITY_RIGHT_TO_LEFT: 90 case Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC: 91 return TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE; 92 93 case Character.DIRECTIONALITY_LEFT_TO_RIGHT: 94 default: 95 return TEXT_LAYOUT_DIRECTION_LTR_DO_NOT_USE; 96 } 97 } 98 } 99