0

assume I have an app that is translated in German and English as default. It is now opened on a device with French locale. Since there is no French translation available, the app will fallback to English. How can I determine that language? I don't mean system language, but the actively displayed one. I found that post, which is a nice workaround but is there a better way?

I need to request data in the appropriate language from a server and it doesn't make sense to get that data in a different language than the app uses.

Further thoughts: Maybe there is no such method as it cannot be clearly defined what the display language is, as the language fallbacks are handled per string. E.g. what would be the display language if 50 percent of the strings are translated and 50 % will default to a fallback? However, it would be nice if there would be a method that at least tells us the first fallback language for which there is at least one translation.

Any tips, thoughts, experiences are welcome!

Edit: I need to get the displayed language programmatically

Community
  • 1
  • 1
iridic
  • 393
  • 2
  • 9

1 Answers1

0

If I remember correctly there is only one fallback: the string.xml file. That means that:

  • your app get device locale and search for strings in corresponding string.xml file
  • if one string is not present in the correct localized file, the app search for a corresponding string in the main string.xml file
  • if the entire localized file (corresponding to device locale) is not present, the app fallback directly to the main string.xml file

The language you are using in your app is not necessary equal to the device one. You can change locale for your app with this code

Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
android.content.res.Configuration conf = res.getConfiguration();
conf.locale = new Locale(lang.toLowerCase());
res.updateConfiguration(conf, dm);

So I think there is always only one active locale (the device one or the app one), then app can fallback only on main string.xml file if a string resource is not present for the active locale.

firegloves
  • 5,581
  • 2
  • 29
  • 50
  • actually there can be several fallbacks with Android 7, see here: https://developer.android.com/guide/topics/resources/multilingual-support.html – iridic Aug 26 '16 at 06:40
  • oh cool, i've never read of this improvement! interesting feature, thank you – firegloves Aug 26 '16 at 08:20