Is there a way to use two locales simultaneously in an app? - android

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.

Related

How do I programmatically validate if a given locale, say wx_rYZ, is supported by my app or not?

My android app has below values directories:
Now, if I set my device language (via Settings) to wx_rYZ then I would like to know if my app supports this locale or not.
At the same time I would like to keep the default values folder as is with all the translations.
Get the current locale
String current = getResources().getConfiguration().locale.toString();
You will have to maintain a hashSet of locales that your application support. You can than check if the current locale is present in the hashSet to know if it is supported or not.

How to stop user interface from changing when the language of phone is changed android

I'm using a UI where the location of edittexts for me is important. But when I try the app in a phone with Persian language as the phone language, the user interface changes automatically. I want my UI to be static on every language, what should i do? I am using relative layout.
Your application shall not be affected with language change unless you have defined different layouts or strings in language specific folders.
Read through this carefully and make sure you have everything under default values folder
https://developer.android.com/guide/topics/resources/localization.html

Android programmatically check if app is localized for a language

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

Run different functions depending on phone language

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

internationalisation in android

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.

Categories

Resources