Internationalization Notes
Internationalization Notes
Description
Locale
ResourceBundle
NumberFormat
DecimalFormat
DateFormat
Creating a Locale
Creating a java.util.Locale instance can be done in four different ways:
Locale constants
Locale constructors
Locale Constants
The java.util.Locale class contains a set of constants that represent the
most commonly used languages in the world. These are:
Locale.CANADA
Locale.CANADA_FRENCH
Locale.CHINA
Locale.CHINESE
Locale.ENGLISH
Locale.FRANCE
Locale.FRENCH
Locale.GERMAN
Locale.GERMANY
Locale.ITALIAN
Locale.ITALY
Locale.JAPAN
Locale.JAPANESE
Locale.KOREA
Locale.KOREAN
Locale.PRC
Locale.ROOT
Locale.SIMPLIFIED_CHINESE
Locale.TAIWAN
Locale.TRADITIONAL_CHINESE
Locale.UK
Locale.US
You use one of these constants simply by referring to it, like this:
Localelocale=Locale.JAPANESE;
Locale Constructors
You can also create a java.util.Locale instance by using one of its
constructors. The constructors are:
Locale(Stringlanguage)
Locale(Stringlanguage,Stringcountry)
Localelocale=newLocale("en");//Englishlanguage
Localelocale=newLocale("en","UK");//Englishlanguage,UnitedKingdom
Localelocale=newLocale("en","CA");//Englishlanguage,Canada
Locale Builder
From Java 7 you can use the Locale.Builder class to build
a Locale instance. Here is an example:
LocalecLocale=newLocale.Builder().setLanguage("en")
.setRegion("US").build();
Locale.forLanguageTag()
The factory method Locale.forLanguageTag() can also be used to
create a Localeinstance. Here is an example:
LocaleaLocale=Locale.forLanguageTag("enUS");
ResourceBundleresourceBundle=
ResourceBundle.getBundle("bundleName",locale);
Java ResourceBundle
The java.util.ResourceBundle class is used to store texts and
components that are locale sensitive.
ListResourceBundle.
The PropertyResourceBundle class stores localized texts in standard
Java property files.
You do not directly interact with these two subclasses. All interaction goes
through theResourceBundle class.
First you need a Locale instance. Then you pass that Locale instance to
theResourceBundle.getBundle() method along with the name of the
resource bundle to load. Finally you can access the localized values in
the ResourceBundle via its differentgetString() and getObject() etc.
methods.
ResourceBundlelabels=ResourceBundle.getBundle("i18n.MyBundle",locale);
System.out.println(labels.getString("label1"));
For this example to work you should put a standard Java property file
namedMyBundle.properties in a Java package named i18n. Make sure
this property file is available on your class path when you run the above code,
meaning the property file should be located among the classes of your
application, and in the i18n package.
As is the standard with Java property files, it is a list of key and value pairs.
The key is on the left side of the = , and the value is on the right side. The
value is what you should localize, not the key.
no default Locale set (e.g. a German computer will have a German Locale as
default), this file is read and returned as aResourceBundle.
Java NumberFormat
The java.text.NumberFormat class is used to format numbers according
to a specificLocale. Different countries have different standards for how they
format numbers. In Denmark fractions of a number are separated from the
integer part using a comma. In England they use a dot.
Creating a NumberFormat
Creating a NumberFormat for a specific Locale is done like this:
Localelocale=newLocale("da","DK");
NumberFormatnumberFormat=NumberFormat.getInstance(locale);
Formatting Numbers
Formatting a number using a NumberFormatter is done using
the format() method. Here is an example:
Stringnumber=numberFormat.format(100.99);
System.out.println(number);
Using a Danish Locale, the output printed from this code would be:
100,99
Notice that numbers like 100.00 might be formatted without the decimals,
as 100.
Formatting Currencies
To format a number as currency you need a
currency NumberFormat instance. You create a
currency NumberFormat using the getCurrencyInstance() like this:
NumberFormatcurrencyFormat=NumberFormat.getCurrencyInstance(locale);
System.out.println(currency);
Formatting Percentages
To format a number as percentage you need a
percentage NumberFormat instance. You create that using
the getPercentageInstance() like this:
NumberFormatpercentageFormat=NumberFormat.getPercentInstance(locale);
System.out.println(percentage);
Notice the % character after the number. Notice also that the number is
rounded.
Java DateFormat
The java.text.DateFormat class is used to format dates as strings
according to a specificLocale. Different countries have different standards for
how they format dates. In Denmark dates are written using the format ddmm
yyyy, but in the US they format dates using the format mmddyyyy.
Creating a DateFormat
You create a DateFormat using
the getDateInstance() and getTimeInstance()method of
the DateFormat class. Here is an example:
Localelocale=newLocale("da","DK");
DateFormatdateFormat=DateFormat.getDateInstance(
DateFormat.DEFAULT,locale);
Exactly how the formatted date ends up looking for each date format used
depends on theLocale.
Formatting Dates
Formatting dates is done using the format() method. Here is an example:
Localelocale=newLocale("da","DK");
DateFormatdateFormat=DateFormat.getDateInstance(
DateFormat.DEFAULT,locale);
Stringdate=dateFormat.format(newDate());
System.out.println(date);
The output printed from this code when executed on nov. 1st 2012 would be:
01112012
Formatting Time
In order to format only time and not the date itself, you need a time instance of
the DateFormatclass. You create such an instance using
the getTimeInstance() method. Here is an example:
Localelocale=newLocale("da","DK");
DateFormatdateFormat=DateFormat.getTimeInstance(
DateFormat.DEFAULT,locale);
Stringdate=dateFormat.format(newDate());
System.out.println(date);
The output printed from that code could look like this:
12:43:37
Stringdate=dateFormat.format(newDate());
System.out.println(date);
Java SimpleDateFormat
The java.text.SimpleDateFormat class is used to both parse and
format dates according to a formatting pattern you specify yourself.
This text explains how to use the SimpleDateFormat class to format dates.
Creating a SimpleDateFormat
You create a SimpleDateFormat instance like this:
Stringpattern="yyyyMMdd";
SimpleDateFormatsimpleDateFormat=newSimpleDateFormat(pattern);
Formatting Dates
Once you have created a SimpleDateFormat instance you can format
dates using itsformat() method. Here is an example:
Stringpattern="yyyyMMdd";
SimpleDateFormatsimpleDateFormat=newSimpleDateFormat(pattern);
Stringdate=simpleDateFormat.format(newDate());
System.out.println(date);
Parsing Dates
You can parse a String into a java.util.Date instance using
the parse() method of theSimpleDateFormat instance. Here is an
example:
Stringpattern="yyyyMMdd";
SimpleDateFormatsimpleDateFormat=newSimpleDateFormat(pattern);
Datedate=simpleDateFormat.parse("20121224");
Once this code is executed, the date variable points to a Date instance
representing december 24th, 2012.
the name of the week day will write the week day in the language of the
given Locale. Here is an example:
Stringpattern="EEEEEMMMMMyyyyHH:mm:ss.SSSZ";
SimpleDateFormatsimpleDateFormat=
newSimpleDateFormat(pattern,newLocale("da","DK"));
Stringdate=simpleDateFormat.format(newDate());
System.out.println(date);
Pattern Syntax
You can use the following symbols in your formatting pattern:
G Era designator (before christ, after christ)
M Month in year. Number of M's determine length of format (e.g. MM, MMM
or MMMMM)
a AM / PM marker
z Time Zone
'
'
Single quote
Characters other than these will be treated as normal text to insert into the
pattern, and thus into the formatted dates.
Some characters can be used in different numbers. For instance, you can
write either yy for a 2-character version of the year (e.g. 12), or you can
write yyyy for a 4-character version of the year (e.g. 2012). For more
information about the patterns accepted, see the JavaDoc for
theSimpleDateFormat class.
Pattern Examples
Here are a few patterns examples:
Pattern
Example
dd-MM-yy
31-01-12
dd-MM-yyyy
31-01-2012
MM-dd-yyyy
01-31-2012
yyyy-MM-dd
2012-01-31
yyyy-MM-dd HH:mm:ss
2012-01-31 23:59:59
yyyy-MM-dd HH:mm:ss.SSS
2012-01-31 23:59:59.999
yyyy-MM-dd HH:mm:ss.SSSZ
2012-01-31 23:59:59.999+0100
DateFormatSymbols
It is possible to customize the date symbols used in the formatted output, for a
specific Locale. You do so using
a java.text.DateFormatSymbols instance. Here is an example:
Localelocale=newLocale("en","UK");
DateFormatSymbolsdateFormatSymbols=newDateFormatSymbols(locale);
dateFormatSymbols.setWeekdays(newString[]{
"Unused",
"SadSunday",
"ManicMonday",
"ThrivingTuesday",
"WetWednesday",
"TotalThursday",
"FatFriday",
"SuperSaturday",
});
Stringpattern="EEEEEMMMMMyyyy";
SimpleDateFormatsimpleDateFormat=
newSimpleDateFormat(pattern,dateFormatSymbols);
Stringdate=simpleDateFormat.format(newDate());
System.out.println(date);
SuperSaturdayNovember2012