Android获取系统语言(兼容7.0)

从 Android 7.0 开始,系统语言支持多个,可手动排序,系统根据 App 本身支持的语言和手机出厂设置的语言等因素来调整 App 本身的默认语言。使用 getResources().getConfiguration().getLocales() 返回的是一个 LocaleList 对象,它包含 >=1 个 Locale,内容项可由用户增删,顺序可由用户调整。但是,此接口返回的语言顺序和用户定义的顺序不一定一致!

获取系统真实首选语言:

1
2
3
4
5
6
7
8
Locale locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
locale = LocaleList.getDefault().get(0);
} else {
locale = Locale.getDefault();
}

String language = locale.getLanguage() + "-" + locale.getCountry();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Log.i("yezhou", "Language: " + getLanguage() + ", LocaleLanguage: " + getLocaleLanguage() + ", Country: " + getCountry());
//Language: zh, LocaleLanguage: zh-CN, Country: CN

public static String getLanguage() {
Locale locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
locale = LocaleList.getDefault().get(0);
} else {
locale = Locale.getDefault();
}
return locale.getLanguage();
}

public static String getLocaleLanguage() {
Locale locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
locale = LocaleList.getDefault().get(0);
} else {
locale = Locale.getDefault();
}
return locale.getLanguage() + "-" + locale.getCountry();
}

public static String getCountry() {
Locale locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
locale = LocaleList.getDefault().get(0);
} else {
locale = Locale.getDefault();
}
return locale.getCountry();
}

参考资料:https://developer.android.com/reference/java/util/Locale.html

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :