首页 文章

获取设备中的当前语言

提问于
浏览
522

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

20 回答

  • 1

    获取设备语言的正确方法如下:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        return context.getResources().getConfiguration().getLocales().get(0);
    } else {
        return context.getResources().getConfiguration().locale;
    }
    

    希望能帮助到你 .

  • 6

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

    Locale.getDefault().getDisplayLanguage();
    
  • 2

    我在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
    
  • 2

    对我有用的是:

    Resources.getSystem().getConfiguration().locale;
    

    Resource.getSystem()返回一个全局共享的Resources对象,该对象仅提供对系统资源的访问(没有应用程序资源),并且没有为当前屏幕配置(不能使用维度单位,不会根据方向更改等) .

    由于 getConfiguration.locale 现已弃用,因此在Android Nougat中获取主要语言环境的首选方法是:

    Resources.getSystem().getConfiguration().getLocales().get(0);
    

    为了保证与以前的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;
    }
    

    Update

    从支持库26.1.0开始,您不需要检查Android版本,因为它提供了向后兼容的方便方法getLocales() .

    只需致电:

    ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration());
    
  • 1

    您可以从当前语言环境中“提取”语言 . 您可以通过标准Java API或使用Android Context来提取语言环境 . 例如,下面两行是等价的:

    String locale = context.getResources().getConfiguration().locale.getDisplayName();
    String locale = java.util.Locale.getDefault().getDisplayName();
    
  • 1

    为了节省其他时间和/或混淆,我想分享一下,我已经尝试了Johan Pelgrim提出的两个替代方案,在我的设备上它们是等效的 - 无论默认位置是否改变 .

    所以我的设备的默认设置是英语(United Kindom),并且在这种状态下如预期的那样,Johan的答案中的两个字符串给出了相同的结果 . 如果我然后更改手机设置中的语言环境(比如意大利语(意大利语))并重新运行,那么Johan的答案中的两个字符串都会将语言环境设置为italiano(Italia) .

    因此,我认为约翰的原始帖子是正确的,格雷姆的评论是不正确的 .

  • 751

    Locale reference所述,获得语言的最佳方式是:

    Locale.getDefault().getLanguage()
    

    此方法根据ISO 639-1 standart返回具有语言ID的字符串

  • 70

    你可以用它

    boolean isLang = Locale.getDefault().getLanguage().equals("xx");
    

    当“xx”是任何语言代码,如“en”,“fr”,“sp”,“ar”....等等

  • 5

    要添加到Johan Pelgrim's answer

    context.getResources().getConfiguration().locale
    Locale.getDefault()
    

    是等价的,因为 android.text.format.DateFormat 类可以互换使用,例如

    private static String zeroPad(int inValue, int inMinDigits) {
        return String.format(Locale.getDefault(), "%0" + inMinDigits + "d", inValue);
    }
    

    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 ...
    }
    
  • 16

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

    PackageManager packageManager = context.getPackageManager();
    Resources resources = packageManager.getResourcesForApplication("android");
    String language = resources.getConfiguration().locale.getLanguage();
    
  • 1

    有两种语言 .

    OS的默认语言:

    Locale.getDefault().getDisplayLanguage();
    

    目前的应用语言:

    getResources().getConfiguration().locale.getDisplayLanguage();//return string
    
  • 34

    您可以使用此代码查找键盘电流

    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    InputMethodSubtype ims = imm.getCurrentInputMethodSubtype();
    String locale = ims.getLocale();
    
  • -3

    上面的答案不区分简单的中文和繁体中文 . Locale.getDefault().toString() 作品返回"zh_CN","zh_TW","en_US"等 .

    参考:https://developer.android.com/reference/java/util/Locale.html,ISO 639-1是旧的 .

  • 4

    如果您选择的语言无法输入希腊文可能会有所帮助 .

    getDisplayLanguage().toString() = English
    getLanguage().toString() = en 
    getISO3Language().toString() = eng
    getDisplayLanguage()) = English
    getLanguage() = en
    getISO3Language() = eng
    

    现在尝试用希腊语

    getDisplayLanguage().toString() = Ελληνικά
    getLanguage().toString() = el
    getISO3Language().toString() = ell
    getDisplayLanguage()) = Ελληνικά
    getLanguage() = el
    getISO3Language() = ell
    
  • 0
    if(Locale.getDefault().getDisplayName().equals("हिन्दी (भारत)")){
        // your code here
    }
    
  • 664

    如果API级别为24或更高,请使用 LocaleList.getDefault().get(0).getLanguage() 否则使用 Locale.getDefault.getLanguage()

    private fun getSystemLocale(): String {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            return LocaleList.getDefault()[0].language
        } else {
            return Locale.getDefault().language
        }
    }
    

    参考:https://developer.android.com/guide/topics/resources/multilingual-support

  • 13
    Locale.getDefault().getDisplayLanguage()
    

    会给你 Written 语言的名称,例如, English, Dutch, French

    Locale.getDefault().getLanguage()
    

    会给你 language code ,例如: en, nl, fr

    两种方法都返回String

  • 2

    我的解决方案是这样的

    @SuppressWarnings("deprecation")
    public String getCurrentLocale2() {
        return Resources.getSystem().getConfiguration().locale.getLanguage();
    }
    
    @TargetApi(Build.VERSION_CODES.N)
    public Locale getCurrentLocale() {
        getResources();
        return Resources.getSystem().getConfiguration().getLocales().get(0);
    }
    

    然后

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    Log.e("Locale", getCurrentLocale().getLanguage());
                } else {
                    Log.e("Locale", getCurrentLocale2().toString());
                }
    

    显示---> en

  • 0

    这是获取设备国家/地区的代码 . 兼容所有版本的android甚至oreo .

    解决方案:如果用户没有SIM卡而不是国家,则在手机设置或当前语言选择期间使用SIM卡 .

    public static String getDeviceCountry(Context context) {
        String deviceCountryCode = null;
    
        final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    
            if(tm != null) {
                deviceCountryCode = tm.getNetworkCountryIso();
            }
    
        if (deviceCountryCode != null && deviceCountryCode.length() <=3) {
            deviceCountryCode = deviceCountryCode.toUpperCase();
        }
        else {
            deviceCountryCode = ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration()).get(0).getCountry().toUpperCase();
        }
    
      //  Log.d("countryCode","  : " + deviceCountryCode );
        return deviceCountryCode;
    }
    
  • 43

    如果你想为那些说印地语的印度居住的用户做特定的任务,那么在条件下使用以下

    if(Locale.getDefault().getDisplayName().equals("हिन्दी (भारत)")){
     //Block executed only for the users resides in India who speaks Hindi 
    }
    

相关问题