1

Example: I want to get to get string "about_message" from "en"'s locale file (strings.xml) but currently using locale "de" inside the app and when referencing (R.string.about_message) returns the de string value obviously.

Is there a method to do this?

Louis Ferraiolo
  • 409
  • 4
  • 18
  • 3
    Possible duplicate of [Load language specific string from resource?](https://stackoverflow.com/questions/5244889/load-language-specific-string-from-resource) – akhilesh0707 Sep 21 '17 at 10:37
  • Yes, you can check out this answer here: https://stackoverflow.com/questions/9475589/how-to-get-string-from-different-locales-in-android – Peter Sep 21 '17 at 10:45
  • you can also try: https://stackoverflow.com/a/45351072/7612991 – Stuti Kasliwal Sep 21 '17 at 13:33

1 Answers1

1

Let's assume you have a <string name="hello">Hello</string> inside your values/strings.xml, which also has a translation (say French) inside values-fr/strings.xml <string name="hello">Bonjour</string>. Normally you'd do the following:

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

To get the "Bonjor" string you'd have to create an alternative resources instance and use it to access the French string by changing to appropirate Locale:

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"

Hope this helps.

Sergey Emeliyanov
  • 5,158
  • 6
  • 29
  • 52