Reputation: 13
I'm building an Android app where the locale needs to be changed at runtime.
I recreate the whole activity and some strings change that is bound to my viewmodel
but other strings of (strings.xml) remain untranslated.
I used the following class in my activity to translate:
public class Language {
private static String PREFS_LANGUAGE = "LANGUAGE";
public static void setLanguage(Context context, String language, Activity activity) {
// Save selected language
persist(language);
// Update language
Locale locale = new Locale(language);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = Locale.UK;
context.getResources().updateConfiguration(config, MainApplication.getContext().getResources().getDisplayMetrics());
activity.onConfigurationChanged(config);
}
private static SharedPreferences getSharedPrefs(Context context) {
return context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
}
private static void persist(String language) {
SharedPreferences.Editor editor = getSharedPrefs(MainApplication.getContext()).edit();
editor.putString(PREFS_LANGUAGE, language);
editor.apply();
}
}
and in my activity:
fun setLanguage(language: String){
Language.setLanguage(MainApplication.getContext(),language, this)
}
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
recreate()
}
Upvotes: 1
Views: 2052
Reputation: 13
The answer of notTdar helped me to find the solution but I also found a bug in my viewmodel:
change: getContext().getResources().getString(R.string.str)
to: Mainapplication.getContext().getResources().getString(R.string.str)
Upvotes: 0
Reputation: 1490
I just copy-pasted this from my old source, based on Java.
Override this in your Application class and Mainactvity.
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(LocaleHelper.onAttach(base));
Log.e(TAG, "attachBaseContext: ");
}
How to use: Whenever you click something, invoke like this.
Here,
language is hi (Hindi) and the region is India (IN)
Eg:
LocaleHelper.setLocale(this, "hi", "IN");
recreate(); //now restart.
A Local Helper class
public class LocaleHelper {
private static final String TAG = "DAFT_PUNK_LH : ";
private static final String SELECTED_LANGUAGE = "en";
private static final String SELECTED_LANGUAGE_COUNTRY = "US";
public static Context onAttach(Context context) {
Log.d(TAG, "onAttach:");
String lang = getPersistedData(context, Locale.getDefault().getLanguage());
String langCountry = getPersistedCountryData(context, Locale.getDefault().getCountry());
return setLocale(context, lang, langCountry);
}
public static String getLanguage(Context context) {
return getPersistedData(context, Locale.getDefault().getLanguage());
}
public static String getLanguageCountry(Context context) {
return getPersistedCountryData(context, Locale.getDefault().getCountry());
}
public static Context setLocale(Context context, String language, String langCountry) {
Log.d(TAG, "setLocale: ");
persist(context, language, langCountry);
return updateResources(context, language, langCountry);
}
private static String getPersistedData(Context context, String defaultLanguage) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(SELECTED_LANGUAGE, defaultLanguage);
}
private static String getPersistedCountryData(Context context, String defaultLangCountry) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(SELECTED_LANGUAGE_COUNTRY, defaultLangCountry);
}
private static void persist(Context context, String language, String langCountry) {
Log.d(TAG, "persist: ");
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(SELECTED_LANGUAGE, language);
editor.putString(SELECTED_LANGUAGE_COUNTRY, langCountry);
editor.apply();
}
@TargetApi(Build.VERSION_CODES.N)
private static Context updateResources(Context context, String language, String langCountry) {
Log.d(TAG, "updateResources: ");
Locale locale = new Locale(language, langCountry);
Locale.setDefault(locale);
Configuration configuration = context.getResources().getConfiguration();
configuration.setLocale(locale);
configuration.setLayoutDirection(locale);
return context.createConfigurationContext(configuration);
}
}
Upvotes: 2