Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get locale with region?

Tags:

android

I am trying to get the current device locale with the region like "en_us","en_gb".

I am calling Locale.getDefault().getLanguage() and it returns only the two letters code en.

like image 529
user1940676 Avatar asked Aug 25 '14 10:08

user1940676


People also ask

How do I check my current locale?

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.

What is locale default?

When the Java virtual machine starts running on a computer, it creates an object called the default locale. This object affects the format of strings created from data. Depending on the default locale, a program creates different strings for the same number.

What is device locale?

Device locale - Android Tutorial En indicates English and US indicates that it's English specifically spoken in the United States. Locales are used on Android to tailor the app to that specific region.

How do I get current region in C#?

WriteLine("DisplayName:" + regionInfo. DisplayName); Console. WriteLine("NativeName:" + regionInfo. NativeName); Console.


2 Answers

Format like "en_us" or "en_gb" has "language code"_"country code"

A Locale object contains both country code and language code.

So you can use below snippet to format your own code..

String cCode = Locale.getDefault().getCountry();
String lCode = Locale.getDefault().getLanguage();
String code = lCode+"_"+cCode;

or

you can use toString() method on Locale object to get the data

String code = Locale.getDefault().toString();
like image 169
Andromeda Avatar answered Sep 17 '22 21:09

Andromeda


It's worth pointing out that locale codes and language tags are related but different. Locale codes have an underscore separator (e.g. fr_CA), and language tags have a dash separator (e.g. fr-ca). I'm sure there are some deeper differences but that's beyond my pay grade.

This answer gives the result of various methods on the Locale class: https://stackoverflow.com/a/23168383

It looks like you want the toString() method (to get the locale code) or the toLanguageTag() (to get the language tag).

like image 41
Frank Schmitt Avatar answered Sep 18 '22 21:09

Frank Schmitt