I have two different strings.xml for multi-language app. Where can I set that I want to use English strings.xml file independent of phone locale language always but use Chinese language only in case when on splash screen we have chosen it?
-
Have you read this support-languagesPratik Butani– Pratik Butani2020-01-02 11:30:54 +00:00Commented Jan 2, 2020 at 11:30
-
Do you want to support choosing language in your app?Bracadabra– Bracadabra2020-01-02 11:39:08 +00:00Commented Jan 2, 2020 at 11:39
-
@Bracadabra yes, simply I need to open app with strings.xml that will conform to selected on start screen language.Eugene V.– Eugene V.2020-01-02 12:09:20 +00:00Commented Jan 2, 2020 at 12:09
Add a comment
|
1 Answer
To support language setting in your application you need to change current configuration and restart activity to reload all resources. You can change current configuration like this:
void changeLanguage(Context context, String language) {
final Locale locale = new Locale(language);
Locale.setDefault(locale);
final Resources res = context.getResources();
final Configuration config = new Configuration(res.getConfiguration());
config.setLocale(locale);
res.updateConfiguration(config, res.getDisplayMetrics());
}
And then don't forget to recreate your activity activity.recreate(); or reload your resources manually.
Also you need to set your locale on every process start. You can do it in application onCreate:
public void YourApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
changeLanguage(this, getLanguageSettingsFromPreferences());
}
}