Return to site

Java localization

July 4, 2021

ResourceBundle

ResourceBundle class is used to store text and objects which are locale-sensitive.

Generally, we use property files to store locale-specific text and then represent them using a ResourceBundle object.

―Given two properties files:

Messages_en_US.properties containing

Messages_fr_FR.properties containing

―Given the resource bundle:

―We can use the resource bundle to get value in the selected language (here French or English).

Locale class

―Define:

A Locale class object represents a specific geographical/political/cultural region.

Any operation requiring a Locale to perform its task is called locale-sensitive operation,

and uses the Locale to master information relative to the user.

Locale contains: Language, Script, Country, Variant, Extensions

―Snippet:

Display setting

―Define:

It is possible to independently set the default locale for the display setting that is used in menus and dialogs. Introduced in the Java SE 7 release, the

method takes a Locale.Category parameter. Passing the DISPLAY enum returns the default locale used by the UI.

The corresponding

method allows setting the locale for the desired category.

The no-argument getDefault method returns the DISPLAY default value.

On the Windows platform, these default values are initialized according to the settings in the Windows control panel.

―Snippet:

NumberFormat

―Given:

―We have

DecimalFormat

―Given

and

―We have

DateFormat

java.text.DateFormat class formats dates as per the locale. As different countries use different formats to display dates. This class is extremely useful in dealing with dates in the internationalization of applications.

SimpleDateFormat

java.text.SimpleDateFormat class formats dates as per the given pattern. It is also used to parse dates from a string where the string contains a date in the mentioned format.

―Given

and

―We have