How to get the script from a Locale object on Android? - java

From what I see the Android Locale class doesn't mention anything about the script, only the language, country and variant.
Whereas the Java SE Locale class mentions language, country, script, variant and extensions, and of course has a method getScript() to get the script.
So, my question is: How to get the script from a Locale object on Android?
or is there another way to get the script?
I need this to be able to differentiate between Chinese Traditional (zho-Hant) and Chinese Simplified (zho-Hans), for that I need to be able to get the script from the Locale, e.g. Hant or Hans for the language zho
script =
ISO 15924 alpha-4 script code. You can find a full list of valid
script codes in the IANA Language Subtag Registry (search for "Type:
script"). The script field is case insensitive, but Locale always
canonicalizes to title case (the first letter is upper case and the
rest of the letters are lower case).
Well-formed script values have the form [a-zA-Z]{4}
Example: "Latn" (Latin), "Cyrl" (Cyrillic)

This feature is not built into the Android SDK. Instead, I wrote a version you can use, available here. Basically, it takes the table in the above answer and ports it to a Map<String, Map<String, String>> containing the useful information, then a simple lookup method is used. To use this class in your project, just call:
String script = LocaleUtilities.getScript(Locale.getDefault());
to get the script for the default locale.

Related

How to convert 3 letter language code to corresponding text?

Do we have any java libraries to convert 3 letter language code to its corresponding language with localization support?
Like,
ENG -> English
PS: I guess its a bad question. But, google was of not a good help. Hence, turning to you all. Probably, my search term was not accurate.
Use Locale's getDisplayLanguage() method:
Locale eng = Locale.forLanguageTag("ENG"); // Make a locale from language code
System.out.println(eng.getDisplayLanguage()); // Obtain language display name
Demo.
I do not know about a Java library but this might help.
https://www.loc.gov/standards/iso639-2/php/code_list.php
It has the data you are looking for. You might have to scrape it off the page and put it into your Java code.

Java Locale.getDefault() cannot return values other than en

there is some problem I encounter in Java, i want to get system locale country from a window computer, so I write the code like this:
Locale x = Locale.getDefault();
String output = x.getCountry();
If i set my system language to like English(Singapore), i will get result as en-SG, and if i set my system language as English(Canada), it will also return me the result with en-CA, but if i change to some language which is not english, it will return me as en-GB for all options, why is it so??
Besides that, is there any other way to get the current country information using java?
Locale.getDefault() should abstract from the operating system. That is the purpose.
I do not htink that this is a problem of Java. I just tried to change my language and it works.
My Setup was:
Windows 10 Home edition 64 Bit
Java 1.8.0_45
de_DE to en_GB
I had to download the language pack for en_GB.
I had to log off and login in to activate the other language.

About Locale class in Java

I'm using a TTS method on Android which takes a Locale instance as an argument. So I googled the class Locale and found some example code. But I don't understand what is different among usages below because I tested them all with a TTS method and it seems to work all the same to me.
Locale("ja")
Locale("ja_JP")
Locale("ja", "JP", "")
Locale.JAPAN
Locale.JAPANESE
Are there any differences?
The documentation for the Locale class describes this in (almost excruciating) detail. The valid language, country, and variant codes are described in ISO 639.
Here are the differences between the five examples you give:
ja simply describes the Japanese language with no country.
ja_JP specifies both the Japanese language and the country of Japan
The three parameter constructor splits off the language, country, and variant into separate arguments. Locale("ja", "JP", "") is equivalent to Locale("ja_JP") since no variant is provided.
Locale.JAPAN is a constant shortcut for ja_JP (the country of Japan).
Locale.JAPANESE is a constant shortcut for ja (the Japanese language).
What does this all mean? Well, it depends on where it is used. Locales are used with a number of different APIs, including the date-time APIs, text-to-speech APIs, and more.
In the context of text-to-speech, the locale can be used in a number of ways, such as:
Selecting the appropriate voice to use
Applying the proper inflection for certain words. Different locales may speak the same word in the same language differently.
Translating certain non-words into speech. For instance, different locales may speak numbers or fractions differently.
In general, you want to be as specific and accurate as possible when selecting a Locale.

Saving Locale in a Database

On my JSF 2.1 Application with JDK 7, I searched for a solution to provide my users the possibility to save their preferred language. So, on the next login the language should received from the Database and replaced by the language from the default (Browser).
My only problem is now, how to save the java.util.Locale in my database?
After hours of googling I found a new functionality inside of JDK 7 that is the "forLanguageTag Factory Method". This method returns a Locale and only needs for that a IETF BCP 47 standard string.
This sounds really simple and great for me. But how can I get this "IETF BCP 47 standard"-String from an existing locale? I looked in the API but found nothing that is comparable to the "IETF BCP 47 standard".
Consider Locale.toLanguageTag:
Returns a well-formed IETF BCP 47 language tag representing [a]
locale.
Example usage:
String expectedTag = "en-US";
Locale locale = Locale.forLanguageTag( expectedTag );
String actualTag = locale.toLanguageTag();
Assert.assertEquals( expectedTag, actualTag );
Note: There are some restrictions as mentioned in the javadocs.
If you're just interested in the language, simply use Locale.getLanguage() to transform a locale into a String, and new Locale(String language) to transform the String into a Locale.
If you want to store the whole Locale, use Locale.toString(), and a custom method that splits on _ to transform the string into the three parts of a Locale.

Getting the Locale short code

In Java it is easy enough to get the default Locale, but is there a way to get the short code that you see so often in websites such as lang=en or lang=zh
I need to send to information to a website but need the short code so the response will be in the correct language.
Locale.getDefault().getLanguage();
Use the Locale.getLanguage() method (API Link)
Returns the language code for this locale, which will either be the empty string or a lowercase ISO 639 code.
ISO-639 is the standard for two-letter language codes. See here for the list.

Categories