2012-06-03 55 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); 

當前的語言環境沒有改變,代碼這是關鍵。

+0

+1的代碼! –

+6

您的意思是:「cENG = resources.getStringArray(R.array.cities);」 ? – xVir

+2

它改變了上下文的語言環境(例如,針對您的活動) –

0

在Java 7(所以不是機器人)區域設置可以爲不同格式的資源設置和顯示不同:

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

相關問題