获取设备中的当前语言

Pin*_*nki 578 android localization

我们如何才能在Android设备中选择当前语言?

DeR*_*gan 803

如果您想获得设备的选定语言,这可能对您有所帮助:

Locale.getDefault().getDisplayLanguage();
Run Code Online (Sandbox Code Playgroud)

  • 您可以使用`Locale.getDefault().getLanguage();`来获取通常的语言代码(例如"de","en"). (355认同)
  • getDisplayLanguage()将本地化该语言.如果您只想获取ISO代码(例如if或switch语句),请使用'Locale.getDefault().getISO3Language();' (70认同)
  • 我更喜欢`Locale.getDefault().toString()`,它提供了一个完全标识语言环境的字符串,例如"en_US". (47认同)
  • @DeRagan,这并不总能为您的设备提供语言,但仅适用于您的应用.例如,如果我调用`Locale.setDefault("ru")`,系统设置中的语言设置为英语,则方法`Locale.getDefault().getLanguage()`将返回**"ru"**,但不是**"en"**.是否有另一种获得真正的SYSTEM语言环境/语言的方法?我发现没有记录解决方案[这里](http://stackoverflow.com/a/4683532/1048087),但有更优雅的解决方案吗? (34认同)
  • getISO3Language()为Deutschland(德国)返回"deu"之类的东西而不是de ... (13认同)
  • @Prizoff这篇文章将帮助您/sf/answers/1994868361/,使用`Resources.getSystem()。getConfiguration()。locale;` (3认同)
  • `Resources.getSystem()。getConfiguration()。locale`可以在Android 9上正常工作。Locale.getDefault()总是返回英文!谨防! (2认同)

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)

  • 谢谢,很好的一系列可能性.你能否按照"Tom"的评论中的建议添加"Locale.getDefault().toString()". (5认同)
  • 还有`Locale.getDefault().getDisplayLanguage()`返回`"English"` (3认同)

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)

  • getConfiguration().locale目前已弃用,请使用getConfiguration().getLocales().get(0)代替 (6认同)
  • 我不知道在编写答案时是否存在这种情况,但支持库具有向后兼容的版本,因此无需自己编写向后兼容性.`ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration())` (6认同)
  • 这是这个问题的正确答案.其他答案获取应用程序的本地,而不是设备. (3认同)
  • 这是正确的答案。其他人返回应用程序的语言而不是系统的语言。 (2认同)

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)

  • gregm的评论可能是错误的.请参阅airewyre的回答. (16认同)
  • 这不是真的.它们是不同的.如果用户切换Locale,则第一个可以更改.第二个是预先安装在手机上的那个.无论用户做什么,它都不会改变. (10认同)
  • @gregm的评论是错误的.只需自己尝试一下,您会看到,当您更改设置中的语言时,Locale.getDefault()会发生变化. (4认同)

小智 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"....等等

  • if(Locale.ENGLISH.equals(Locale.getDefault().getLanguage())){...} (3认同)

小智 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


arm*_*ino 6

加入Johan Pelgrim的回答

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)


Arj*_*jun 6

这个解决方案对我有用。这将返回 Android 设备的语言(不是应用程序的本地语言)

String locale = getApplicationContext().getResources().getConfiguration().locale.getLanguage();
Run Code Online (Sandbox Code Playgroud)

这将返回“en”或“de”或“fr”或您的设备语言设置的任何内容。


Gra*_*ign 6

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)


ste*_*nsk 5

您可以尝试从系统资源获取区域设置:

PackageManager packageManager = context.getPackageManager();
Resources resources = packageManager.getResourcesForApplication("android");
String language = resources.getConfiguration().locale.getLanguage();
Run Code Online (Sandbox Code Playgroud)


Coo*_*ind 5

如果要检查当前语言,请使用@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)