2017-09-21 5 views
1

から文字列を取得する例: "en"のロケールファイル(strings.xml)から文字列 "about_message"を取得したいが、現在アプリケーション内でロケール "de"参照(R.string.about_message)はストリング値を明らかに返します。別のロケールのstring.xmlファイル

これを行う方法はありますか?

+3

[リソースから言語固有の文字列をロードする](https://stackoverflow.com/questions/5244889/load-language-specific-string-from-resource)の可能な複製 – akhilesh0707

+0

はい、あなたはこの回答をチェックアウトすることができますhttps://stackoverflow.com/questions/9475589/how-to-get-string-from-different-locales-in-android – Peter

+0

次の方法もお試しください:https://stackoverflow.com/a/45351072/7612991 –

答えて

1

values/strings.xmlの中に<string name="hello">Hello</string>という文字列があり、値-fr/strings.xml <string name="hello">Bonjour</string>の中に翻訳(フランス語など)があるとします。

String s = getResources.getString(R.string.hello); // s: "Hello" 

あなたは、代替資源のインスタンスを作成し、ロケールをappropirateするために変更することにより、フランス語の文字列にアクセスするためにそれを使用する必要があるだろう「Bonjor」の文字列を取得するには:通常は、次の操作を行いたい

Resources normalResources = getResources(); 
AssetManager assets = normalResources.getAssets(); 
DisplayMetrics metrics = normalResources.getDisplayMetrics(); 
Configuration config = new Configuration(standardResources.getConfiguration()); 
config.locale = Locale.FRENCH; 
Resources frenchResources = new Resources(assets, metrics, config); 

String s = defaultResources.getString(R.string.hello); // s: "Bonjour" 

これが役に立ちます。

関連する問題