0

My aim is to change an application language from English to Chinese during runtime, is there any suggestions?

 language_spinner = (Spinner)findViewById(R.id.settings_language_spinner);
 language_spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                if (pos == 1){
                    Toast.makeText(parent.getContext(),"You have selected English",Toast.LENGTH_SHORT).show();
                    setLocale("en");

                }else if (pos == 2){
                    Toast.makeText(parent.getContext(),"You have selected Chinese",Toast.LENGTH_SHORT).show();
                    setLocale("zh");
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {

            }
        });


    }

    public void setLocale(String lang) {

        myLocale = new Locale(lang);
        Resources res = getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        Configuration conf = res.getConfiguration();
        if (!conf.locale.getLanguage().equals(lang)) {
            conf.locale = myLocale;
            res.updateConfiguration(conf, dm);
            Intent refresh = new Intent(this, SettingsActivity.class);
            startActivity(refresh);
            finish();
        }
    }

This code is working properly with English but not working with Chinese please help me in finding the solution ..

Laazo
  • 467
  • 8
  • 22
Surekha Kc
  • 3
  • 1
  • 3

2 Answers2

0

I can give you idea how you can implement this:

step 1: make string file for all texts in values directory as string-ch, ch is the code for Chinese language. and in code get every string with the getResources.getString(R.string.text_name); so that it can take string value at run time either English or Chinese.

step 2: now create method :

void changeLanguage(String language) {
        Locale locale = new Locale(language);
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getBaseContext().getResources().updateConfiguration(config,
                getBaseContext().getResources().getDisplayMetrics());
    }

step 3: call this method where you want to change language suppose if you want to get changed language of you application according to device language then you can call as:

changeLanguage(Locale.getDefault().getLanguage());
Stuti Kasliwal
  • 771
  • 9
  • 20
  • Thanks a lot .I tried this way its not working.This project is having multiple activity.. – Surekha Kc Jul 27 '17 at 15:55
  • please suggest another method .This was a little urgent project. I really appreciate that you answered. – Surekha Kc Jul 27 '17 at 15:56
  • I also implemented this on multiple activities project and it was successfully worked, can you describe the exact scenario for this? – Stuti Kasliwal Jul 28 '17 at 05:29
  • Hi ..yea it started working .Thank you so much ..Now at the finishing stage ..But When the language is changing....the app need to restart ..is there any option so that the app does not restart. – Surekha Kc Jul 31 '17 at 15:43
  • Hey It is really nice that it started working, and for refreshing the activity you first need to finish the current activity and start it again. – Stuti Kasliwal Aug 01 '17 at 05:36
0

Create below function in Util like class

public static void setLocale(Activity activity, String languageCode) {
    Locale locale = new Locale(languageCode);
    Locale.setDefault(locale);
    Resources resources = activity.getResources();
    Configuration config = resources.getConfiguration();
    config.setLocale(locale);
    resources.updateConfiguration(config, resources.getDisplayMetrics());
}

Call this function from activity at the start. The original post with this code snippet can be found at Change app language programmatically in Android page.

Vikrant Korde
  • 315
  • 2
  • 11