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);
}
...
}
5 回答
我自己的解决方案是添加到
strings.xml
键值对locale=<locale code>
,因此context.getResources().getString(R.string.locale)
将返回特定于已使用区域设置的区域设置代码 .这是你的应用程序配置,所以你可以得到它:
这不同于
并显示应用程序使用的区域设置可能不同 .
它可能不同,因为开发人员可以通过更新应用程序配置来更改它,请检查:Resources.updateConfiguration
以下代码适用于我:
注意:在Build.VERSION_CODES.N上获取区域设置的方式略有变化 . 对于N及以上,代码应该类似于下面的代码块;但是,我只是在上面的代码块中为M及以下做它,它也适用于N及以上 .
我得到了以下支持的国家语言:
有一个方法列表以不同的格式提供相同的区域设置信息:
您可以使用以下代码 . 例如,下面给出的函数可以放在扩展Application类的类中 .
然后在主代码中我们可以写的任何地方:
在您的情况下,您很快就可以使用
getAppLanguage()
,然后检查公共变量APP_LANG
以获取当前使用的语言 .使用此方法 .