Locale.getDefault() vs Locale.English main Difference android kotlin - android

I am working on android application my application running across the world so I my using local times of all around the world to calculate times of daily activities. The problem which i am facing is that when i want to get time of local country like for example when i use Locale.getDefault() it some times returns values in user's device language for example if a user have a device language Arabic it will return me those values in Arabic format. my logic is design only for English numeric characters when language changes it gives me exception of number format. there is another option which is Locale.English it returns me values in English I want to Ask that if i use Locale.english instead of Locale.getDefault() is there any thing wrong in it or I am doing it in a currect way?

Locale.getDefault().toString() and context.getResources().getConfiguration().locale.toString()
should return the same value.
The only major difference between the two is that the
Locale.getDefault() can be directly overridden by Locale.setDefault(locale). (Which will also affect context.getResources().getConfiguration().locale.toString())

Related

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

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.

How do I localize schedule sting in Android

I have a set of strings like "Friday 8:00am till 6:00pm", how do I localize this string to different languages.
I've tried to use 2 Calendars and SimpleDateFormat, but it doesn't support several values.
Use DateUtils.formatDateRange(...) to format time spans.
Formats a date or a time range according to the local conventions.
Although this will not allow you to localize parts of it yourself, it will provide a consistent user experience on the device and is properly localized. You can modify what and how it is displayed by providing various flags.

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

Android get system locale different approaches

So, I found out that there are two approaches to get system's locale in Android:
Locale.getDefault()
And
getResources().getConfiguration().locale
Basically question is - can output differ for these two? Or are they always the same and I can use the first one, since it does not require Context object?
Quoted from this page:
Be wary of the default locale
Note that there are many convenience methods that automatically use the default locale, but using them may lead to subtle bugs.
The default locale is appropriate for tasks that involve presenting data to the user. In this case, you want to use the user's date/time formats, number formats, rules for conversion to lowercase, and so on. In this case, it's safe to use the convenience methods.
The default locale is not appropriate for machine-readable output. The best choice there is usually Locale.US – this locale is guaranteed to be available on all devices, and the fact that it has no surprising special cases and is frequently used (especially for computer-computer communication) means that it tends to be the most efficient choice too.
A common mistake is to implicitly use the default locale when producing output meant to be machine-readable. This tends to work on the developer's test devices (especially because so many developers use en_US), but fails when run on a device whose user is in a more complex locale.
For example, if you're formatting integers some locales will use non-ASCII decimal digits. As another example, if you're formatting floating-point numbers some locales will use ',' as the decimal point and '.' for digit grouping. That's correct for human-readable output, but likely to cause problems if presented to another computer (parseDouble(String) can't parse such a number, for example). You should also be wary of the toLowerCase() and toUpperCase() overloads that don't take a Locale: in Turkey, for example, the characters 'i' and 'I' won't be converted to 'I' and 'i'. This is the correct behavior for Turkish text (such as user input), but inappropriate for, say, HTTP headers.
Locale current = getResources().getConfiguration().locale;
You may find that this value is updated more quickly after a settings change if that is necessary for your application.
Which is not in your another method
AFAIK, you can use either, unless your app itself tampers with locale using one way but not the other (Locale.setDefault() but not config.locale or vice versa)
Here's why:
ResourcesManager.applyConfigurationToResourcesLocked() #275:
public final boolean applyConfigurationToResourcesLocked(Configuration config,
CompatibilityInfo compat)
...
// set it for java, this also affects newly created Resources
if (config.locale != null) {
Locale.setDefault(config.locale);
}
Resources.updateSystemConfiguration(config, defaultDisplayMetrics, compat);
ApplicationPackageManager.configurationChanged();
...

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

Categories

Resources