summaryrefslogtreecommitdiffstats
path: root/src/com/android/launcher3/locale/LocaleSet.java
blob: 34634ab7ef6774639396115cf0cf8ab90ee31960 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package com.android.launcher3.locale;

import android.text.TextUtils;
import com.google.common.annotations.VisibleForTesting;
import java.util.Locale;

public class LocaleSet {
    private static final String CHINESE_LANGUAGE = Locale.CHINESE.getLanguage().toLowerCase();
    private static final String JAPANESE_LANGUAGE = Locale.JAPANESE.getLanguage().toLowerCase();
    private static final String KOREAN_LANGUAGE = Locale.KOREAN.getLanguage().toLowerCase();

    private static class LocaleWrapper {
        private final Locale mLocale;
        private final String mLanguage;
        private final boolean mLocaleIsCJK;

        private static boolean isLanguageCJK(String language) {
            return CHINESE_LANGUAGE.equals(language) ||
                    JAPANESE_LANGUAGE.equals(language) ||
                    KOREAN_LANGUAGE.equals(language);
        }

        public LocaleWrapper(Locale locale) {
            mLocale = locale;
            if (mLocale != null) {
                mLanguage = mLocale.getLanguage().toLowerCase();
                mLocaleIsCJK = isLanguageCJK(mLanguage);
            } else {
                mLanguage = null;
                mLocaleIsCJK = false;
            }
        }

        public boolean hasLocale() {
            return mLocale != null;
        }

        public Locale getLocale() {
            return mLocale;
        }

        public boolean isLocale(Locale locale) {
            return mLocale == null ? (locale == null) : mLocale.equals(locale);
        }

        public boolean isLocaleCJK() {
            return mLocaleIsCJK;
        }

        public boolean isLanguage(String language) {
            return mLanguage == null ? (language == null)
                    : mLanguage.equalsIgnoreCase(language);
        }

        public String toString() {
            return mLocale != null ? mLocale.toLanguageTag() : "(null)";
        }
    }

    public static LocaleSet getDefault() {
        return new LocaleSet(Locale.getDefault());
    }

    public LocaleSet(Locale locale) {
        this(locale, null);
    }

    /**
     * Returns locale set for a given set of IETF BCP-47 tags separated by ';'.
     * BCP-47 tags are what is used by ICU 52's toLanguageTag/forLanguageTag
     * methods to represent individual Locales: "en-US" for Locale.US,
     * "zh-CN" for Locale.CHINA, etc. So eg "en-US;zh-CN" specifies the locale
     * set LocaleSet(Locale.US, Locale.CHINA).
     *
     * @param localeString One or more BCP-47 tags separated by ';'.
     * @return LocaleSet for specified locale string, or default set if null
     * or unable to parse.
     */
    public static LocaleSet getLocaleSet(String localeString) {
        // Locale.toString() generates strings like "en_US" and "zh_CN_#Hans".
        // Locale.toLanguageTag() generates strings like "en-US" and "zh-Hans-CN".
        // We can only parse language tags.
        if (localeString != null && localeString.indexOf('_') == -1) {
            final String[] locales = localeString.split(";");
            final Locale primaryLocale = Locale.forLanguageTag(locales[0]);
            // ICU tags undefined/unparseable locales "und"
            if (primaryLocale != null &&
                    !TextUtils.equals(primaryLocale.toLanguageTag(), "und")) {
                if (locales.length > 1 && locales[1] != null) {
                    final Locale secondaryLocale = Locale.forLanguageTag(locales[1]);
                    if (secondaryLocale != null &&
                            !TextUtils.equals(secondaryLocale.toLanguageTag(), "und")) {
                        return new LocaleSet(primaryLocale, secondaryLocale);
                    }
                }
                return new LocaleSet(primaryLocale);
            }
        }
        return getDefault();
    }

    private final LocaleWrapper mPrimaryLocale;
    private final LocaleWrapper mSecondaryLocale;

    public LocaleSet(Locale primaryLocale, Locale secondaryLocale) {
        mPrimaryLocale = new LocaleWrapper(primaryLocale);
        mSecondaryLocale = new LocaleWrapper(
                mPrimaryLocale.equals(secondaryLocale) ? null : secondaryLocale);
    }

