Change the Locale at Runtime

Change the locale at runtime?

Since API 11 you can use recreate so can make this method in your activity:

private void restartInLocale(Locale locale)
{
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
Resources resources = getResources();
resources.updateConfiguration(config, resources.getDisplayMetrics());
recreate();
}

Changing locale at runtime in Swing

  1. You have a method that is used to change app locale (and probably persist the new value) and another one to get localized strings.

  2. Create an interface:

    interface LocaleChangeListener {
    void onLocaleChange();
    }

    Implement it by UI components that need to be able to change locale at runtime and set the new values in overrides onLocaleChange().

  3. Now, have a list of listeners that will be notified on locale change by the first method.

Android locale not (completely) changing at runtime

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);
}
}

Android how to change the application language at runtime

you can use this code in spinner or any way you want

String languageToLoad  = "en"; // your language 
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());

then you should save the language like this

SharedPreferences languagepref = getSharedPreferences("language",MODE_PRIVATE);
SharedPreferences.Editor editor = languagepref.edit();
editor.putString("languageToLoad",languageToLoad );
editor.commit();

and use the same code in every activity in onCreate() to load the languageToLoad from the SharedPreferences

Dynamically change the locale in Angular 7 at runtime without having to reload the app

I think you don't need to reload, but just delay a little to ending registerCulture

for example:

english() {
spinner.show(); // <-- start any loader
setTimeout(() => {
this.session.registerCulture('en');
spinner.hide(); // <-- stop the loader
}, 1000);
// window.location.reload();
}

Change language at runtime in android

1.try like this it work for me

public static void changeLanguage(String languageCode, Context context)
{
Locale locale = new Locale(languageCode);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
context.getApplicationContext().getResources()
.updateConfiguration(config, context.getResources()
.getDisplayMetrics());

}

Change display language at runtime and recreate all activities

Add "locale" property in your android:configChanges in all you activities and then override onConfigurationChanged() in activities and handle the language changes accordingly.

More on http://developer.android.com/guide/topics/manifest/activity-element.html#config

Changing Locale at runtime not effecting fragments

I assume that you are using this approach. Where its not suggested any change in the AndroidManifest.xml so the android:configChanges="locale" may cause the misbehavior you defined.

For the date formating you should take into consideration that your application is not using the Locale.getDefault() but something different that is defined by the user and your LocaleHelper mechanism.

Some extra details about config change

The android:configChanges means you don't want the system to recreate your activity when one of the provided attribute happens. In your case the locale.

In terms of development with that approach in order to properly handle this option in the manifest you have to implement the

@Override
public void onConfigurationChanged(Configuration newConfig) {
// refresh your views here
super.onConfigurationChanged(newConfig);
}

and then perform your own handling. Something that you didn't required in your case.



Related Topics



Leave a reply



Submit