0

I would like to read the string.xml file, in full, located under : src -> main -> res -> values.

For the sake of the argument, i need the specific file in order to handle some localization issues which might occur in my application : say the selected device language is french, but still, i want some values from the main english string.xml file only for specific scenarios. So, all the resources in the values/strings.xml should exists on values-fr/strings.xml, and removing them from values-fr IS NOT AN OPTION.

  • Obviously, a solution of the sort of getString(R.string.text_name) is not acceptable since we'll receive the value of text_name from values-fr/strings.xml file, and not from values/strings.xml.
  • This won't help as well :

    getResources().getIdentifier("text_name", "string", getPackageName()); 
    
  • This won't help as well, since string.xml is not a raw asset. context.getResources().openRawResource(R.raw.test) context.getAssets().open("values/string.xml");

This is what i've tried, but none gave me the file i'm looking for :

  1. using uri :

    Uri.parse("android.resource://" + context.getPackageName() + resource_id_for_string_file);
    
  2. using specific file path, something like :

    File file = new File("/src/res/values/strings.xml");
    File file = new File("/res/values/strings.xml");
    
  3. used this post (class.getClass().getResourceAsStream(...)) which didn't work either.

I honestly though it'll be pretty easy, but i can't find anywhere for a solution for this issue.

Community
  • 1
  • 1
Dus
  • 4,052
  • 5
  • 30
  • 49
  • you can still add the english values for certain keys in french file ?! – Yazan Nov 08 '16 at 13:17
  • your approach is not correct. If you want some values to be read from `values-fr/strings.xml`, and other - from `values/strings.xml`, then just don't add the values you need to be taken from `values/strings.xml` into `values-fr/strings.xml`. In case if there is no value for specific locale, the default locale will be used . – Vladyslav Matviienko Nov 08 '16 at 13:17
  • thanks, but it doesn't help me since i need all the resources in the values-fr strings as well (i want a specific scenario not to access that file and to go to values/strings.xml file) – Dus Nov 08 '16 at 13:21
  • 1
    if you have to do it, then write a method that sets Locale of app to say (FR) while current locale is `US` , then invokes `getString()` which will get the value from related file, and then revert back to the originally selected locale (before setting temp locale) this way you keep use same API methods no twists ?! – Yazan Nov 08 '16 at 13:26

2 Answers2

1

If you want some of the text in english language then i would suggest you to mention that text in english language only in french string.xml file. It will automatically take it in english language only from the french string.xml file. You don't have to read it explicitly. Or else don't add that values in your french string.xml file. It will directly refer it from the main string.xml file.

GrIsHu
  • 29,068
  • 10
  • 64
  • 102
  • 1
    thats not the case, i want to be able to control it in several cases, not in all, so i can't do what you're suggesting. – Dus Nov 08 '16 at 13:16
0

if you have to do it, then write a method that sets Locale of app to say (FR) while current locale is US , then invokes getString() which will get the value from related file, and then revert back to the originally selected locale (before setting temp locale) this way you keep use same API methods no twists

public String getString(int id, Locale fromLocale, Context ctx){
    final Locale oldDefaultLocale = Locale.getDefault();

    Locale.setDefault(fromLocale);
    Configuration config = getBaseContext().getResources().getConfiguration();
    final Locale oldConfigLocale = config.locale;
    config.locale = fromLocale;
    ctx.getResources().updateConfiguration(config,
            getBaseContext().getResources().getDisplayMetrics());
    final String result = ctx.getString(id);

    Locale.setDefault(oldDefaultLocale);
    config.locale = oldConfigLocale;
    ctx.getResources().updateConfiguration(config,
            getBaseContext().getResources().getDisplayMetrics());

    return result;
}

locale is changed/reverted instantly during the method execution it will not affect other views/resources... on the screen

if you place this in a parent activity/fragment, you don't have to pass Context ctx but if in a Util class or something, you need to pass it

Yazan
  • 6,074
  • 1
  • 19
  • 33