如何使用上下文包装来更改运行时的应用程序语言?

问题描述:

我想添加一个选项来使用Shared Preferences更改应用程序语言。我在“共享首选项”中保存了“en”或“fa”等语言。如何使用上下文包装来更改运行时的应用程序语言?

现在我需要将Shared Preferences数据设置为语言。我试图使用This code,但我遇到了一些问题。

对于使用此代码,我应该换我的活动上下文有:

@Override 
protected void attachBaseContext(Context newBase) { 
    super.attachBaseContext(MyContextWrapper.wrap(newBase,languageFromPrefs)); 
} 

我写了一个方法,从共享偏好称为getString(Context context,String key,String defaultValue)越来越语言,然后编辑该代码:

@Override 
protected void attachBaseContext(Context newBase) { 
    lang = App.Prefs.getString(newBase, App.Prefs.LANGUAGE_KEY,"fa"); 
    super.attachBaseContext(MyContextWrapper.wrap(newBase,lang)); 
} 

看起来不错,但是当我运行我的应用程序时,我看到了一个奇怪的场景。第一次运行时,app语言是fa(波斯语),下次它会自动更改为en(英文)!

[注:设备的语言是目前英语]


这里是我的MainActivity:

(我评论说,不与我的问题代码)

public class MainActivity extends AppCompatActivity { 

    EditText userText; 
    Button embellishBtn; 
    ImageView settings; 
    String lang; 

    @Override 
    protected void attachBaseContext(Context newBase) { 
     lang = App.Prefs.getString(newBase, App.Prefs.LANGUAGE_KEY,"fa"); 
     super.attachBaseContext(App.wrap(newBase,lang)); 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
/* 
     userText = (EditText) findViewById(R.id.textBox); 
     embellishBtn = (Button) findViewById(R.id.embellishButton); 
     settings = (ImageView) findViewById(R.id.settings); 

     App.SetFont.EditText(userText,this); 
     App.SetFont.Button(embellishBtn,this); 

     settings.setOnLongClickListener(new View.OnLongClickListener() { 

      @Override 
      public boolean onLongClick(View v) { 
       Toast.makeText(getBaseContext(),getString(R.string.settings),Toast.LENGTH_SHORT).show(); 
       return true; 
      } 
     }); 

     MarginActivity.isFirstRateMessage = true; 



     Boolean isFirstOpen = App.Prefs.getBoolean(this, App.Prefs.FIRST_OPEN_KEY,true); 

     if (isFirstOpen && TimeZone.getDefault().getDisplayName().toLowerCase().equals("iran standard time")) 
     { 
      App.Prefs.setString(this, App.Prefs.LANGUAGE_KEY,"fa"); 
      App.Prefs.setBoolean(this,App.Prefs.FIRST_OPEN_KEY,false); 
      startActivity(new Intent(this, MainActivity.class)); 
      finish(); 
     } 
     */ 
    } 
/* 
    public void onSettingsClick(View view) 
    { 
     startActivity(new Intent(this, SettingsActivity.class)); 
    } 

    public void onEmbellishClick(View view) 
    { 
     if(userText.getText().toString().trim().length() > 0) 
     { 
      Intent intent = new Intent(this,EmbellishActivity.class); 
      intent.putExtra("user_text",userText.getText().toString() + " "); 

      startActivity(intent); 
     } 
     else 
     { 
      Animation shake = AnimationUtils.loadAnimation(this,R.anim.focus_zoom); 
      userText.startAnimation(shake); 
     } 
     userText.setText(null); 
    } 

    @Override 
    protected void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 
     outState.putString("user_text",userText.getText().toString()); 
    } 
    @Override 
    protected void onRestoreInstanceState(Bundle savedInstanceState) { 
     super.onRestoreInstanceState(savedInstanceState); 
     userText.setText(savedInstanceState.getString("user_text")); 
    } 
    */ 
} 

和getString方法App.Prefs.getString()):

static String getString(Context context,String key,String defaultValue) 
{ 
    SharedPreferences shared = context.getSharedPreferences("Prefs", MODE_PRIVATE); 
    return shared.getString(key, defaultValue); 
} 


我的应用程序运行没有任何错误。 我应该怎样解决这个问题?怎么了?

+0

请检查以下链接:https://*.com/questions/40221711/android-context-getresources-updateconfiguration-deprecated/40704077#40704077 – nomag

+0

@nomag [LocaleHelper.java](https://gist.github。 com/muhammad-naderi/0ff264e6cc07df904bc88a9f7efbe57d)实际上与[This one]相同(https://*.com/questions/40221711/android-context-getresources-updateconfiguration-deprecated/40704077#40704077)。 –

你必须使用这种方式改变语言运行时

SharedPreferences LanguagePreference = getSharedPreferences("SelectedLanguage", 0); 
    String languageToLoad = LanguagePreference.getString("Language", null); 
    Locale locale =null; 
    if(languageToLoad!=null && languageToLoad.equals("english")) 
     locale =new Locale("en", "US"); 
    else if(languageToLoad!=null && languageToLoad.equals("tamil")) 
     locale =new Locale("ta", "IN"); 
    else 
     locale =new Locale("en", "US"); 
    Locale.setDefault(locale); 
    Configuration config = new Configuration(); 
    config.setLocale(locale); 
    getBaseContext().getResources().updateConfiguration(config, 
      getBaseContext().getResources().getDisplayMetrics()); 
+0

我不能使用它,因为'config.locale'已被弃用。它可能会导致高API的问题。 –

+0

看到我更新的帖子。你可以使用'config.setLocale'而不是'config.locale' – Raja

+0

'config.setLocale'也有问题。它需要API 17和更高版本。我需要一个适用于API 15及更高版本的方法。 –

@Override 
protected void attachBaseContext(Context newBase) { 
super.attachBaseContext(MyContextWrapper.wrap 
(newBase,languageFromPrefs)); 
} 

这应该是通过在项目中的每一次活动扩展基地的活动来完成。

+0

我用这段代码告诉我,当我运行我的应用程序时,我看到一个奇怪的场景。第一次运行时,app语言是fa(波斯语),下次它会自动更改为en(英文)! –