1/*
2 * Copyright (C) 2014 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.layoutlib.bridge.android;
18
19import java.util.Locale;
20
21import com.ibm.icu.util.ULocale;
22
23/**
24 * This class provides an alternate implementation for {@code java.util.Locale#toLanguageTag}
25 * which is only available after Java 6.
26 *
27 * The create tool re-writes references to the above mentioned method to this one. Hence it's
28 * imperative that this class is not deleted unless the create tool is modified.
29 */
30@SuppressWarnings("UnusedDeclaration")
31public class AndroidLocale {
32
33    public static String toLanguageTag(Locale locale)  {
34        return ULocale.forLocale(locale).toLanguageTag();
35    }
36
37    public static String adjustLanguageCode(String languageCode) {
38        String adjusted = languageCode.toLowerCase(Locale.US);
39        // Map new language codes to the obsolete language
40        // codes so the correct resource bundles will be used.
41        if (languageCode.equals("he")) {
42            adjusted = "iw";
43        } else if (languageCode.equals("id")) {
44            adjusted = "in";
45        } else if (languageCode.equals("yi")) {
46            adjusted = "ji";
47        }
48
49        return adjusted;
50    }
51
52    public static Locale forLanguageTag(String tag) {
53        return ULocale.forLanguageTag(tag).toLocale();
54    }
55
56    public static String getScript(Locale locale) {
57        return ULocale.forLocale(locale).getScript();
58    }
59}
60