1 /*
2  * Copyright (C) 2023 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.settings.localepicker;
18 
19 import android.os.LocaleList;
20 
21 import androidx.annotation.NonNull;
22 
23 import java.util.Locale;
24 
25 /**
26  * A locale utility class.
27  */
28 public class LocaleUtils {
29     /**
30      * Checks if the languageTag is in the system locale. Since in the current design, the system
31      * language list would not show two locales with the same language and region but different
32      * numbering system. So, the u extension has to be stripped out in the process of comparison.
33      *
34      * @param languageTag A language tag
35      * @return true if the locale is in the system locale. Otherwise, false.
36      */
isInSystemLocale(@onNull String languageTag)37     public static boolean isInSystemLocale(@NonNull String languageTag) {
38         LocaleList systemLocales = LocaleList.getDefault();
39         Locale localeWithoutUextension =
40                 new Locale.Builder()
41                         .setLocale(Locale.forLanguageTag(languageTag))
42                         .clearExtensions()
43                         .build();
44         for (int i = 0; i < systemLocales.size(); i++) {
45             Locale sysLocaleWithoutUextension =
46                     new Locale.Builder().setLocale(systemLocales.get(i)).clearExtensions().build();
47             if (localeWithoutUextension.equals(sysLocaleWithoutUextension)) {
48                 return true;
49             }
50         }
51         return false;
52     }
53 }
54