Localization is a big issue in current software development.
In Java API, Java provides a Locale class and predefines some Locale constants such as Locale.US, Locale.English for convenience to cope with localization issue. But how about the Locale that Java does not predefine, like PORTUGAL, PORTUGUESE? Do not worry, you can use Locale constructor to construct customized Locale.
In Locale class, there are three constructors:
- Code: Select all
Locale(String language)
Locale(String language, String country)
Locale(String language, String country, String variant)
Let's see the third one, then you will know the former two constructors.
The first parameter is language code. It is a lowercase two-letter ISO-639 code. You can find it in http://www.loc.gov/standards/iso639-2/englangn.html.
The second parameter is country code. It is a uppercase two-letter ISO-3166 code. You can find it in http://www.iso.ch/iso/en/prods-services ... t-en1.html.
The third parameter is vendor and browser specific code. For example, use WIN for Windows, MAC for Macintosh, and POSIX for POSIX. Where there are two variants, separate them with an underscore, and put the most important one first. For example, a Traditional Spanish collation might construct a locale with parameters for language, country and variant as: "es", "ES", "Traditional_WIN".
The following is an example of Locale.
Test.java
- Code: Select all
public class Test {
static Locale PORTUGAL = new Locale("pt","PT");
public static void main(String[] args) {
String s = (PORTUGAL).getDisplayLanguage();
System.out.println(s);
Currency curr = Currency.getInstance(PORTUGAL);
System.out.println("Portugal: " + curr.getSymbol());
}
}
We construct a Locale named PORTUGAL which is not predefined in Locale class as a constant variable.
Then we can use this PORTUGAL locale in other places.
