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