如何获取当前 Android 应用程序用于向用户显示文本的语言(区域设置)?
我知道可以使用Locale.getDefault()
来获取默认的操作系统语言环境。但是,如果应用程序不支持该语言环境,则它可能与用于显示文本和其他资源的应用程序使用的语言环境不同。
我需要确定应用程序所显示的语言(区域设置),以便应用程序可以将语言传递给服务器,从而可以在返回的结果中进行本地化。
如何获取当前 Android 应用程序用于向用户显示文本的语言(区域设置)?
我知道可以使用Locale.getDefault()
来获取默认的操作系统语言环境。但是,如果应用程序不支持该语言环境,则它可能与用于显示文本和其他资源的应用程序使用的语言环境不同。
我需要确定应用程序所显示的语言(区域设置),以便应用程序可以将语言传递给服务器,从而可以在返回的结果中进行本地化。
我的解决方案是在strings.xml
中添加键值对locale=<locale code>
,这样context.getResources().getString(R.string.locale)
将返回用于当前语言环境的语言代码。
这取决于你的应用配置,你可以使用以下代码获取它:
getResources().getConfiguration().locale
这与之前的不同
Locale.getDefault()
同时展示应用程序使用的Locale,这可能会有所不同。
它可能会有所不同,因为开发人员可以通过更新应用程序配置来更改它,参见:Resources.updateConfiguration
Locale
的正确方法(第一行代码)。但是错误的是有一个区别:Locale.getDefault()
等同于第一个例子。如果你想要获取语言代码,你需要调用getLanguage()
。 - cawgetResources().getConfiguration().locale
返回的是da_DK,而显示的是英文字符串(来自values/strings.xml
)。 - Aleksejs MjaliksLocale.getDefault()
显示为"de_CH",而不是设备的语言(丹麦语)。getResources().getConfiguration().getLocales().get(0)
(无论是否使用getLanguage()
)正确显示为"德语",但是如果设备使用的是应用程序不支持的语言(例如丹麦语+意大利语),那么Locale.getDefault()
和getResources().getConfiguration().getLocales().get(0)
都会返回设备的语言,而不是应用程序的语言。 - undefined@TargetApi(Build.VERSION_CODES.M)
private String getCurrentDefaultLocaleStr(Context context) {
Locale locale = context.getResources().getConfiguration().locale;
return locale.getDefault().toString();
}
注意:在Build.VERSION_CODES.N中获取区域设置的方法略有变化。对于N及以上版本,代码应如下所示;但是,我只在上面的代码块中为M及以下版本执行此操作,而且它也适用于N及以上版本。
// Build.VERSION_CODES.N
Locale locale = context.getResources().getConfiguration().getLocales().get(0);
English: en
Traditional Chinese: zh_TW_#Hant
Simplified Chinese: zh_CN_#Hans
Spanish: es_ES
French: fr_FR
Japanese: ja_JP
German: de_DE
以下是以不同格式提供相同区域信息的方法列表:
请注意,保留了html标签。locale.getDefault().getCountry();
locale.getDefault().getISO3Language();
locale.getDefault().getISO3Country();
locale.getDefault().getDisplayCountry();
locale.getDefault().getDisplayName();
locale.getDefault().getDisplayLanguage();
locale.getDefault().getLanguage();
locale.getDefault().toString();
Locale
类中没有非静态的getDefault()
方法。 - arekolekfun Context.getCurrentLocale(): Locale {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
this.resources.configuration.locales.get(0);
} else{
this.resources.configuration.locale;
}
}
使用时不会出现任何警告:
val locale = applicationContext.getCurrentLocale()
public class MyApplication extends Application {
...
public static String APP_LANG;
private Context ctx = getBaseContext();
private Resources res = ctx.getResources();
public SharedPreferences settingPrefs;
...
public void restoreAppLanguage(){
/**
*Use this method to store the app language with other preferences.
*This makes it possible to use the language set before, at any time, whenever
*the app will started.
*/
settingPrefs = getSharedPreferences("ConfigData", MODE_PRIVATE);
String lang = settingPrefs.getString("AppLanguage", "");
if(!settingPrefs.getAll().isEmpty() && lang.length() == 2){
Locale myLocale;
myLocale = new Locale(lang);
Locale.setDefault(myLocale);
Configuration config = new Configuration();
config.locale = myLocale;
res.updateConfiguration( config, res.getDisplayMetrics() );
}
}
public void storeAppLanguage(int lang) {
/**
*Store app language on demand
*/
settingPrefs = getSharedPreferences("ConfigData", MODE_PRIVATE);
Editor ed = settingPrefs.edit();
Locale myLocale;
myLocale = new Locale(lang);
Locale.setDefault(myLocale);
Configuration config = new Configuration();
config.locale = myLocale;
res.updateConfiguration( config, res.getDisplayMetrics() );
ed.putString("AppLanguage", lang);
ed.commit();
}
public void setAppLanguage(String lang){
/**
*Use this method together with getAppLanguage() to set and then restore
*language, whereever you need, for example, the specifically localized
*resources.
*/
Locale myLocale;
myLocale = new Locale(lang);
Locale.setDefault(myLocale);
Configuration config = new Configuration();
config.locale = myLocale;
res.updateConfiguration( config, res.getDisplayMetrics() );
}
public void getAppLanguage(){
/**
*Use this method to obtain the current app language name
*/
settingPrefs = getSharedPreferences("ConfigData",MODE_PRIVATE);
String lang = settingPrefs.getString("AppLanguage", "");
if(!settingPrefs.getAll().isEmpty() && lang.length() == 2){
APP_LANG = lang;
}
else APP_LANG = Locale.getDefault().getLanguage();
}
}
然后在主代码中的任何位置,我们可以编写:
public class MainActivity extends ActionBarActivity {
...
MyApplication app;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
...
app = (MyApplication)getApplicationContext();
...
if(settingPrefs.getAll().isEmpty()){
//Create language preference if it is not
app.storeAppLanguage(Locale.getDefault().getLanguage());
}
//Restore previously used language
app.restoreAppLanguage();
...
}
...
void SomethingToDo(){
String lang = "en"; //"fr", "de", "es", "it", "ru" etc.
...
app.getAppLanguage();
app.setAppLanguage(lang);
...
//do anything
...
app.setAppLanguage(app.APP_LANG);
}
...
}
getAppLanguage()
,然后检查公共变量APP_LANG
来获取当前使用的语言。 public boolean CheckLocale(Context context, String app_language) {
Locale myLocale = new Locale(app_language);
Resources res = context.getResources();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
if(res.getConfiguration().locale==myLocale)
return true;
else
return false;
}