如何在Android设备中选择当前语言?


当前回答

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

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

其他回答

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

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

以上答案没有区分简体中文和繁体中文。 Locale.getDefault(). tostring()工作,返回“zh_CN”,“zh_TW”,“en_US”等。

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

下面是获取设备国家的代码。兼容所有版本的android甚至奥利奥。

解决方案:如果用户没有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;
}

Locale.getDefault().getLanguage()是虚拟机语言

Locale.getDefault().getLanguage()

它是当前运行应用程序的虚拟机实例的语言。它由java类(如DateFormat等)使用。如果您使用某些java类,则在更改应用程序区域设置时可能需要修改此选项。如果你在改变你的App语言环境时修改了这个,它与android的语言是不一样的。

context.getConfiguration().locale.getLanguage()是活动语言

context.getConfiguration().locale.getLanguage()

这是您活动中设置的语言。 在最新的SDK版本中,以下是可取的

context.getConfiguration().getLocales().get(0).getLanguage()

Resources.getSystem().getConfiguration().getLocales()给出用户在系统级添加的所有地区

这将为您提供用户在系统级别设置的第一个区域设置。

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

大量的用户是多语言的,所以你可能想要循环使用locale。

你可以使用 .getLanguage .setLanguage (Locale.forLanguageTag (Locale.getDefault () ())); 这很好