1a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio/* 2a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio * Copyright (C) 2011 The Android Open Source Project 3a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio * 4a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio * Licensed under the Apache License, Version 2.0 (the "License"); 5a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio * you may not use this file except in compliance with the License. 6a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio * You may obtain a copy of the License at 7a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio * 8a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio * http://www.apache.org/licenses/LICENSE-2.0 9a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio * 10a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio * Unless required by applicable law or agreed to in writing, software 11a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio * distributed under the License is distributed on an "AS IS" BASIS, 12a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio * See the License for the specific language governing permissions and 14a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio * limitations under the License. 15a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio */ 16a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio 17a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Megliopackage android.util; 18a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio 19a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglioimport java.util.Locale; 20a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio 21a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglioimport libcore.icu.ICU; 22a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio 23a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio/** 24a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio * Various utilities for Locales 25a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio * 26a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio * @hide 27a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio */ 28a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Megliopublic class LocaleUtil { 29a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio 30a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio private LocaleUtil() { /* cannot be instantiated */ } 31a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio 32a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio /** 33a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio * @hide Do not use. Implementation not finished. 34a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio */ 35a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio public static final int TEXT_LAYOUT_DIRECTION_LTR_DO_NOT_USE = 0; 36a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio 37a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio /** 38a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio * @hide Do not use. Implementation not finished. 39a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio */ 40a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio public static final int TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE = 1; 41a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio 42a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio private static final char UNDERSCORE_CHAR = '_'; 43a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio 44a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio private static String ARAB_SCRIPT_SUBTAG = "Arab"; 45a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio private static String HEBR_SCRIPT_SUBTAG = "Hebr"; 46a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio 47a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio /** 48a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio * Return the layout direction for a given Locale 49a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio * 50a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio * @param locale the Locale for which we want the layout direction. Can be null. 51a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio * @return the layout direction. This may be one of: 52a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio * {@link #TEXT_LAYOUT_DIRECTION_LTR_DO_NOT_USE} or 53a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio * {@link #TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE}. 54a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio * 55a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio * Be careful: this code will need to be changed when vertical scripts will be supported 56a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio * 57a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio * @hide 58a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio */ 59a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio public static int getLayoutDirectionFromLocale(Locale locale) { 607810b5f8cffb3c2c98fd0df579f4da5a9ac6cc73Fabrice Di Meglio if (locale != null && !locale.equals(Locale.ROOT)) { 617810b5f8cffb3c2c98fd0df579f4da5a9ac6cc73Fabrice Di Meglio final String scriptSubtag = ICU.getScript(ICU.addLikelySubtags(locale.toString())); 627810b5f8cffb3c2c98fd0df579f4da5a9ac6cc73Fabrice Di Meglio if (scriptSubtag == null) return getLayoutDirectionFromFirstChar(locale); 63a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio 647810b5f8cffb3c2c98fd0df579f4da5a9ac6cc73Fabrice Di Meglio if (scriptSubtag.equalsIgnoreCase(ARAB_SCRIPT_SUBTAG) || 657810b5f8cffb3c2c98fd0df579f4da5a9ac6cc73Fabrice Di Meglio scriptSubtag.equalsIgnoreCase(HEBR_SCRIPT_SUBTAG)) { 667810b5f8cffb3c2c98fd0df579f4da5a9ac6cc73Fabrice Di Meglio return TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE; 677810b5f8cffb3c2c98fd0df579f4da5a9ac6cc73Fabrice Di Meglio } 68a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio } 697810b5f8cffb3c2c98fd0df579f4da5a9ac6cc73Fabrice Di Meglio 70a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio return TEXT_LAYOUT_DIRECTION_LTR_DO_NOT_USE; 71a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio } 72a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio 73a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio /** 74a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio * Fallback algorithm to detect the locale direction. Rely on the fist char of the 75a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio * localized locale name. This will not work if the localized locale name is in English 76a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio * (this is the case for ICU 4.4 and "Urdu" script) 77a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio * 78a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio * @param locale 79a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio * @return the layout direction. This may be one of: 80a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio * {@link #TEXT_LAYOUT_DIRECTION_LTR_DO_NOT_USE} or 81a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio * {@link #TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE}. 82a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio * 83a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio * Be careful: this code will need to be changed when vertical scripts will be supported 84a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio * 85a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio * @hide 86a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio */ 87a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio private static int getLayoutDirectionFromFirstChar(Locale locale) { 88a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio switch(Character.getDirectionality(locale.getDisplayName(locale).charAt(0))) { 89a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio case Character.DIRECTIONALITY_RIGHT_TO_LEFT: 90a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio case Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC: 91a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio return TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE; 927810b5f8cffb3c2c98fd0df579f4da5a9ac6cc73Fabrice Di Meglio 937810b5f8cffb3c2c98fd0df579f4da5a9ac6cc73Fabrice Di Meglio case Character.DIRECTIONALITY_LEFT_TO_RIGHT: 94a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio default: 957810b5f8cffb3c2c98fd0df579f4da5a9ac6cc73Fabrice Di Meglio return TEXT_LAYOUT_DIRECTION_LTR_DO_NOT_USE; 96a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio } 97a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio } 98a47f45e4829f812ff47f9e5c9370b02284d92ae8Fabrice Di Meglio} 99