blob: a9d51132cdcc779f7d74ff116d8ceacd5a2a8083 [file] [log] [blame]
Mihai Nita1808ff72016-01-12 08:53:54 -08001/*
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
17package com.android.internal.app;
18
Mihai Nitafc881a22016-01-28 09:38:17 -080019import android.icu.text.ListFormatter;
Mihai Nita502141d2016-02-22 16:40:26 -080020import android.icu.util.ULocale;
Mihai Nita1808ff72016-01-12 08:53:54 -080021import android.util.LocaleList;
22
23import java.text.Collator;
24import java.util.Comparator;
25import java.util.Locale;
26
27/**
Mihai Nita137b96e2016-01-25 11:31:15 -080028 * This class implements some handy methods to process with locales.
Mihai Nita1808ff72016-01-12 08:53:54 -080029 */
30public 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 Nita137b96e2016-01-25 11:31:15 -080059 * <p>That also means creating a BreakIterator for each locale. Expensive...</p>
Mihai Nita1808ff72016-01-12 08:53:54 -080060 *
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 Nita502141d2016-02-22 16:40:26 -0800102 String result = ULocale.getDisplayNameWithDialect(locale.toLanguageTag(),
Mihai Nita1808ff72016-01-12 08:53:54 -0800103 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 Nita502141d2016-02-22 16:40:26 -0800115 String result = ULocale.getDisplayNameWithDialect(locale.toLanguageTag(),
116 ULocale.getDefault());
Mihai Nita1808ff72016-01-12 08:53:54 -0800117 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 Nita1808ff72016-01-12 08:53:54 -0800150 final Locale dispLocale = displayLocale == null ? Locale.getDefault() : displayLocale;
Mihai Nitafc881a22016-01-28 09:38:17 -0800151
Mihai Nita1808ff72016-01-12 08:53:54 -0800152 int localeCount = locales.size();
Mihai Nitafc881a22016-01-28 09:38:17 -0800153 final String[] localeNames = new String[localeCount];
Mihai Nita1808ff72016-01-12 08:53:54 -0800154 for (int i = 0; i < localeCount; i++) {
Mihai Nitafc881a22016-01-28 09:38:17 -0800155 localeNames[i] = LocaleHelper.getDisplayName(locales.get(i), dispLocale, false);
Mihai Nita1808ff72016-01-12 08:53:54 -0800156 }
157
Mihai Nitafc881a22016-01-28 09:38:17 -0800158 ListFormatter lfn = ListFormatter.getInstance(dispLocale);
Mihai Nita502141d2016-02-22 16:40:26 -0800159 return lfn.format((Object[]) localeNames);
Mihai Nita1808ff72016-01-12 08:53:54 -0800160 }
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 Nita137b96e2016-01-25 11:31:15 -0800181 public static final class LocaleInfoComparator implements Comparator<LocaleStore.LocaleInfo> {
Mihai Nita1808ff72016-01-12 08:53:54 -0800182 private final Collator mCollator;
Mihai Nitaf1f39cf2016-02-29 14:42:12 -0800183 private final boolean mCountryMode;
Mihai Nita4de75e92016-03-30 14:57:58 -0700184 private static final String PREFIX_ARABIC = "\u0627\u0644"; // ALEF-LAM, ال
Mihai Nita1808ff72016-01-12 08:53:54 -0800185
186 /**
187 * Constructor.
188 *
189 * @param sortLocale the locale to be used for sorting.
190 */
Mihai Nitaf1f39cf2016-02-29 14:42:12 -0800191 public LocaleInfoComparator(Locale sortLocale, boolean countryMode) {
Mihai Nita1808ff72016-01-12 08:53:54 -0800192 mCollator = Collator.getInstance(sortLocale);
Mihai Nitaf1f39cf2016-02-29 14:42:12 -0800193 mCountryMode = countryMode;
Mihai Nita1808ff72016-01-12 08:53:54 -0800194 }
195
Mihai Nita4de75e92016-03-30 14:57:58 -0700196 /*
197 * The Arabic collation should ignore Alef-Lam at the beginning (b/26277596)
198 *
199 * We look at the label's locale, not the current system locale.
200 * This is because the name of the Arabic language itself is in Arabic,
201 * and starts with Alef-Lam, no matter what the system locale is.
202 */
203 private String removePrefixForCompare(Locale locale, String str) {
204 if ("ar".equals(locale.getLanguage()) && str.startsWith(PREFIX_ARABIC)) {
205 return str.substring(PREFIX_ARABIC.length());
206 }
207 return str;
208 }
209
Mihai Nita1808ff72016-01-12 08:53:54 -0800210 /**
211 * Compares its two arguments for order.
212 *
213 * @param lhs the first object to be compared
214 * @param rhs the second object to be compared
215 * @return a negative integer, zero, or a positive integer as the first
216 * argument is less than, equal to, or greater than the second.
217 */
218 @Override
219 public int compare(LocaleStore.LocaleInfo lhs, LocaleStore.LocaleInfo rhs) {
220 // We don't care about the various suggestion types, just "suggested" (!= 0)
221 // and "all others" (== 0)
Mihai Nita86235d42016-04-01 11:50:59 -0700222 if (lhs.isSuggested() == rhs.isSuggested()) {
Mihai Nita1808ff72016-01-12 08:53:54 -0800223 // They are in the same "bucket" (suggested / others), so we compare the text
Mihai Nita4de75e92016-03-30 14:57:58 -0700224 return mCollator.compare(
225 removePrefixForCompare(lhs.getLocale(), lhs.getLabel(mCountryMode)),
226 removePrefixForCompare(rhs.getLocale(), rhs.getLabel(mCountryMode)));
Mihai Nita1808ff72016-01-12 08:53:54 -0800227 } else {
228 // One locale is suggested and one is not, so we put them in different "buckets"
229 return lhs.isSuggested() ? -1 : 1;
230 }
231 }
232 }
233}