我在所有活动中使用Base类更改语言。我正在使用语言环境类来更改语言,并且将语言设置为sharedpreferences。此代码仅适用于棒棒糖版本和较早版本,尤其是在诸如棉花糖和馅饼版本语言更改方法之类的所有版本之后均无效。
public class BaseActivity extends AppCompatActivity {
private Locale mCurrentLocale;
public static Locale getLocale(Context context) {
SharedPreferences sharedPreferences = context.getSharedPreferences("language", Context.MODE_PRIVATE);
String lang = sharedPreferences.getString("languege", "");
switch (lang) {
case "English":
lang = "en";
break;
case "Swahili":
lang = "sw";
break;
}
return new Locale(lang);
}
@Override
protected void onStart() {
super.onStart();
mCurrentLocale = getResources().getConfiguration().locale;
}
@Override
protected void onRestart() {
super.onRestart();
Locale locale = getLocale(this);
if (!locale.equals(mCurrentLocale)) {
mCurrentLocale = locale;
recreate();
}
}
}
我正在创建扩展清单的App类。该类从共享首选项类中获取语言。
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
setLocale();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setLocale();
}
private void setLocale() {
final Resources resources = getResources();
final Configuration configuration = resources.getConfiguration();
final Locale locale = BaseActivity.getLocale(this);
if (!configuration.locale.equals(locale)) {
configuration.setLocale(locale);
resources.updateConfiguration(configuration, null);
}
}
}
LanguegeSelectionActivity类,用于将语言设置为sharedpreference并使用intent将selectedLanguage传递给MainActivity
public class LanguegeSelectionActivity extends AppCompatActivity implements View.OnClickListener {
LinearLayout llEnglish, llSwahili;
SharedPreferences sharedPreferences;
Intent intent;
Toolbar toolbar;
String language, isLanguageSelect;
TextView txtEnglish, txtSubEnglish, txtKiswahili, txtSubSwahili;
Typeface typeface;
String color = "#ffffff";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_languege_selection);
llEnglish = (LinearLayout) findViewById(R.id.ll_english);
llSwahili = (LinearLayout) findViewById(R.id.ll_swahili);
txtEnglish = (TextView) findViewById(R.id.txt_english);
txtSubEnglish = (TextView) findViewById(R.id.txt_sub_english);
txtKiswahili = (TextView) findViewById(R.id.txt_kiswahili);
txtSubSwahili = (TextView) findViewById(R.id.txt_sub_swahili);
typeface = Typeface.createFromAsset(getAssets(), "font/Aller_Rg.ttf");
txtEnglish.setTypeface(typeface);
txtSubEnglish.setTypeface(typeface);
txtKiswahili.setTypeface(typeface);
txtSubSwahili.setTypeface(typeface);
llEnglish.setOnClickListener(this);
llSwahili.setOnClickListener(this);
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle(R.string.choose_language);
toolbar.setTitleTextColor(Color.parseColor(color));
if (getIntent().getStringExtra("languageSelection") != null) {
isLanguageSelect = getIntent().getStringExtra("languageSelection");
if (isLanguageSelect.equalsIgnoreCase("true")) {
sharedPreferences = LanguegeSelectionActivity.this.getSharedPreferences("language", Context.MODE_PRIVATE);
language = sharedPreferences.getString("languege", "");
if (!language.equalsIgnoreCase("")) {
intent = new Intent(LanguegeSelectionActivity.this, DrawerActivity.class);
startActivity(intent);
finish();
}
}
} else {
sharedPreferences = LanguegeSelectionActivity.this.getSharedPreferences("language", Context.MODE_PRIVATE);
sharedPreferences.edit().clear().apply();
}
}
@Override
public void onClick(View view) {
sharedPreferences = LanguegeSelectionActivity.this.getSharedPreferences("language", Context.MODE_PRIVATE);
switch (view.getId()) {
case R.id.ll_english:
SharedPreferences.Editor editor;
editor = sharedPreferences.edit();
editor.putString("languege", "English");
editor.apply();
intent = new Intent(LanguegeSelectionActivity.this, DrawerActivity.class);
ProcessPhoenix.triggerRebirth(LanguegeSelectionActivity.this, intent);
startActivity(intent);
finish();
break;
case R.id.ll_swahili:
editor = sharedPreferences.edit();
editor.putString("languege", "Swahili");
editor.apply();
intent = new Intent(LanguegeSelectionActivity.this, DrawerActivity.class);
ProcessPhoenix.triggerRebirth(LanguegeSelectionActivity.this, intent);
startActivity(intent);
finish();
break;
}
}
}
答案 0 :(得分:1)
尝试使用此代码更改语言...
Locale locale = new Locale("ar");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
settings.edit().putString("locale", "ar").commit();
this.finish();
Intent refresh = new Intent(this, MainActivity.class);
startActivity(refresh);
在每个活动的onCreate和Onresume方法中,您都必须做这样的事情。
if (settings.getString("locale","").equals("en")) {
Locale locale = new Locale("en");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
settings.edit().putString("locale", "en").commit();
}