Hector Ricardo
Hector Ricardo

Reputation: 559

How to know if Android resolved to use a localized strings.xml file or the default one?

The motivation of this question comes from this other question:

How do I get the current language my app is in? (Not the device's language as specified in the Settings. I want the language that Android resolved to use for my app).

This question has been asked several times on the site, but it fails to consider this corner-case:

Imagine the user has only one preferred language in his device: say German, for example.

My app two strings.xml files: The default one, and one in French (values-fr/strings.xml).

Obviously, Android will resolve to use the default strings.xml in this case.

But if I do any of the following, it will return German:

Locale.getDefault()
getResources().getConfiguration().getLocales().get(0)
getResources().getConfiguration().locale.
(And many other suggestions that I have found on the site)

And who told Android that the default strings.xml file was in German? Why did it made that assumption? The default file could be in Spanish, Italian, Polish...whatever.

Ideally, I would like a method that returns null in this case. Some method that tells me that no match was found for German and Android had to fall-back to the default strings.xml.

Does such method exist?

Upvotes: 0

Views: 478

Answers (2)

Rediska
Rediska

Reputation: 1470

Put the language name in both strings.xml files. For example, as languageName. When you get the string for R.string.languageName, it will be the language chosen by Android among the ones you support.

Upvotes: 2

Gabe Sechan
Gabe Sechan

Reputation: 93688

Those functions all return the phone's locale. They have nothing to do with resource localization. So nobody said the strings.xml file was German. The user set the phone to German, and the resource subsystem decided strings.xml was the best match for that. Basically you have the way it works backwards.

I don't think there is a way to get what you want for two reasons:

1)It's supposed to be transparent to the programmer.

2)It doesn't pick one file over the other. It picks independently for each string. So if you have a strings.xml with two strings A and B, and had a german strings file with only A, it would give you the german A and the default B.

Upvotes: 0

Related Questions