该应用程序应该首先使用默认语言?

时间:2017-07-06 07:23:21

标签: android language-translation

我为该应用程序创建了4种语言。我可以更改Lauaguage,好吧,但如果关闭应用程序然后再启动它,应用程序将首先使用默认的string.xml启动。

如何让应用程序以上次选择的语言开始?

我应该在mainActivity中通过OnCreate调用方法吗?

     @SuppressWarnings("deprecation")
    public void setLocale(String lang) {
        Locale myLocale = new Locale(lang);

        DisplayMetrics dm = getResources().getDisplayMetrics();
        Configuration conf = getResources().getConfiguration();
        conf.locale = myLocale;
        getResources().updateConfiguration(conf, dm);
        Intent refresh = new Intent(this, Languages.class);
        startActivity(refresh);
        /*         "en" = English
            "hi" =Hindi
            "fr" =French
            "it" =Italian
            "de" =German
            "es" =Spanish
            "ja" =Japanese
            "ko" =Korean
            "nl" =Dutch
            "pt" =Portuguese
            "ru" =Russian
            "zh" =Chinese
            "ar" = arabic
   */
    }

用户如何更改默认语言?

2 个答案:

答案 0 :(得分:1)

为什么不将所选语言存储在共享首选项中?这样,您可以随时在应用启动时检查所选语言,然后加载相应的语言文件。

答案 1 :(得分:0)

我使用了很长的方式,但它有效。谢谢:

OnResume:

    selected_lang= myshared.getString("selected_lang","de"); 
    lang_found= Integer.parseInt(myshared.getString("lang_found","0"));  
setLocale(selected_lang);



@SuppressWarnings("deprecation")
public void setLocale(String lang) {
    Locale myLocale = new Locale(lang);    
    DisplayMetrics dm = getResources().getDisplayMetrics();
    Configuration conf = getResources().getConfiguration();
    conf.locale = myLocale;
    getResources().updateConfiguration(conf, dm);

    if(lang_found==0) {    
        Intent refresh = new Intent(this, MainActivity.class);
        startActivity(refresh);
        lang_found=1;    
    }

@Override
protected void onDestroy() {

    lang_found=0;
    Save_setting();
    super.onDestroy();
}