Mihai Nita | 1808ff7 | 2016-01-12 08:53:54 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 com.android.internal.app; |
| 18 | |
Mihai Nita | fc881a2 | 2016-01-28 09:38:17 -0800 | [diff] [blame] | 19 | import android.icu.text.ListFormatter; |
Mihai Nita | 502141d | 2016-02-22 16:40:26 -0800 | [diff] [blame] | 20 | import android.icu.util.ULocale; |
Mihai Nita | 1808ff7 | 2016-01-12 08:53:54 -0800 | [diff] [blame] | 21 | import android.util.LocaleList; |
| 22 | |
| 23 | import java.text.Collator; |
| 24 | import java.util.Comparator; |
| 25 | import java.util.Locale; |
| 26 | |
| 27 | /** |
Mihai Nita | 137b96e | 2016-01-25 11:31:15 -0800 | [diff] [blame] | 28 | * This class implements some handy methods to process with locales. |
Mihai Nita | 1808ff7 | 2016-01-12 08:53:54 -0800 | [diff] [blame] | 29 | */ |
| 30 | public class LocaleHelper { |
| 31 | |
| 32 | /** |
| 33 | * Sentence-case (first character uppercased). |
| 34 | * |
| 35 | * <p>There is no good API available for this, not even in ICU. |
| 36 | * We can revisit this if we get some ICU support later.</p> |
| 37 | * |
| 38 | * <p>There are currently several tickets requesting this feature:</p> |
| 39 | * <ul> |
| 40 | * <li>ICU needs to provide an easy way to titlecase only one first letter |
| 41 | * http://bugs.icu-project.org/trac/ticket/11729</li> |
| 42 | * <li>Add "initial case" |
| 43 | * http://bugs.icu-project.org/trac/ticket/8394</li> |
| 44 | * <li>Add code for initialCase, toTitlecase don't modify after Lt, |
| 45 | * avoid 49Ers, low-level language-specific casing |
| 46 | * http://bugs.icu-project.org/trac/ticket/10410</li> |
| 47 | * <li>BreakIterator.getFirstInstance: Often you need to titlecase just the first |
| 48 | * word, and leave the rest of the string alone. (closed as duplicate) |
| 49 | * http://bugs.icu-project.org/trac/ticket/8946</li> |
| 50 | * </ul> |
| 51 | * |
| 52 | * <p>A (clunky) option with the current ICU API is:</p> |
| 53 | * {{ |
| 54 | * BreakIterator breakIterator = BreakIterator.getSentenceInstance(locale); |
| 55 | * String result = UCharacter.toTitleCase(locale, |
| 56 | * source, breakIterator, UCharacter.TITLECASE_NO_LOWERCASE); |
| 57 | * }} |
| 58 | * |
Mihai Nita | 137b96e | 2016-01-25 11:31:15 -0800 | [diff] [blame] | 59 | * <p>That also means creating a BreakIterator for each locale. Expensive...</p> |
Mihai Nita | 1808ff7 | 2016-01-12 08:53:54 -0800 | [diff] [blame] | 60 | * |
| 61 | * @param str the string to sentence-case. |
| 62 | * @param locale the locale used for the case conversion. |
| 63 | * @return the string converted to sentence-case. |
| 64 | */ |
| 65 | public static String toSentenceCase(String str, Locale locale) { |
| 66 | if (str.isEmpty()) { |
| 67 | return str; |
| 68 | } |
| 69 | final int firstCodePointLen = str.offsetByCodePoints(0, 1); |
| 70 | return str.substring(0, firstCodePointLen).toUpperCase(locale) |
| 71 | + str.substring(firstCodePointLen); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Normalizes a string for locale name search. Does case conversion for now, |
| 76 | * but might do more in the future. |
| 77 | * |
| 78 | * <p>Warning: it is only intended to be used in searches by the locale picker. |
| 79 | * Don't use it for other things, it is very limited.</p> |
| 80 | * |
| 81 | * @param str the string to normalize |
| 82 | * @param locale the locale that might be used for certain operations (i.e. case conversion) |
| 83 | * @return the string normalized for search |
| 84 | */ |
| 85 | public static String normalizeForSearch(String str, Locale locale) { |
| 86 | // TODO: tbd if it needs to be smarter (real normalization, remove accents, etc.) |
| 87 | // If needed we might use case folding and ICU/CLDR's collation-based loose searching. |
| 88 | // TODO: decide what should the locale be, the default locale, or the locale of the string. |
| 89 | // Uppercase is better than lowercase because of things like sharp S, Greek sigma, ... |
| 90 | return str.toUpperCase(); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Returns the locale localized for display in the provided locale. |
| 95 | * |
| 96 | * @param locale the locale whose name is to be displayed. |
| 97 | * @param displayLocale the locale in which to display the name. |
| 98 | * @param sentenceCase true if the result should be sentence-cased |
| 99 | * @return the localized name of the locale. |
| 100 | */ |
| 101 | public static String getDisplayName(Locale locale, Locale displayLocale, boolean sentenceCase) { |
Mihai Nita | 502141d | 2016-02-22 16:40:26 -0800 | [diff] [blame] | 102 | String result = ULocale.getDisplayNameWithDialect(locale.toLanguageTag(), |
Mihai Nita | 1808ff7 | 2016-01-12 08:53:54 -0800 | [diff] [blame] | 103 | ULocale.forLocale(displayLocale)); |
| 104 | return sentenceCase ? toSentenceCase(result, displayLocale) : result; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Returns the locale localized for display in the default locale. |
| 109 | * |
| 110 | * @param locale the locale whose name is to be displayed. |
| 111 | * @param sentenceCase true if the result should be sentence-cased |
| 112 | * @return the localized name of the locale. |
| 113 | */ |
| 114 | public static String getDisplayName(Locale locale, boolean sentenceCase) { |
Mihai Nita | 502141d | 2016-02-22 16:40:26 -0800 | [diff] [blame] | 115 | String result = ULocale.getDisplayNameWithDialect(locale.toLanguageTag(), |
| 116 | ULocale.getDefault()); |
Mihai Nita | 1808ff7 | 2016-01-12 08:53:54 -0800 | [diff] [blame] | 117 | return sentenceCase ? toSentenceCase(result, Locale.getDefault()) : result; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Returns a locale's country localized for display in the provided locale. |
| 122 | * |
| 123 | * @param locale the locale whose country will be displayed. |
| 124 | * @param displayLocale the locale in which to display the name. |
| 125 | * @return the localized country name. |
| 126 | */ |
| 127 | public static String getDisplayCountry(Locale locale, Locale displayLocale) { |
| 128 | return ULocale.getDisplayCountry(locale.toLanguageTag(), ULocale.forLocale(displayLocale)); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Returns a locale's country localized for display in the default locale. |
| 133 | * |
| 134 | * @param locale the locale whose country will be displayed. |
| 135 | * @return the localized country name. |
| 136 | */ |
| 137 | public static String getDisplayCountry(Locale locale) { |
| 138 | return ULocale.getDisplayCountry(locale.toLanguageTag(), ULocale.getDefault()); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Returns the locale list localized for display in the provided locale. |
| 143 | * |
| 144 | * @param locales the list of locales whose names is to be displayed. |
| 145 | * @param displayLocale the locale in which to display the names. |
| 146 | * If this is null, it will use the default locale. |
| 147 | * @return the locale aware list of locale names |
| 148 | */ |
| 149 | public static String getDisplayLocaleList(LocaleList locales, Locale displayLocale) { |
Mihai Nita | 1808ff7 | 2016-01-12 08:53:54 -0800 | [diff] [blame] | 150 | final Locale dispLocale = displayLocale == null ? Locale.getDefault() : displayLocale; |
Mihai Nita | fc881a2 | 2016-01-28 09:38:17 -0800 | [diff] [blame] | 151 | |
Mihai Nita | 1808ff7 | 2016-01-12 08:53:54 -0800 | [diff] [blame] | 152 | int localeCount = locales.size(); |
Mihai Nita | fc881a2 | 2016-01-28 09:38:17 -0800 | [diff] [blame] | 153 | final String[] localeNames = new String[localeCount]; |
Mihai Nita | 1808ff7 | 2016-01-12 08:53:54 -0800 | [diff] [blame] | 154 | for (int i = 0; i < localeCount; i++) { |
Mihai Nita | fc881a2 | 2016-01-28 09:38:17 -0800 | [diff] [blame] | 155 | localeNames[i] = LocaleHelper.getDisplayName(locales.get(i), dispLocale, false); |
Mihai Nita | 1808ff7 | 2016-01-12 08:53:54 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Mihai Nita | fc881a2 | 2016-01-28 09:38:17 -0800 | [diff] [blame] | 158 | ListFormatter lfn = ListFormatter.getInstance(dispLocale); |
Mihai Nita | 502141d | 2016-02-22 16:40:26 -0800 | [diff] [blame] | 159 | return lfn.format((Object[]) localeNames); |
Mihai Nita | 1808ff7 | 2016-01-12 08:53:54 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Adds the likely subtags for a provided locale ID. |
| 164 | * |
| 165 | * @param locale the locale to maximize. |
| 166 | * @return the maximized Locale instance. |
| 167 | */ |
| 168 | public static Locale addLikelySubtags(Locale locale) { |
| 169 | return libcore.icu.ICU.addLikelySubtags(locale); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Locale-sensitive comparison for LocaleInfo. |
| 174 | * |
| 175 | * <p>It uses the label, leaving the decision on what to put there to the LocaleInfo. |
| 176 | * For instance fr-CA can be shown as "français" as a generic label in the language selection, |
| 177 | * or "français (Canada)" if it is a suggestion, or "Canada" in the country selection.</p> |
| 178 | * |
| 179 | * <p>Gives priority to suggested locales (to sort them at the top).</p> |
| 180 | */ |
Mihai Nita | 137b96e | 2016-01-25 11:31:15 -0800 | [diff] [blame] | 181 | public static final class LocaleInfoComparator implements Comparator<LocaleStore.LocaleInfo> { |
Mihai Nita | 1808ff7 | 2016-01-12 08:53:54 -0800 | [diff] [blame] | 182 | private final Collator mCollator; |
Mihai Nita | f1f39cf | 2016-02-29 14:42:12 -0800 | [diff] [blame] | 183 | private final boolean mCountryMode; |
Mihai Nita | 1808ff7 | 2016-01-12 08:53:54 -0800 | [diff] [blame] | 184 | |
| 185 | /** |
| 186 | * Constructor. |
| 187 | * |
| 188 | * @param sortLocale the locale to be used for sorting. |
| 189 | */ |
Mihai Nita | f1f39cf | 2016-02-29 14:42:12 -0800 | [diff] [blame] | 190 | public LocaleInfoComparator(Locale sortLocale, boolean countryMode) { |
Mihai Nita | 1808ff7 | 2016-01-12 08:53:54 -0800 | [diff] [blame] | 191 | mCollator = Collator.getInstance(sortLocale); |
Mihai Nita | f1f39cf | 2016-02-29 14:42:12 -0800 | [diff] [blame] | 192 | mCountryMode = countryMode; |
Mihai Nita | 1808ff7 | 2016-01-12 08:53:54 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Compares its two arguments for order. |
| 197 | * |
| 198 | * @param lhs the first object to be compared |
| 199 | * @param rhs the second object to be compared |
| 200 | * @return a negative integer, zero, or a positive integer as the first |
| 201 | * argument is less than, equal to, or greater than the second. |
| 202 | */ |
| 203 | @Override |
| 204 | public int compare(LocaleStore.LocaleInfo lhs, LocaleStore.LocaleInfo rhs) { |
| 205 | // We don't care about the various suggestion types, just "suggested" (!= 0) |
| 206 | // and "all others" (== 0) |
Mihai Nita | f1f39cf | 2016-02-29 14:42:12 -0800 | [diff] [blame] | 207 | if (mCountryMode || (lhs.isSuggested() == rhs.isSuggested())) { |
Mihai Nita | 1808ff7 | 2016-01-12 08:53:54 -0800 | [diff] [blame] | 208 | // They are in the same "bucket" (suggested / others), so we compare the text |
Mihai Nita | f1f39cf | 2016-02-29 14:42:12 -0800 | [diff] [blame] | 209 | return mCollator.compare(lhs.getLabel(mCountryMode), rhs.getLabel(mCountryMode)); |
Mihai Nita | 1808ff7 | 2016-01-12 08:53:54 -0800 | [diff] [blame] | 210 | } else { |
| 211 | // One locale is suggested and one is not, so we put them in different "buckets" |
| 212 | return lhs.isSuggested() ? -1 : 1; |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | } |