【问题标题】:should the app first start with the default language?应用程序应该首先使用默认语言启动吗?
【发布时间】:2017-07-06 07:23:20
【问题描述】:

我为该应用创建了 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
   */
    }

用户如何更改默认语言?

【问题讨论】:

    标签: android language-translation


    【解决方案1】:

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

    【讨论】:

    • 是的,我这样做了,我将所选语言符号(如 En、De、Fr、...)和标志的图像路径保存为共享首选项中的字符串。解决这个问题,我每次重新启动应用程序后都必须选择语言。我在这里找到了一些类似的东西但没有用:link
    • 您可能需要利用活动生命周期函数,如 onCreateonResume 和其他函数来加载文件。您在什么时候检查所选语言?
    • @Biblio ... and the image-path of the flag as string in shared preference ... 错误。您也可以本地化 drawable 文件夹。不需要做一些天真的事情,比如存储正确标志图像的路径。
    • 我使用额外的活动来更改语言:Laguage.java。主要是MainActivity。如果我更改活动语言中的语言,我会再次自动返回 MainActivity,语言和标志(img)将被更改。一切都好。问题是:如果关闭应用程序并重新启动它,所选语言将丢失,我再次获得默认语言。
    • @Biblio 正如建议的那样,使用 SharedPreferences 在更改时存储语言。更新应用程序中的语言环境。开始时,检查 SharedPreferences 中的设置并相应地设置语言环境。完成。
    【解决方案2】:

    我使用了如下所示的长方式,但它有效。谢谢:

    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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多