1

i'm new member to this community.first i say to everybody "Hello"..... this is my first question.this is based on the android app development. So this is my question. I'm still developing an android application.so in that application i need to develop for the 3 multiple languages.

1.English -"en" 2.Chinese (Simplified) - "zh_" 3.Chinese (Traditional) - "zh_TW"

So i add string resources for these languages and i implement a code part for this.But Unfortunately it's not working some time.but it's working some time. i can't identity the mistake of my code. it seems that it hasn't any error.

these is my testing string names(all lanuages)

  • "en" -Sign In
  • "zh_"-登录
  • "zh_TW"-登錄

and this is my code part

public class MainActivity extends AppCompatActivity {  

private Spinner spinnerctrl;
private Locale myLocale;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

  spinnerctrl=(Spinner)findViewById(R.id.spinner1);

    spinnerctrl.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> parent, View view,
                                   int pos, long id) {

            if (pos == 1) {

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

                Toast.makeText(parent.getContext(),
                        "You have selected Chinese(Traditional)", Toast.LENGTH_SHORT)
                        .show();
                saveLocale("zh_TW");

            }
            else if (pos == 3)
            {
                Toast.makeText(parent.getContext(),
                        "You have select English",Toast.LENGTH_SHORT)
                        .show();
                saveLocale("en");
            }

        }

        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
        }

    });

    loadLocale(); 

@Override
public void onBackPressed()
{
    Intent MyProfile = new Intent(getApplicationContext(), MainActivity.class);
    startActivity(MyProfile);
    finish();
}

public void loadLocale() {
    String langPref = "Language";
    SharedPreferences prefs = getSharedPreferences("CommonPrefs",
            Activity.MODE_PRIVATE);
    String language = prefs.getString(langPref, "");
    changeLang(language);
}

public void changeLang(String lang) {
    if (lang.equals(""))
        return;
    myLocale = new Locale(lang);
    saveLocale(lang);
    Locale.setDefault(myLocale);
    Resources res = getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    conf.locale = myLocale;
    res.updateConfiguration(conf, dm);
}

public void saveLocale(String lang) {
    String langPref = "Language";
    SharedPreferences prefs = getSharedPreferences("CommonPrefs",
            Activity.MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString(langPref, lang);
    editor.commit();

}}

So this is my problem.this code part run without any runtime errors,etc.but not working some time.i don't know why is that.So if any one can help me slove this problem it's very big help to me. Thank you.

androidpc
  • 13
  • 5

1 Answers1

0
public static void changeLocale(Context context, String localeString) {

String locale = "en";
if (localeString != null) {
    if (localeString.equals("Malay")) {
        locale = "ms";
    } else if (localeString.equals("Myanmar")) {
        locale = "my";
    } else if (localeString.equals("Tamil")) {
        locale = "ta";
    }
}

Locale myLocale = new Locale(locale);
Resources res = context.getResources();
DisplayMetrics dm = res.getDisplayMetrics();

Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);

}