The i18n Cookbook - recipies for a global society

  • java cookbook
  • about the author
Home

Java Cookbook

  • Java Internationalization Cookbook

Locale

Create an ICU4J ULocale

Problem:

You want more options and power than provided by a core Java locale.

Solution:

ICU4J provides a locale class that provides significantly more capabilites than the Locale class in core Java.  ULocale is the foundation of all internationalization classes in ICU4J.

 

ULocale is defined with at minimum a language code.  It can also contain script name, region code, and other locale specific meta data such as calendar type.

 

To get an English ULocale:

  • icu4j
  • Java
  • Locale
  • ulocale
  • Add new comment
  • Read more

Get localized display names

Problem:

You want to retrieve a translated (localized) display name for a language, region, or locale.

Solution:

The Locale class contains methods to retrieve the localized display names for languages and countries. To retrieve a display name use the getDisplayLanguage, getDisplayCountry, and getDisplayName methods.

To get the display name for the language, country or locale using the system locale:

Locale[] locales = Locale.getAvailableLocales();

  • Locale
  • Add new comment
  • Read more

Get all ISO language and country codes

Problem:

You want to retrieve all ISO639 language codes or ISO3166 region codes.

Solution:

You can retrieve both ISO 639 language codes and ISO 3166 country codes by calling static methods on the Locale class.

To retrieve the ISO 639 language codes:

String[] locales = Locale.getISOLanguages();

for(int x = 0; x < locales.length; x++){

  System.out.println(locales[x]);

}

 

  • Locale
  • Add new comment
  • Read more

How to get an array of available lLocales

Problem:

You want to list all locales your system supports.

Solution:

To get an array of all installed locales you can use the static getAvailableLocales method. This retrieves the full locales, and not only their ids.

To loop through available locales and output their ids:

Locale[] locales = Locale.getAvailableLocales();

for(int x = 0; x < locales.length; x++){

System.out.println(locales[x].toString());

}

  • Locale
  • Add new comment

How to use a static constant to retrieve a common locale

Problem:

You want to use a common locale without having to create a new locale object.

Solution:

Many common locales are stored in static constants within the Locale class for convenience. To retrieve one simply use the locale name attached to the class name. To get a Japanese locale: Locale myLocale = Locale.JAPANESE; To get a Locale for the country of Japan: Locale myLocale = Locale.JAPAN; To get a simplified Chinese Locale: Locale myLocale = Locale.SIMPLIFIED_CHINESE; The available constants as of 1.5 are:

  • Locale
  • static locale
  • Read more

How to make a Locale object using language and country arguments

Problem:

You want to create a representation of your user's language and country.

Solution:

A Java locale represents a unique language and country combination. You create a Locale by specifying an ISO 639 language code and ISO 3166-2 country code in the constructor.

To create a Locale representing the English language:

Locale locale = new Locale("en");

 

To create a Locale representing English as spoken in Great Britain:

  • Java
  • Locale
  • Add new comment
  • Read more

Locales

The concept of the locale is central to internationalization. It is the foundation for all other aspects of software internationalization.  At its most basic a locale represents the language of a user.  This is handled by combining a language and country code.
 
Java uses ISO 639 codes for languages and ISO 3166-2 codes for country.  ICU4J combines the two codes with a potential script name and appends additional data to the end after an '@'.  That additional data could refer to a calendar type or collation order, etc...
 

  • icu4j
  • iso 3166
  • iso 639
  • Java
  • Locale
  • Add new comment
  • Read more

If you are testing any of these recipes in Eclipse and the characters are not displaying correctly in your console visit http://i18ncookbook.com/eclipse_settings.

This site is ad supported.  I hope you find something among our sponsors worth clicking. ;)

i18n search

Google
Custom Search
Syndicate content

Search

Tags in Tags

calendar date icu4j Java Locale number format numberformat parse spellout timezone transliteration transliterator
more tags

User login

  • Create new account
  • Request new password
  • java cookbook
  • about the author