Java locale string to locale

Creating a Locale

There are several ways to create a Locale object. Regardless of the technique used, creation can be as simple as specifying the language code. However, you can further distinguish the locale by setting the region (also referred to as «country») and variant codes. If you are using the JDK 7 release or later, you can also specify the script code and Unicode locale extensions.

The four ways to create a Locale object are:

Version Note: The Locale.Builder class and the forLanguageTag method were added in the Java SE 7 release.

LocaleBuilder Class

The Locale.Builder utility class can be used to construct a Locale object that conforms to the IETF BCP 47 syntax. For example, to specify the French language and the country of Canada, you could invoke the Locale.Builder constructor and then chain the setter methods as follows:

Locale aLocale = new Locale.Builder().setLanguage("fr").setRegion("CA").build();

The next example creates Locale objects for the English language in the United States and Great Britain:

Locale bLocale = new Locale.Builder().setLanguage("en").setRegion("US").build(); Locale cLocale = new Locale.Builder().setLanguage("en").setRegion("GB").build();

The final example creates a Locale object for the Russian language:

Locale dLocale = new Locale.Builder().setLanguage("ru").setScript("Cyrl").build();

Locale Constructors

There are three constructors available in the Locale class for creating a Locale object:

The following examples create Locale objects for the French language in Canada, the English language in the U.S. and Great Britain, and the Russian language.

aLocale = new Locale("fr", "CA"); bLocale = new Locale("en", "US"); cLocale = new Locale("en", "GB"); dLocale = new Locale("ru");

It is not possible to set a script code on a Locale object in a release earlier than JDK 7.

forLanguageTag Factory Method

If you have a language tag string that conforms to the IETF BCP 47 standard, you can use the forLanguageTag(String) factory method, which was introduced in the Java SE 7 release. For example:

Locale aLocale = Locale.forLanguageTag("en-US"); Locale bLocale = Locale.forLanguageTag("ja-JP-u-ca-japanese");

Locale Constants

For your convenience the Locale class provides constants for some languages and countries. For example:

cLocale = Locale.JAPAN; dLocale = Locale.CANADA_FRENCH;

When you specify a language constant, the region portion of the Locale is undefined. The next three statements create equivalent Locale objects:

j1Locale = Locale.JAPANESE; j2Locale = new Locale.Builder().setLanguage("ja").build(); j3Locale = new Locale("ja");

The Locale objects created by the following three statements are also equivalent:

j4Locale = Locale.JAPAN; j5Locale = new Locale.Builder().setLanguage("ja").setRegion("JP").build(); j6Locale = new Locale("ja", "JP");

Codes

The following sections discuss the language code and the optional script, region, and variant codes.

Language Code

The language code is either two or three lowercase letters that conform to the ISO 639 standard. You can find a full list of the ISO 639 codes at http://www.loc.gov/standards/iso639-2/php/code_list.php.

Читайте также:  Basic program in cpp

The following table lists a few of the language codes.

Sample Language Codes

Language Code Description
de German
en English
fr French
ru Russian
ja Japanese
jv Javanese
ko Korean
zh Chinese

Script Code

The script code begins with an uppercase letter followed by three lowercase letters and conforms to the ISO 15924 standard. You can find a full list of the ISO 15924 codes at http://unicode.org/iso15924/iso15924-codes.html.

The following table lists a few of the script codes.

Sample Script Codes

Script Code Description
Arab Arabic
Cyrl Cyrillic
Kana Katakana
Latn Latin

There are three methods for retrieving the script information for a Locale :

  • getScript() – returns the 4-letter script code for a Locale object. If no script is defined for the locale, an empty string is returned.
  • getDisplayScript() – returns a name for the locale’s script that is appropriate for display to the user. If possible, the name will be localized for the default locale. So, for example, if the script code is «Latn,» the display script name returned would be the string «Latin» for an English language locale.
  • getDisplayScript(Locale) – returns the display name of the specified Locale localized, if possible, for the locale.

Region Code

The region (country) code consists of either two or three uppercase letters that conform to the ISO 3166 standard, or three numbers that conform to the UN M.49 standard. A copy of the codes can be found at http://www.chemie.fu-berlin.de/diverse/doc/ISO_3166.html.

The following table contains several sample country and region codes.

Sample Region Codes

A-2 Code A-3 Code Numeric Code Description
AU AUS 036 Australia
BR BRA 076 Brazil
CA CAN 124 Canada
CN CHN 156 China
DE DEU 276 Germany
FR FRA 250 France
IN IND 356 India
RU RUS 643 Russian Federation
US USA 840 United States

Variant Code

The optional variant code can be used to further distinguish your Locale . For example, the variant code can be used to indicate dialectical differences that are not covered by the region code.

Version Note: Prior to the Java SE 7 release, the variant code was sometimes used to identify differences that were not specific to the language or region. For example, it might have been used to identify differences between computing platforms, such as Windows or UNIX. Under the IETF BCP 47 standard, this use is discouraged.

To define non-language-specific variations relevant to your environment, use the extensions mechanism, as explained in BCP 47 Extensions.

