2017-09-16 3 views
0

共有環境設定でアプリケーション言語を変更するオプションを追加したかったのです。共有環境設定で「en」や「fa」のような言語を保存しました。コンテキストラッパーを使用して実行時にアプリケーション言語を変更するにはどうすればよいですか?

これで、言語として共有設定データを設定する必要があります。私はThis codeを使用しようとしましたが、いくつか問題がありました。このコードを使用するための

私は私の活動のコンテキストをラップする必要があります:私はgetString(Context context,String key,String defaultValue)と呼ばれる共有環境設定から言語を取得するための方法を書いて、その後にそのコードを編集した

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

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

それは良いようだが、私は私のアプリを実行したときに私は奇妙なシーンを見た。最初の実行時に、アプリケーション言語は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://stackoverflow.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://stackoverflow.com/questions/40221711/android-context-getresources-updateconfiguration-deprecated/40704077#40704077)と実際は同じです。 –

答えて

0

あなたは、これは、プロジェクト内のすべての活動によって拡張されたベースの活動で行う必要があり、実行時に変更言語の

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.locale'の代わりに' config.setLocale'を使うことができます – Raja

+0

'config.setLocale'にも問題があります。 API 17以上が必要です。 API 15以上のメソッドが必要です。 –

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

をこの方法を使用する必要があります。

+0

私はこのコードを使って私のアプリを走らせたところ、奇妙なシーンを見ました。最初の実行時に、アプリケーション言語はfa(ペルシャ語)で、次に自動的にen(英語)に変更されました。 –

関連する問題