如何在不更改电话语言的情况下更改片段的语言?

时间:2020-03-18 01:30:37

标签: android android-fragments fragment locale

我注意到我的应用程序仅在更改电话语言时才更改语言,但是仅对于片段而言,对于活动而言,它将起作用

我正在使用

 private void setLocale(String lang) {
        Locale locale= new Locale(lang);
        Locale.setDefault(locale);
        Configuration config=new Configuration();
        config.locale=locale;
        getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());
        SharedPreferences.Editor editor=getSharedPreferences("Settings",MODE_PRIVATE).edit();
        editor.putString("My_Lang",lang);
        editor.commit();

    }

    public void loadLocale(){
        SharedPreferences editor=getSharedPreferences("Settings",Splash.MODE_PRIVATE);
        String language=editor.getString("My_Lang","My_Lang");
        Log.d("hahahahahahdhjzshsdj",language);
        setLocale(language);


    }

private void showChangeLanguageDialog() {
        final String[] listLang={"Francais","العربية","English"};
        AlertDialog.Builder onBuilder=new AlertDialog.Builder(Splash.this);
        onBuilder.setTitle("Choose Language...");
        onBuilder.setSingleChoiceItems(listLang, -1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if(which==0){
                    setLocale("fr");
                    recreate();
                    Intent intent=new Intent(Splash.this,Splash2.class);
                    startActivity(intent);
                }
                else if(which==1){

                    setLocale("ar");
                    recreate();
                    Intent intent=new Intent(Splash.this,Splash2.class);
                    startActivity(intent);
                }
                else if(which==2){

                    setLocale("en");
                    recreate();
                    Intent intent=new Intent(Splash.this,Splash2.class);
                    startActivity(intent);
                }

                dialog.dismiss();
            }
        });
        AlertDialog mDialog=onBuilder.create();
        mDialog.show();
    }

因此,当我运行该应用程序时,我得到一个alertDialog来选择语言,选择它后,它改变了唯一的不活动状态,但没有变化,我有6个片断

我该如何解决?

0 个答案:

没有答案