5

I have an app that needs to provide strings localized in the language of another device. As soon as my app knows the language of the other device it creates a new Resources class. Similar to this question.

The resource object just created can fall back when getting strings if the language is not supported (assume other device is set to en_US):

  1. values-en-rUS/strings.xml
  2. values-en/strings.xml
  3. values/strings.xml

Instead of the third fallback I want to fall back to the language of my own device (assume my device is set to es_ES it would then be):

  1. values-en-rUS/strings.xml
  2. values-en/strings.xml
  3. values-es-rES/strings.xml
  4. values-es/strings.xml
  5. values/strings.xml

I could achieve that very easy if I could programmatically check if a language/region is supported by my app. Found this AssetManager.getLocales() but think it doesn't help.

Is there a way to achieve the desired fallback or a way to check if the app is localized in a specific language/region?

Community
  • 1
  • 1
Kujta
  • 51
  • 1
  • 3
  • "or a way to check if the app is localized in a specific language/region?" -- since you are the one writing the app, and you know what languages you have localized it to, just encode that information in your app somewhere. – CommonsWare Jan 14 '16 at 19:24
  • Thats right but exactly what I want to avoid. This code should run on different apps with different set of supported languages. If this could be done programmatically it would reduce maintenance and risk of fail. – Kujta Jan 14 '16 at 19:29

1 Answers1

-1

See here. Tested it and got the correct device locale as a two sign string. Code is:

String Language = Locale.getDefault().getLanguage() //returns eg. "de" for german
Community
  • 1
  • 1
chris-pollux
  • 309
  • 2
  • 7
  • Knowing the device locale doesn't help to get the apps localizations. It may not match. – Kujta Jan 14 '16 at 20:47
  • Oh right, I misunderstood your question, sorry. If it is your app, you could try to save information about the available localizations in SharedPreferences for example. So every time you create a new Resources class you save the new localozation in a string. – chris-pollux Jan 14 '16 at 21:25
  • Slowly I believe that's the only way. CommonsWare mentioned the same. Sad that I cannot even check subfolders of the res folder. – Kujta Jan 14 '16 at 22:40