After the screen rotation, the language of my application will be changed
Asked Answered
H

4

15

I have created a bilingual (having two languages) android application. I have inserted my resource strings in two files:

For Persian language (default)
values/strings_locale.xml‬

For English language 
values-en/strings_locale.xml‬

I my first launching Activity I have inserted the following code:

Configuration c = new Configuration(this.getResources().getConfiguration());
c.locale = new Locale("fa_IR");

this.getResources().updateConfiguration(c, this.getResources().getDisplayMetrics());

So after this code, my default language will be Persian and all of strings in all of Activities are shown in Persian language correctly.

But the problem is when the screen of the device is rotated, all of the strings are shown in English and it also happens for all other Activities! And I have to close and reopen my application.

Why this happens and how I can solve this problem?

Harp answered 4/11, 2013 at 10:14 Comment(1)
Check this documentation, when the activity is recreated after the rotation, the code you above is probably not run again... make sure you place the code in the spot where it may be processed again after rotation.Unproductive
M
27

You can make class which extends Application. There you can override one method which gets called everytime you change configuration (example when screen orientation gets changed).

Something like:

public class MyApplication extends Application {
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        setLocale();
    }

    private void setLocale() {
        Locale locale = new Locale("fa_IR");
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getBaseContext().getResources().updateConfiguration(config,
              getBaseContext().getResources().getDisplayMetrics());
    }
}

And dont forget to declare it in the manifest: example of Application class

In AndroidManifest.xml:

<application
    android:name="com.blaablaa.bumbam.MyApplication"
    ...
Mispronounce answered 4/11, 2013 at 10:41 Comment(0)
P
0

I don't exactly understand what you are trying to do. But in general you don't have to manage the language of the app. Android will automatically pick the phone language if available and in any other case bring up a dialog to pick the language.

If you do want to set language independent from that with your code - which I would not recommend - then there is probably a problem with your lifecycle preventing your code from being run again after an orientation change. I can help you with that if you post more of your code.

Par answered 4/11, 2013 at 10:36 Comment(2)
It is possible. If you put the code I mentioned In your first Activity, All of your activities will be shown in that language. You can test it. My problem is when I rotate device in for example the third Activity, the language of the whole Activities of my application is changed to English.Harp
I know it is possible. I'm just saying you should not do it. But if you still want me to help, then you need to post more of your code.Par
D
0

You can create custom ContextWrapper

public class mContextWrapper extends ContextWrapper {
    public mContextWrapper(Context base){
        super(base);
    }

    @SuppressWarnings("deprecation")
    public static ContextWrapper wrap(Context context, String language) {
        Configuration config = context.getResources().getConfiguration();
        Locale sysLocale = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            sysLocale = getSystemLocale(config);
        } else {
            sysLocale = getSystemLocaleLegacy(config);
        }
        if (!language.equals("") && !sysLocale.getLanguage().equals(language)) {
            Locale locale = new Locale(language);
            Locale.setDefault(locale);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                setSystemLocale(config, locale);
            } else {
                setSystemLocaleLegacy(config, locale);
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                context = context.createConfigurationContext(config);
            } else {
                context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
            }
        }
        return new mContextWrapper(context);
    }

    @SuppressWarnings("deprecation")
    public static Locale getSystemLocaleLegacy(Configuration config){
        return config.locale;
    }

    @TargetApi(Build.VERSION_CODES.N)
    public static Locale getSystemLocale(Configuration config){
        return config.getLocales().get(0);
    }

    @SuppressWarnings("deprecation")
    public static void setSystemLocaleLegacy(Configuration config, Locale locale){
        config.locale = locale;
    }

    @TargetApi(Build.VERSION_CODES.N)
    public static void setSystemLocale(Configuration config, Locale locale){
        config.setLocale(locale);
    }
}

and in your activity attachBaseContext attach your custom ContextWrapper

 @Override
    protected void attachBaseContext(Context newBase) {
        globalClass = (YouAppClass)newBase.getApplicationContext();
        //try geting the lang you have setted inside your YouAppClass class
        // or you can use shared preferences for it 
        //pref = PreferenceManager.getDefaultSharedPreferences(newBase)
        lang=globalClass .getLang();
        super.attachBaseContext(mContextWrapper.wrap(newBase,lang));
    }
Doyen answered 11/10, 2017 at 8:20 Comment(0)
K
0

Activity change the configuration object

    override fun onConfigurationChanged(newConfig: Configuration) {
    val lang = appPreferences.getAppPrefString(AppConstants.PREF_USER_LANG)
    val locale = if (lang.isNullOrEmpty() || (lang == AppConstants.GERMAN)) {
        Locale("ksh")
    } else {
        Locale("en")
    }
    Locale.setDefault(locale)
    val resources: Resources = resources
    val config: Configuration =newConfig
    config.setLocale(locale)
    resources.updateConfiguration(config, resources.displayMetrics)
    super.onConfigurationChanged(config)
}
Kynewulf answered 11/11, 2022 at 14:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.