如何获取当前Android应用程序用于向用户显示文本的语言(区域设置)?如何获取当前Android应用程序显示的语言(区域设置)?
我知道我可以使用Locale.getDefault()
来获取默认的OS语言环境。但是,如果此语言环境不受应用程序支持,则它可能与应用程序用于显示文本和其他资源的语言环境有所不同。
我需要确定由应用显示的语言(区域),从而应用程序可以通过语言到服务器,所以它可以本地化返回的结果。
如何获取当前Android应用程序用于向用户显示文本的语言(区域设置)?如何获取当前Android应用程序显示的语言(区域设置)?
我知道我可以使用Locale.getDefault()
来获取默认的OS语言环境。但是,如果此语言环境不受应用程序支持,则它可能与应用程序用于显示文本和其他资源的语言环境有所不同。
我需要确定由应用显示的语言(区域),从而应用程序可以通过语言到服务器,所以它可以本地化返回的结果。
我自己的解决方案是添加到strings.xml
键值对locale=<locale code>
,因此context.getResources().getString(R.string.locale)
将返回特定于使用的语言环境的语言环境代码。
这是对你的应用程序的配置,这样你就可以得到它:
getResources().getConfiguration().locale
这是
Locale.getDefault()
不同,显示了区域设置的应用程序使用它可以是不同的。
它可以是不同的,因为开发人员可以通过更新应用程序的配置更改, 检查:Resources.updateConfiguration
您列出的第一个fn的文档显示“当前用户的语言环境用户首选项,对应于语言环境资源限定符”。如果这是“当前用户偏好”,那么与第二个fn有什么不同? 无论哪种方式,Locale类的哪个函数只需一次调用即可获取可传回服务器的语言环境代码String(等同于资源扩展)(如原始问题)? – Tom
这个答案提到了获取'Locale'(第一行代码)的正确方法。但是有区别的是错误的:'Locale.getDefault()'等价于第一个例子。如果你想获得语言代码,你必须调用'getLanguage()'。 – caw
不幸的是,这并不像我需要的那样工作。如果设置了系统语言,例如Dansk(丹麦语),getResources()。getConfiguration()。locale'返回da_DK,同时显示英文字符串(来自'values/strings.xml')。 –
您可以使用下面的代码。例如,下面介绍的函数可以放在扩展Application类的类中。
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;
}
使用此方法。
下面的代码对我的作品:
@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
还有就是,让不同格式的相同的语言环境信息的方法列表:
locale.getDefault ().getCountry();
locale.getDefault()。getISO3Language();
locale.getDefault()。getISO3Country();
locale.getDefault()。getDisplayCountry();
locale.getDefault()。getDisplayName();
locale.getDefault()。getDisplayLanguage();
locale.getDefault()。getLanguage();
locale.getDefault()。toString();
我想这个解决方案应该是你的问题的一部分。 –
@YaqubAhmad我以后来到这个解决方案。但我仍在等待更好的解决方案。 –
我喜欢这个解决方案,因为我保证只有在使用我的翻译资源时才能获得语言环境代码。这是我唯一需要的时间。否则,如果我在获取语言环境时犯了某种错误,我可能会在另一种语言中获得日期和其他字符串资源。 – Suragch