2012-06-03 11 views

答えて

9

は切り抜いたロケールからCMKは、現在のロケールからの文字列の配列で、CENは、文字列配列の場合は私のために働くのコード

cMK = getResources().getStringArray(R.array.cities); 

     Configuration confTmp =new Configuration(getResources().getConfiguration()); 

     confTmp.locale = new Locale("en"); 

     DisplayMetrics metrics = new DisplayMetrics(); 

     getWindowManager().getDefaultDisplay().getMetrics(metrics); 

     Resources resources = new Resources(getAssets(), metrics, confTmp); 

     /* get localized string */ 
     cENG = getResources().getStringArray(R.array.cities); 

現在のロケールを変更しないで、それがポイントだった。ジャワ7において

+0

1のコードのために! –

+6

あなたは「cENG = resources.getStringArray(R.array.cities);」という意味ですか? ? – xVir

+2

コンテキストのロケールを変更します(たとえば、あなたのアクティビティ用) –

0

は(そうアンドロイドない)ロケール形式リソースの異なる設定および表示のために異なることができる。

Locale.setDefault(DISPLAY, Locale.PL); 
Locale.setDefault(FORMAT, Locale.US); 

類似スレッド:Changing Locale within the app itselfを。

15

よりよい解決策は、(あなたがAPI 17にしている場合)、次のようになります。

@NonNull 
protected String getEnglishString() { 
    Configuration configuration = getEnglishConfiguration(); 

    return getContext().createConfigurationContext(configuration).getResources().getString(message); 
} 

@NonNull 
private Configuration getEnglishConfiguration() { 
    Configuration configuration = new Configuration(getContext().getResources().getConfiguration()); 
    configuration.setLocale(new Locale("en")); 
    return configuration; 
} 
+0

偉大な答え、ありがとうございます。私はこれをバックポートして、 'AssetManager'をつぶす努力を省くことを願っています – brandall

+0

ありがとう!これはGoogleが推奨する解決策でもあります:https://code.google.com/p/android/issues/detail?id=67672 – phreakhead