使用例:ユーザーに表示されるエラーメッセージを記録します。Android:現在のロケールを変更せずに特定のロケールで文字列を取得する方法
ただし、ユーザーのデバイスのロケールに依存するメッセージはログに記録したくありません。 一方、(技術的な)ロギングのためだけに、ユーザーのデバイスのロケールを変更する必要はありません。 これを達成できますか? 私はstackoverflowの上、ここで潜在的な解決策のカップルが見つかりました:
- how to get string from different locales in Android?
- Can I access to resources from different locale android?
- android - Get string from default locale using string in specific locale
- 多分android - Changing Locale within the app itself
は、しかし、彼らはすべての私のロケールを変更することになり(次の構成変更まで)。
とにかく、私の現在の回避策はそうのようなものです:正直に言うと
public String getStringInDefaultLocale(int resId) {
Resources currentResources = getResources();
AssetManager assets = currentResources.getAssets();
DisplayMetrics metrics = currentResources.getDisplayMetrics();
Configuration config = new Configuration(
currentResources.getConfiguration());
config.locale = DEFAULT_LOCALE;
/*
* Note: This (temporiarily) changes the devices locale! TODO find a
* better way to get the string in the specific locale
*/
Resources defaultLocaleResources = new Resources(assets, metrics,
config);
String string = defaultLocaleResources.getString(resId);
// Restore device-specific locale
new Resources(assets, metrics, currentResources.getConfiguration());
return string;
}
、私はすべてこの方法を好きではありません。それは効率的ではなく、並行性とそのすべてを考えています - それは "間違った"ロケールで何らかのビューをもたらすかもしれません。
だから - どのようなアイデアですか?これは、標準のJavaの場合と同じように、ResourceBundleを使用して達成できますか?
こんにちは、あなたはどんな解決策を得たのですか?私もほとんど同じユースケースを持っています。 – Nitish
いいえ、私はまだ質問 – schnatterer
に記載された回避策を使用していますが、ロギング目的に特定の英語ベースの文字列を使用しない理由は何ですか。 R.string.dev_error_clicking_button_b。他の言語に翻訳していない場合は、デフォルトでこれを取得します。 –