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):
values-en-rUS/strings.xml
values-en/strings.xml
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):
values-en-rUS/strings.xml
values-en/strings.xml
values-es-rES/strings.xml
values-es/strings.xml
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?
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
Related
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?
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.
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.
i.e. If a device is running in a particular language (Say Spanish) then how can I set the Spanish language(After translation) as the default language of the app without asking the user.
The app should initialize in the Spanish language in this case while installation and a user should be able to change the language from the setting page of the app.
You need to add all string.xml files in specific language values folder.
So when your user change mobile default language your app may take string values from that particular language values strings.xml file.
By Locale, you can get a currently selected language of a device. Please have a look below code to get the language.
Locale.getDefault().getDisplayLanguage();
Within my app, I let user change the language when they install the app. Lets say they choose "English" while installing the app. But after that if they change the language of the phone to "French" how should my app receive it and change its language?
Assuming you use strings.xml to manage your string resources, Android natively supports localization:
https://developer.android.com/preview/features/multilingual-support.html
The idea is that you have distinct strings and values for each language you want to manage, then when the user changes the language in settings the app will automatically choose the correct file (or fall back to the default strings.xml if you haven't defined resources for the chosen language).
The app will automatically load its respective language strings files from the res/values directory after the application restarts. You could persist and compare Locale.getDefault().getLanguage() to check if the language has changed since the last restart.
It will try to get information about french in values (value-fr) if you didnt add the translations it will keep showing english
in my case the application will be installed on a single device and will be used by different users to login and use the app, problem here is that the users are of different languages you can say some are of french language and some are English language so i decided to do this by putting two strings.xml files, one in "Values-fr" for french ones and one in "Values" means if the person is french then use french resources otherwise default will be English resources. as this will not work because device is single and it'll be irritating if french one have to change device language to french to see app in french and then when English user came he needs again to change the device local language to English to see app in English. so language prefrences are stored in database for each user. i just want here that the selection of localized resources i.e the selection between "Values" and "Values-fr" should be done by my decision not by android deciding on the basis of device local language. is this possible?
You can try changing the locale from within the app so that the proper values are assigned and restore it when quitting. This is rather a crude solution to your problem.
This link could be useful in your quest.
I am making an app in which I want to implement internationalization.
I have created alternative resources like
res/values-fr/strings.xml
which Contains French text for all the strings, including title
Can anyone tell me what to do next...
thanks
You should always have default strings in res/values/strings.xml, because Android tries to use the most specific resource available. If you have for example res/values-fr/strings.xml and res/values-de/strings.xml and the users phone is set to English, your app will crash because neither de nor fr are applicable for English there are no fallback resources.
After you have specified your default strings and any translations in their respective subfolders, you can use the strings by their qualifiers. For example R.string.some_string. Android will then use the most appropriate translation that is available for the users current device language.
All that and more is explained here: Localizing with Resources
Device will load locale automatically based on system languge. No extra steps required unless you want to change locale in your app independently.