想要在单一选择中更改整个应用程序语言。
MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static Button english,russian;
private static TextView chooseText;
private static Locale myLocale;
//Shared Preferences Variables
private static final String Locale_Preference = "Locale Preference";
private static final String Locale_KeyValue = "Saved Locale";
private static SharedPreferences sharedPreferences;
private static SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
setListeners();
loadLocale();
}
//Initiate all views
private void initViews() {
sharedPreferences = getSharedPreferences(Locale_Preference, Activity.MODE_PRIVATE);
editor = sharedPreferences.edit();
chooseText = (TextView) findViewById(R.id.choose_text);
english = (Button) findViewById(R.id.english);
russian = (Button) findViewById(R.id.russian);
}
//Set Click Listener
private void setListeners() {
english.setOnClickListener(this);
russian.setOnClickListener(this);
}
@Override
public void onClick(View view) {
String lang = "en";//Default Language
switch (view.getId()) {
case R.id.english:
lang = "en";
break;
case R.id.russian:
lang = "ru";
break;
}
changeLocale(lang);//Change Locale on selection basis
}
//Change Locale
public void changeLocale(String lang) {
if (lang.equalsIgnoreCase(""))
return;
myLocale = new Locale(lang);//Set Selected Locale
saveLocale(lang);//Save the selected locale
Locale.setDefault(myLocale);//set new locale as default
Configuration config = new Configuration();//get Configuration
config.locale = myLocale;//set config locale as selected locale
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());//Update the config
updateTexts();//Update texts according to locale
}
//Save locale method preferences
public void saveLocale(String lang) {
editor.putString(Locale_KeyValue, lang);
editor.commit();
}
//Get locale method in preferences
public void loadLocale() {
String language = sharedPreferences.getString(Locale_KeyValue, "");
changeLocale(language);
}
//Update text methods
private void updateTexts() {
chooseText.setText(R.string.tap_text);
english.setText(R.string.btn_en);
russian.setText(R.string.btn_ru);
}
}
我可以使用上面的代码更改单一活动语言。但我想一键更改所有应用程序语言。请帮我这样做。我试图允许从设备设置语言访问
答案 0 :(得分:1)
创建一个LocalHelper.java类来管理应用的本地化。
public class LocaleHelper {
private static final String SELECTED_LANGUAGE = "SELECTED_LANGUAGE";
public static Context onAttach(Context context) {
String lang = getPersistedData(context, Locale.getDefault().getLanguage());
return setLocale(context, lang);
}
public static Context onAttach(Context context, String defaultLanguage) {
String lang = getPersistedData(context, defaultLanguage);
return setLocale(context, lang);
}
public static String getLanguage(Context context) {
return getPersistedData(context, Locale.getDefault().getLanguage());
}
public static Context setLocale(Context context, String language) {
persist(context, language);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return updateResources(context, language);
}
return updateResourcesLegacy(context, language);
}
private static String getPersistedData(Context context, String defaultLanguage) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(SELECTED_LANGUAGE, defaultLanguage);
}
private static void persist(Context context, String language) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(SELECTED_LANGUAGE, language);
editor.apply();
}
@TargetApi(Build.VERSION_CODES.N)
private static Context updateResources(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Configuration configuration = context.getResources().getConfiguration();
configuration.setLocale(locale);
configuration.setLayoutDirection(locale);
return context.createConfigurationContext(configuration);
}
@SuppressWarnings("deprecation")
private static Context updateResourcesLegacy(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
configuration.locale = locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLayoutDirection(locale);
}
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
return context;
}
}
在您的应用程序类中,您需要覆盖attachBaseContext并调用LocaleHelper.onAttach()来初始化应用程序中的区域设置。
public class MyApplication extends Application {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(LocaleHelper.onAttach(base, "en"));
}
}
每当您的语言发生变化时,您需要致电
LocaleHelper.setLocale(this, yourNewLanguageCode);
这将更改所有其他活动,但不会更改您更改语言的活动。因此,您需要重新创建该活动或更新活动的视图。
要重新创建活动,请致电
startActivity(new Intent(CurrentActivity.this, CurrentActivity.class))
finish();
如果您想更新视图而不是重新创建活动,请使用context
LocaleHelper.setLocale(this, yourNewLanguageCode)
Context context = LocaleHelper.setLocale(this, languageCode);
Resources resources = context.getResources();
textView.setText(resources.getString(R.string.textTitle));
注意:在Android API版本24(Nougat)之后,您需要覆盖所有活动中的attachBaseContext
以反映更改。