1/*
2 * Copyright (C) 2013 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 android.support.v4.text;
18
19import android.os.Build;
20
21public class ICUCompat {
22
23    interface ICUCompatImpl {
24        public String getScript(String locale);
25        public String addLikelySubtags(String locale);
26    }
27
28    static class ICUCompatImplBase implements ICUCompatImpl {
29        @Override
30        public String getScript(String locale) {
31            return null;
32        }
33
34        @Override
35        public String addLikelySubtags(String locale) {
36            return locale;
37        }
38    }
39
40    static class ICUCompatImplIcs implements ICUCompatImpl {
41        @Override
42        public String getScript(String locale) {
43            return ICUCompatIcs.getScript(locale);
44        }
45
46        @Override
47        public String addLikelySubtags(String locale) {
48            return ICUCompatIcs.addLikelySubtags(locale);
49        }
50    }
51
52    private static final ICUCompatImpl IMPL;
53
54    static {
55        final int version = Build.VERSION.SDK_INT;
56        if (version >= 14) {
57            IMPL = new ICUCompatImplIcs();
58        } else {
59            IMPL = new ICUCompatImplBase();
60        }
61    }
62
63    /**
64     * Returns the script (language code) of a script.
65     *
66     * @param locale The locale.
67     * @return a String representing the script (language code) of the locale.
68     */
69    public static String getScript(String locale) {
70        return IMPL.getScript(locale);
71    }
72
73    /**
74     * Add the likely subtags for a provided locale ID, per the algorithm described in the following
75     * CLDR technical report:
76     *
77     * http://www.unicode.org/reports/tr35/#Likely_Subtags
78     *
79     * If locale is already in the maximal form, or there is no data available for maximization,
80     * it will be just returned. For example, "und-Zzzz" cannot be maximized, since there is no
81     * reasonable maximization.
82     *
83     * Examples:
84     *
85     * "en" maximizes to "en_Latn_US"
86     * "de" maximizes to "de_Latn_US"
87     * "sr" maximizes to "sr_Cyrl_RS"
88     * "sh" maximizes to "sr_Latn_RS" (Note this will not reverse.)
89     * "zh_Hani" maximizes to "zh_Hans_CN" (Note this will not reverse.)
90
91     * @param locale The locale to maximize
92     *
93     * @return the maximized locale
94     */
95    public static String addLikelySubtags(String locale) {
96        return IMPL.addLikelySubtags(locale);
97    }
98}
99