I have support for 3 languages in my app. And depending on what language the phone is set to use I want my app to run a function based on that.
You can determine this by looking for the devices Locale using the following syntax:
Locale current = getResources().getConfiguration().locale;
Generally best practice is not to use different code for different languages, just use different resources. However if you still need it you can use the Locale class.
If you want to get the selected language of your device:
Locale.getDefault().getDisplayLanguage();
If you're interested in just getting the ISO code (e.g. for if or switch statements) use:
Locale.getDefault().getISO3Language();
if you want change the App language use the file strings for each language supported
Related
I would like to use two locales for resource values in an app.
For example, I want some specific strings to depend on what country the user is in right now (like phone numbers), while the rest of the strings will depend on the phone's language/locale settings.
Is there a clean way of doing this without programatically getting a string resource from a different locale? Or should I be using resources in the first place?
You can set forcefully based on device locale and you will need to handle that pattern when locale check returns true for the particular locale. Also, check for a relevant pattern that needs to be set for example phone number.
When you want to add locale-specific resources in Android, you have to add the lowercase ISO-3166-1 code of the language to the resource folder's name. So far so good.
Now I want to add Catalan and Basque strings to my application. According to the ISO list, I would have to add values-ca and values-eu. But will that work, actually?
Edit: With a custom language chooser in your app, you can provide support for languages even when they're not in the device's settings menu. I've made the library that I use for this available on GitHub:
https://github.com/delight-im/Android-Languages
I don't know if Android supports all ISO codes and if these "minor" languages will be displayed at all. If Android is not available in these languages, perhaps the device will not even recognize this language as its default locale and just use es for Spanish.
Can someone help?
Many devices ship with a limited selection of languages in the settings menu, but can actually be set to any system locale using an app such as LocaleSwitch.
It should be pretty easy to test whether Basque is supported by adding a custom locale in LocaleSwitch and then adding the resource folder to see if the system loads the resources from this folder automatically.
Finally, Gingerbread 'support' for Catalan may only reference the inclusion of android.R values since 2.3, though I can't see them in the framework repository.
Actually not all languages are documented at Android docs and many brands did your custom version of Android which may change its list, but be sure that if a device has Catalan and/or Basque suports it will use ISO standard, and the values will got rigth as it is converted with string comparision.
Maybe it would be a good choice to put a handed changer if you got that most devices do not have this support on your tests.
The thing is, if the language is not listed in the language selection list on the device, the resources can never be used as the device can never be put in this configuration by the user.
On the other hand, if the device allows you to select Basque, it will be using this format, and your resources will be used.
We can now create our "values-eu-rES" folder and have our app in Basque, Android Lollipop has made it.
We can create a folder
Basque strings ->
value-eu [Basque]
another folder
Catalan strings ->
value-ca [Catalan]
Adding a new language in LocaleSwitch, you need to type the ISO 639-1 code. For instance, if you want to switch to basque you need to enter "eu". Afterwards, you will see the new locale as "vasco".
That makes me think that basque locale is supported by android.
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
Im using localization in my Android app. I change the localization inside the app, so it doesn't change the system settings. I create dirs with the name "values-nl" and "values-en". This works fine. Buts lets say I want to create my own language. When I do this and create my own folder like "values-test" is trows me an error.
this is how i set the localization inside the app:
Locale locale = new Locale("nl");
Locale.setDefault(locale);
Buts lets say I want to create my own language.
This is not possible, except via custom firmware. -test is not going to be a recognized resource set. You are welcome to support any of the languages that your desired version of Android supports -- you can find the list of these in the Locales section of the SDK release notes, such as these for Android 4.0.3.
Reputedly, you can use Locale to change what resource set will be used on the fly, though I have not tried this myself.
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.