DeR*_*gan 803
如果您想获得设备的选定语言,这可能对您有所帮助:
Locale.getDefault().getDisplayLanguage();
Run Code Online (Sandbox Code Playgroud)
tra*_*nte 769
我在Android 4.1.2设备上检查了Locale方法,结果如下:
Locale.getDefault().getLanguage() ---> en
Locale.getDefault().getISO3Language() ---> eng
Locale.getDefault().getCountry() ---> US
Locale.getDefault().getISO3Country() ---> USA
Locale.getDefault().getDisplayCountry() ---> United States
Locale.getDefault().getDisplayName() ---> English (United States)
Locale.getDefault().toString() ---> en_US
Locale.getDefault().getDisplayLanguage()---> English
Run Code Online (Sandbox Code Playgroud)
Sar*_*rpe 91
对我有用的是:
Resources.getSystem().getConfiguration().locale;
Run Code Online (Sandbox Code Playgroud)
Resource.getSystem()返回一个全局共享的Resources对象,该对象仅提供对系统资源的访问(没有应用程序资源),并且没有为当前屏幕配置(不能使用维度单位,不会根据方向更改等).
由于getConfiguration.locale
现已弃用,因此在Android Nougat中获取主要语言环境的首选方法是:
Resources.getSystem().getConfiguration().getLocales().get(0);
Run Code Online (Sandbox Code Playgroud)
为了保证与以前的Android版本的兼容性,可能的解决方案是一个简单的检查:
Locale locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
locale = Resources.getSystem().getConfiguration().getLocales().get(0);
} else {
//noinspection deprecation
locale = Resources.getSystem().getConfiguration().locale;
}
Run Code Online (Sandbox Code Playgroud)
更新
从支持库开始,26.1.0
您不需要检查Android版本,因为它提供了向后兼容的方便方法getLocales()
.
只需致电:
ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration());
Run Code Online (Sandbox Code Playgroud)
Joh*_*rim 43
您可以从当前语言环境中"提取"语言.您可以通过标准Java API或使用Android Context来提取语言环境.例如,下面两行是等价的:
String locale = context.getResources().getConfiguration().locale.getDisplayName();
String locale = java.util.Locale.getDefault().getDisplayName();
Run Code Online (Sandbox Code Playgroud)
小智 35
为了节省其他时间和/或混淆,我想分享一下,我已经尝试了Johan Pelgrim提出的两个替代方案,在我的设备上它们是等效的 - 无论默认位置是否改变.
所以我的设备的默认设置是英语(United Kindom),并且在这种状态下如预期的那样,Johan的答案中的两个字符串给出了相同的结果.如果我然后更改手机设置中的语言环境(比如意大利语(意大利语))并重新运行,那么Johan的答案中的两个字符串都会将语言环境设置为italiano(Italia).
因此,我认为约翰的原始帖子是正确的,格雷姆的评论是不正确的.
Ops*_*nas 17
如Locale参考中所述,获取语言的最佳方法是:
Locale.getDefault().getLanguage()
Run Code Online (Sandbox Code Playgroud)
此方法根据ISO 639-1标准返回具有语言ID的字符串
Sim*_*ges 16
你可以用它
boolean isLang = Locale.getDefault().getLanguage().equals("xx");
Run Code Online (Sandbox Code Playgroud)
当"xx"是任何语言代码,如"en","fr","sp","ar"....等等
小智 9
如果 API 级别为 24 或以上,则使用LocaleList.getDefault().get(0).getLanguage()
else 使用Locale.getDefault.getLanguage()
private fun getSystemLocale() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
LocaleList.getDefault().get(0).language
} else {
Locale.getDefault().language
}
Run Code Online (Sandbox Code Playgroud)
参考:https : //developer.android.com/guide/topics/resources/multilingual-support
context.getResources().getConfiguration().locale
Locale.getDefault()
Run Code Online (Sandbox Code Playgroud)
是等价的,因为android.text.format.DateFormat
类可以互换使用,例如
private static String zeroPad(int inValue, int inMinDigits) {
return String.format(Locale.getDefault(), "%0" + inMinDigits + "d", inValue);
}
Run Code Online (Sandbox Code Playgroud)
和
public static boolean is24HourFormat(Context context) {
String value = Settings.System.getString(context.getContentResolver(),
Settings.System.TIME_12_24);
if (value == null) {
Locale locale = context.getResources().getConfiguration().locale;
// ... snip the rest ...
}
Run Code Online (Sandbox Code Playgroud)
这个解决方案对我有用。这将返回 Android 设备的语言(不是应用程序的本地语言)
String locale = getApplicationContext().getResources().getConfiguration().locale.getLanguage();
Run Code Online (Sandbox Code Playgroud)
这将返回“en”或“de”或“fr”或您的设备语言设置的任何内容。
public void GetDefaultLanguage( ) {
try {
String langue = Locale.getDefault().toString(); // ---> en_US
/*
Log.i("TAG", Locale.getDefault().getLanguage() ); // ---> en
Log.i("TAG", Locale.getDefault().getISO3Language() ); // ---> eng
Log.i("TAG", Locale.getDefault().getCountry() ); // ---> US
Log.i("TAG", Locale.getDefault().getISO3Country() ); // ---> USA
Log.i("TAG", Locale.getDefault().getDisplayCountry() ); // ---> United States
Log.i("TAG", Locale.getDefault().getDisplayName() ); // ---> English (United States)
Log.i("TAG", Locale.getDefault().toString() ); // ---> en_US
Log.i("TAG", Locale.getDefault().getDisplayLanguage() ); //---> English
*/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
langue = Locale.getDefault().toLanguageTag(); // ---> en-US
url_Api = getUrlMicrosoftLearn(langue);
Log.i("TAG", url_Api );
Log.i("TAG", langue );
}else{
langue = langue.replace("_","-"); // ---> en-US
url_Api = getUrlMicrosoftLearn(langue);
Log.i("TAG", url_Api );
Log.i("TAG", langue );
}
}catch (Exception ex) {
Log.i("TAG", "Exception:GetDefaultLanguage()", ex);
}
}
public String getUrlMicrosoftLearn(String langue) {
return "https://learn.microsoft.com/"+langue+"/learn";
}
Run Code Online (Sandbox Code Playgroud)
您可以尝试从系统资源获取区域设置:
PackageManager packageManager = context.getPackageManager();
Resources resources = packageManager.getResourcesForApplication("android");
String language = resources.getConfiguration().locale.getLanguage();
Run Code Online (Sandbox Code Playgroud)
如果要检查当前语言,请使用@Sarpe (@Thorbear) 的答案:
val language = ConfigurationCompat.getLocales(Resources.getSystem().configuration)?.get(0)?.language
// Check here the language.
val format = if (language == "ru") "d MMMM yyyy ?." else "d MMMM yyyy"
val longDateFormat = SimpleDateFormat(format, Locale.getDefault())
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
292019 次 |
最近记录: |