    public LocaleSet normalize() {
        final Locale primaryLocale = getPrimaryLocale();
        if (primaryLocale == null) {
            return getDefault();
        }
        Locale secondaryLocale = getSecondaryLocale();
        // disallow both locales with same language (redundant and/or conflicting)
        // disallow both locales CJK (conflicting rules)
        if (secondaryLocale == null ||
                isPrimaryLanguage(secondaryLocale.getLanguage()) ||
                (isPrimaryLocaleCJK() && isSecondaryLocaleCJK())) {
            return new LocaleSet(primaryLocale);
        }
        // unnecessary to specify English as secondary locale (redundant)
        if (isSecondaryLanguage(Locale.ENGLISH.getLanguage())) {
            return new LocaleSet(primaryLocale);
        }
        return this;
    }

    public boolean hasSecondaryLocale() {
        return mSecondaryLocale.hasLocale();
    }

    public Locale getPrimaryLocale() {
        return mPrimaryLocale.getLocale();
    }

    public Locale getSecondaryLocale() {
        return mSecondaryLocale.getLocale();
    }

    public boolean isPrimaryLocale(Locale locale) {
        return mPrimaryLocale.isLocale(locale);
    }

    public boolean isSecondaryLocale(Locale locale) {
        return mSecondaryLocale.isLocale(locale);
    }

    private static final String SCRIPT_SIMPLIFIED_CHINESE = "Hans";
    private static final String SCRIPT_TRADITIONAL_CHINESE = "Hant";

    @VisibleForTesting
    public static boolean isLocaleSimplifiedChinese(Locale locale) {
        // language must match
        if (locale == null || !TextUtils.equals(locale.getLanguage(), CHINESE_LANGUAGE)) {
            return false;
        }
        // script is optional but if present must match
        if (!TextUtils.isEmpty(locale.getScript())) {
            return locale.getScript().equals(SCRIPT_SIMPLIFIED_CHINESE);
        }
        // if no script, must match known country
        return locale.equals(Locale.SIMPLIFIED_CHINESE);
    }

    public boolean isPrimaryLocaleSimplifiedChinese() {
        return isLocaleSimplifiedChinese(getPrimaryLocale());
    }

    public boolean isSecondaryLocaleSimplifiedChinese() {
        return isLocaleSimplifiedChinese(getSecondaryLocale());
    }

    @VisibleForTesting
    public static boolean isLocaleTraditionalChinese(Locale locale) {
        // language must match
        if (locale == null || !TextUtils.equals(locale.getLanguage(), CHINESE_LANGUAGE)) {
            return false;
        }
        // script is optional but if present must match
        if (!TextUtils.isEmpty(locale.getScript())) {
            return locale.getScript().equals(SCRIPT_TRADITIONAL_CHINESE);
        }
        // if no script, must match known country
        return locale.equals(Locale.TRADITIONAL_CHINESE);
    }

    public boolean isPrimaryLocaleTraditionalChinese() {
        return isLocaleTraditionalChinese(getPrimaryLocale());
    }

    public boolean isSecondaryLocaleTraditionalChinese() {
        return isLocaleTraditionalChinese(getSecondaryLocale());
    }

    public boolean isPrimaryLocaleCJK() {
        return mPrimaryLocale.isLocaleCJK();
    }

    public boolean isSecondaryLocaleCJK() {
        return mSecondaryLocale.isLocaleCJK();
    }

    public boolean isPrimaryLanguage(String language) {
        return mPrimaryLocale.isLanguage(language);
    }

    public boolean isSecondaryLanguage(String language) {
        return mSecondaryLocale.isLanguage(language);
    }

    @Override
    public boolean equals(Object object) {
        if (object == this) {
            return true;
        }
        if (object instanceof LocaleSet) {
            final LocaleSet other = (LocaleSet) object;
            return other.isPrimaryLocale(mPrimaryLocale.getLocale())
                    && other.isSecondaryLocale(mSecondaryLocale.getLocale());
        }
        return false;
    }

    @Override
    public final String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append(mPrimaryLocale.toString());
        if (hasSecondaryLocale()) {
            builder.append(";");
            builder.append(mSecondaryLocale.toString());
        }
        return builder.toString();
    }
}