简体   繁体   中英

Android Localization from resources

I am currently trying to localize my android app and i have setup everything but it keeps of loading the same string resource whenever i try to change the language.

I have the below string resources

values
values-am
values-om

SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
String languageLocale = sharedPreferences.getString("SAVED_LANGUAGE_LOCALE", "am");

    Log.d(TAG,languageLocale);

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

I am also using the code below for selecting the language in a different activity

RadioGroup.OnCheckedChangeListener radioGroupOnCheckedChangeListener =
        new RadioGroup.OnCheckedChangeListener(){

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                RadioButton checkedRadioButton = (RadioButton)mRadioGroup.findViewById(checkedId);
                int checkedIndex = mRadioGroup.indexOfChild(checkedRadioButton);
                //AppController languageSetting = (AppController)getApplication();
                SavePreferences(KEY_SAVED_RADIO_BUTTON_INDEX, checkedIndex);
                mLanguageId = checkedIndex + 1;
                languageSetting.setLanguageId(mLanguageId);
                switch (mLanguageId){
                    case 1:
                        SavePreferencesLocale(KEY_SAVED_LANGUAGE_LOCALE,"en");
                        mTest = "en";
                        break;
                    case 2:
                        SavePreferencesLocale(KEY_SAVED_LANGUAGE_LOCALE,"am");
                        mTest = "am";
                        break;
                    case 3:
                        SavePreferencesLocale(KEY_SAVED_LANGUAGE_LOCALE,"om");
                        mTest = "om";
                        break;
                }

            }};

Now the problem is whenever the application started for the first time it loads the correct string resource but when I change the language it keeps the same string resource. that means eg. if the app starts with the values-am string resource when i change it to values-om i still see the values-am strings despite the locale is assigned with the correct value ("om")

You need to call the below code on every language selection in onCheckedChanged() .

Locale locale = new Locale("Selected language code comes here");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

After setting the Locale, use recreate() to restart the activity.

Here is something:

String languageLocale = sharedPreferences.getString("SAVED_LANGUAGE_LOCALE", "am");

by **

sharedPreferences.getString(""SAVED_LANGUAGE_LOCALE", "am");

** you dont need string "am" . This "am" you need only by .putString() !

Should look like

  String languageLocale = sharedPreferences.getString("KEY_SAVED_LANGUAGE_LOCALE");

Intent Refresh and call

Locale locale = new Locale("ar"); 
Locale.setDefault(locale); 
Configuration config = new Configuration(); 
config.locale = locale; getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics()); 

this.setContentView(R.layout.activity_main_category_page);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM