私のアプリケーションには2つのロケールがあります。現在のロケールを変更することなく、異なるロケールの文字列配列など、リソースにアクセスできますか? コーディングでは、「設定」で変更したくないということです。異なるロケールアンドロイドのリソースにアクセスできますか?
答えて
はい、できます。新しいResources
オブジェクトを作成して、意図するものを指定する必要があります。Configuration
は切り抜いたロケールから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において
は(そうアンドロイドない)ロケール形式リソースの異なる設定および表示のために異なることができる。
Locale.setDefault(DISPLAY, Locale.PL);
Locale.setDefault(FORMAT, Locale.US);
よりよい解決策は、(あなたが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;
}
偉大な答え、ありがとうございます。私はこれをバックポートして、 'AssetManager'をつぶす努力を省くことを願っています – brandall
ありがとう!これはGoogleが推奨する解決策でもあります:https://code.google.com/p/android/issues/detail?id=67672 – phreakhead
1のコードのために! –
あなたは「cENG = resources.getStringArray(R.array.cities);」という意味ですか? ? – xVir
コンテキストのロケールを変更します(たとえば、あなたのアクティビティ用) –