As of the Java SE 7 release, which conforms to the IETF BCP 47 standard, the variant code is used specifically to indicate additional variations that define a language or its dialects. The IETF BCP 47 standard imposes syntactic restrictions on the variant subtag. You can see a list of variant codes (search for variant) at http://www.iana.org/assignments/language-subtag-registry.

Читайте также:  Setting Font Size

For example, Java SE uses the variant code to support the Thai language. By convention, a NumberFormat object for the th and th_TH locales will use common Arabic digit shapes, or Arabic numerals, to format Thai numbers. However, a NumberFormat for the th_TH_TH locale uses Thai digit shapes. The excerpt from ThaiDigits.java demonstrates this:

String outputString = new String(); Locale[] thaiLocale = < new Locale("th"), new Locale("th", "TH"), new Locale("th", "TH", "TH") >; for (Locale locale : thaiLocale)

The following is a screenshot of this sample:

Источник

How to convert a string to another locale in Java?

Java provides several ways to convert a String to another locale. Here are two common ways:

Method 1: Using the String.toLowerCase() method

This class is used to represent a specific geographical, political, or cultural region.

For example, to convert a String to German locale, you can create a Locale object for Germany by using the following code:

Locale germanLocale = new Locale("de", "DE");

The first argument of the Locale constructor is the language code (in this case «de» for German) and the second argument is the country code (in this case «DE» for Germany).

For example, to convert a String «Hello World» to German locale, you can use the following code:

String inputString = "Hello World"; String germanString = inputString.toLowerCase(germanLocale);

The toLowerCase() method takes the Locale object as an argument and returns the converted String.

Method 2: Using the String.toLowerCase(Locale.Category, Locale) method

  • Step 1 — Import the java.util.Locale class.
  • Step 2 — Create a Locale object for the desired locale

For example, to convert a String to German locale, you can create a Locale object for Germany by using the following code:

Locale germanLocale = new Locale("de", "DE");
  • Step 3 — Use the toLowerCase(Locale.Category, Locale) method to convert the String to the desired locale

For example, to convert a String «Hello World» to German locale, you can use the following code:

String inputString = "Hello World"; String germanString = inputString.toLowerCase(Locale.Category.FORMAT, germanLocale);

The toLowerCase() method takes two arguments: the first argument is a Locale.Category, which specify whether the operation should apply to the display names, formatting data, or both, and the second argument is a Locale.

It’s important to keep in mind that the string representation of lower case characters may be different between different Locale, because different languages have different lowercase representation.

This is the simple way to convert the string to desired locale using inbuilt function, You can also do this using external libraries like ICU4J or LOCALIZER.

Another way to convert a String to another locale in Java is by using the NumberFormat class from the java.text package. This class allows you to format numbers, currencies, and percentages according to the conventions of a specific locale.

Method 3: Using NumberFormat

  • Step 1 — Import the java.text.NumberFormat class.
  • Step 2 — Create a NumberFormat object for the desired locale

For example, to format a number according to the conventions of the German locale, you can use the following code:

NumberFormat numberFormat = NumberFormat.getInstance(new Locale("de", "DE"));
  • Step 3 — Use the format() method to format a number according to the conventions of the desired locale
Читайте также:  What version java runtime

For example, to format the number 12345.67 as a currency value in German locale, you can use the following code:

double value = 12345.67; String germanCurrency = numberFormat.format(value);

The format() method takes a double value and returns the formatted currency string.

Keep in mind, When you work with the number format , date format , it’s always good practice to use specific classes like NumberFormat.getCurrencyInstance , NumberFormat.getNumberInstance , DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT,locale)

Another way of converting a string to a locale is by using the Collator class from the java.text package. This class provides a way to compare strings according to the conventions of a specific locale and to perform locale-sensitive string operations such as sorting.

Method 4: Using Collator

  • Step 1 — Import the java.text.Collator class.
  • Step 2 — Create a Collator object for the desired locale

For example, to compare strings according to the conventions of the German locale, you can use the following code:

Collator collator = Collator.getInstance(new Locale("de", "DE"));
  • Step 3 — Use the compare() method to compare two strings according to the conventions of the desired locale

For example, to compare the strings «äpfel» and «Apfel» in German locale, you can use the following code:

int compareValue = collator.compare("äpfel", "Apfel");

The compare() method takes two strings as arguments and returns an int value that is less than, equal to, or greater than zero, depending on whether the first string is less than, equal to, or greater than the second string.

You can also use Collator class for sorting the string in specific locale order,

Hope this helps! Let me know if you have any other questions.

Conclusion

In conclusion, there are several ways to convert a String to another locale in Java. One way is to use the toLowerCase() method of the String class, which takes a Locale object as an argument and returns the converted string. Another way is to use the NumberFormat class from the java.text package, which allows you to format numbers, currencies, and percentages according to the conventions of a specific locale. The Collator class from the java.text package also provides a way to compare and sort strings according to the conventions of a specific locale. It’s important to choose the correct method based on the requirements of your project. Some methods are better suited for string manipulation and some for formatting. It is also important to keep in mind that the representation of a string can be different depending on the locale, it is important to consider which locale the application will target and make sure the methods used are adequate for that specific scenario.

Источник

Оцените статью