• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.textclassifier.common.base;
18 
19 import android.content.Context;
20 import android.os.Build;
21 import java.util.Locale;
22 
23 /** Helper for accessing locale related stuff that works across different platform versions. */
24 public final class LocaleCompat {
25 
LocaleCompat()26   private LocaleCompat() {}
27 
28   /**
29    * Returns a well-formed IETF BCP 47 language tag representing this locale. In older platforms,
30    * only the ISO 639 language code will be returned.
31    *
32    * @see Locale#toLanguageTag()
33    */
toLanguageTag(Locale locale)34   public static String toLanguageTag(Locale locale) {
35     if (Build.VERSION.SDK_INT >= 24) {
36       return Api24Impl.toLanguageTag(locale);
37     }
38     return ApiBaseImpl.toLanguageTag(locale);
39   }
40 
41   /** Returns the language tags in string for the current resources configuration. */
getResourceLanguageTags(Context context)42   public static String getResourceLanguageTags(Context context) {
43     if (Build.VERSION.SDK_INT >= 24) {
44       return Api24Impl.getResourceLanguageTags(context);
45     } else if (Build.VERSION.SDK_INT >= 21) {
46       return Api21Impl.getResourceLanguageTags(context);
47     }
48     return ApiBaseImpl.getResourceLanguageTags(context);
49   }
50 
51   private static class Api24Impl {
Api24Impl()52     private Api24Impl() {}
53 
toLanguageTag(Locale locale)54     static String toLanguageTag(Locale locale) {
55       return locale.toLanguageTag();
56     }
57 
getResourceLanguageTags(Context context)58     static String getResourceLanguageTags(Context context) {
59       return context.getResources().getConfiguration().getLocales().toLanguageTags();
60     }
61   }
62 
63   private static class Api21Impl {
Api21Impl()64     private Api21Impl() {}
65 
getResourceLanguageTags(Context context)66     static String getResourceLanguageTags(Context context) {
67       return context.getResources().getConfiguration().locale.toLanguageTag();
68     }
69   }
70 
71   private static class ApiBaseImpl {
ApiBaseImpl()72     private ApiBaseImpl() {}
73 
toLanguageTag(Locale locale)74     static String toLanguageTag(Locale locale) {
75       return locale.getLanguage();
76     }
77 
getResourceLanguageTags(Context context)78     static String getResourceLanguageTags(Context context) {
79       return context.getResources().getConfiguration().locale.getLanguage();
80     }
81   }
82 }
83