jyotiprakash
jyotiprakash

Reputation: 2086

Facing issue with multilanguage settings in my application?

In my application I would like create a settings where user can be able to change the language according to his choice. For this i have created respective values folder and string files for each language. Also my code snippet is as follows:

public class MultiLanguage extends Activity implements OnClickListener {

    private Button germany, english, french;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.language);

        english = (Button) findViewById(R.id.english);
        english.setOnClickListener(this);

        germany = (Button) findViewById(R.id.germany);
        germany.setOnClickListener(this);

        french = (Button) findViewById(R.id.french);
        french.setOnClickListener(this);

    }

    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.english:
            setLocale("en");
            break;

        case R.id.germany:
            setLocale("de");
            break;

        case R.id.french:
            setLocale("fr");
            break;
        }
    }
    public void setLocale(String localStr) {

        Locale localeLang = new Locale(localStr);
        Locale.setDefault(localeLang);
        Configuration config = new Configuration();
        config.locale = localeLang;
        getBaseContext().getResources().updateConfiguration(config,
                getBaseContext().getResources().getDisplayMetrics());
        finish();
        Intent refreshActivity = new Intent(this, Home.class);
        refreshActivity.setAction(Intent.ACTION_CONFIGURATION_CHANGED);
        startActivity(refreshActivity);
    }

}

BUt each time I am coming out from the application and launching the app again the language selected became english. I am not getting the root of this problem.

In manifest file I have also mentioned the following line:

android:configChanges="locale"

Can any body correct the mistake I have made here.

Upvotes: 1

Views: 322

Answers (3)

deepak Sharma
deepak Sharma

Reputation: 1641

The problem is with your flow you finish your Activity and and then start new one with intent so do like this:

    Intent refreshActivity = new Intent(this, Home.class);
    refreshActivity.setAction(Intent.ACTION_CONFIGURATION_CHANGED);
    startActivity(refreshActivity);
    finish();

Upvotes: 1

Guillaume
Guillaume

Reputation: 22822

Aren't you trying to reinvent the wheel?

Why not using the default locale selected by the user on his phone? This is the preferred way in the Android ecosystem.

The only thing you have to do is provide multiple versions (one per desired locale) of your resource files (e.g. values/strings.xml). If the user's locale is not found in your application's resources, it will default to the values/strings.xml. Else it will use values-[locale]/strings.xml

More details: http://developer.android.com/guide/topics/resources/localization.html

Upvotes: 1

Vladimir Ivanov
Vladimir Ivanov

Reputation: 43098

The problem is that you don't save your language in between of program launches. Persist it using preferences and read them in your onCreate.

Upvotes: 4

Related Questions