My application that the user can change the language of the application.
Currently, my application supports 5 languages.
But when the user chooses another language, the language of the app will still be English.
Resources res = context.getResources();
DisplayMetrics dm = res.getDisplayMetrics();
android.content.res.Configuration conf = res.getConfiguration();
conf.setLocale(new Locale(language_code.toLowerCase()));
Each time the application is launched, the selected language is set first.
However, there is no change in the language!
Of course, if i change the language before using Context.getString(R.string.name) every time, the selected language will be changed successfully!!.
>Solution :
You must make these changes to the attachBaseContext function when you run each Activity.
public Context createConfiguration(Context context, String lan) {
Locale locale = new Locale(lan);
Configuration configuration = new Configuration(context.getResources().getConfiguration());
configuration.setLocale(locale);
return context.createConfigurationContext(configuration);
}
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(createConfiguration(newBase, "en"/*LANGUAGE_SELECTED*/)));
}
Alse for the activity you are in, after changing the language, call function
recreate();.