Category: 10. Java Internationalization

https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRcUEH_0xlWFYgGpeNRUmn5taquEq7H2pEiaw&s

  • Format Currencies

    In this example, we’re formatting currencies based on US locale and Danish Locale.

    Example

    Open Compiler

    importjava.text.NumberFormat;importjava.util.Locale;publicclassI18NTester{publicstaticvoidmain(String[] args){Locale enLocale =newLocale("en","US");Locale daLocale =newLocale("da","DK");NumberFormat numberFormat =NumberFormat.getCurrencyInstance(daLocale);System.out.println(numberFormat.format(100.76));
    
    
      numberFormat =NumberFormat.getCurrencyInstance(enLocale);System.out.println(numberFormat.format(100.76));}}</code></pre>

    Output

    It will print the following result.

    kr 100,76
    $100.76
    
  • NumberFormat Class

    The java.text.NumberFormat class is used for formatting numbers and currencies as per a specific Locale. Number formats varies from country to country. For example, In Denmark fractions of a number are separated from the integer part using a comma whereas in England they use a dot as separator.

    Example – Format Numbers

    In this example, we’re formatting numbers based on US locale and Danish Locale.

    Open Compiler

    importjava.text.NumberFormat;importjava.util.Locale;publicclassI18NTester{publicstaticvoidmain(String[] args){Locale enLocale =newLocale("en","US");Locale daLocale =newLocale("da","DK");NumberFormat numberFormat =NumberFormat.getInstance(daLocale);System.out.println(numberFormat.format(100.76));
    
    
      numberFormat =NumberFormat.getInstance(enLocale);System.out.println(numberFormat.format(100.76));}}</code></pre>

    Output

    It will print the following result.

    100,76
    100.76
    
  • ResourceBundle Class

    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 ResourceBundle object. Following are the steps to use locale specific properties file in a java based application.

    Step 1: Create Properties Files

    Suppose we need properties file for English locale. Then create a properties file name XXX_en_US.properties where XXX is the name of the file and en_US represents the locale for English(US).

    Messages_en_US.properties

    message=WelcometoTutorialsPoint.COM!

    Let’s now create properties file for French locale. Then create a properties file name XXX_fr_FR.properties where XXX is the name of the file and fr_FR represents the locale for French(France).

    Messages_fr_FR.properties

    message=Bienvenue sur TutorialsPoint.COM!

    Here you can figure out that the key is same but the value is locale specific in both the properties file.

    Step 2: Create ResourceBundle Object

    Create ResourceBundle object with properties file name and locale using following syntax.

    ResourceBundle bundle =ResourceBundle.getBundle("Messages",Locale.US);

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Step 3: Get the value from ResourceBundle Object

    Get the value from ResourceBundle object by passing the key.

    String value = bundle.getString("message");

    Example

    Following example illustrate the use of ResourceBundle objects to display locale specific values from properties files.

    Open Compiler

    importjava.util.Locale;importjava.util.ResourceBundle;publicclassI18NTester{publicstaticvoidmain(String[] args){ResourceBundle bundle =ResourceBundle.getBundle("Messages",Locale.US);System.out.println("Message in "+Locale.US+": "+bundle.getString("message"));  
    
    
      bundle =ResourceBundle.getBundle("Messages",Locale.FRANCE);System.out.println("Message in "+Locale.FRANCE+": "+bundle.getString("message"));}}</code></pre>

    Output

    It will print the following result.

    Message in en_US: Welcome to TutorialsPoint.COM!
    Message in fr_FR: Bienvenue sur TutorialsPoint.COM!
    

    Notes for Naming Conventions

    Following are the naming conventions for the properties file.

    • For properties file mapped to default locale, no prefix is mandatory. message_en_US.properties is equivalent to message.properties.
    • For properties file mapped to locale, prefix can be attached in two ways. message_fr.properties is equivalent to message_fr_FR.properties.
  • Locale Language

    Example

    In this example, we’ll get display language per locale passed as an argument.

    Open Compiler

    importjava.util.Locale;publicclassI18NTester{publicstaticvoidmain(String[] args){Locale defaultLocale =Locale.getDefault();Locale enLocale =newLocale("en","US");Locale frLocale =newLocale("fr","FR");Locale esLocale =newLocale("es","ES");System.out.println(defaultLocale.getDisplayLanguage(enLocale));System.out.println(defaultLocale.getDisplayLanguage(frLocale));System.out.println(defaultLocale.getDisplayLanguage(esLocale));}}

    Output

    It will print the following result.

    English
    anglais
    inglés
    
  • Locale Details

    Example

    In this example, we’ll get default locale and print its details. Then create a locale for “fr” and print its details.

    Open Compiler

    importjava.util.Locale;publicclassI18NTester{publicstaticvoidmain(String[] args){Locale locale =Locale.getDefault();System.out.println("Default Locale Properties:\n");System.out.println(locale.getDisplayCountry());System.out.println(locale.getDisplayLanguage());System.out.println(locale.getDisplayName());System.out.println(locale.getISO3Country());System.out.println(locale.getISO3Language());System.out.println(locale.getLanguage());System.out.println(locale.getCountry());Locale frenchLocale =newLocale("fr","fr");System.out.println("\nfr Locale Properties:\n");System.out.println(frenchLocale.getDisplayCountry());System.out.println(frenchLocale.getDisplayLanguage());System.out.println(frenchLocale.getDisplayName());System.out.println(frenchLocale.getISO3Country());System.out.println(frenchLocale.getISO3Language());System.out.println(frenchLocale.getLanguage());System.out.println(frenchLocale.getCountry());}}

    Output

    It will print the following result.

    Default Locale Properties:
    
    United States
    English
    English (United States)
    USA
    eng
    en
    US
    
    fr Locale Properties:
    
    France
    French
    French (France)
    FRA
    fra
    fr
    FR
    
  • Locale Class

    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. For example, displaying a number is a locale-sensitive operation. The number should be formatted as per the customs and conventions of the user’s native country, region, or culture.

    Locale Contents

    A Locale object contains the following −

    Language

    ISO 639 alpha-2 or alpha-3 language code, or registered language subtags up to 8 alpha letters. alpha-2 code must be used if both alpha-2 and alpha-3 code are present. The language field is case insensitive, but Locale always canonicalizes to lower case.

    Script

    ISO 15924 alpha-4 script code. The script field is case insensitive, but Locale always canonicalizes to title case.

    Country (Region)

    ISO 3166 alpha-2 country code or UN M.49 numeric-3 area code. The country field is case insensitive, but Locale always canonicalizes to upper case.

    Variant

    Any arbitrary value used to indicate a variation of a Locale. Where there are two or more variant values each indicating its own semantics, these values should be ordered by importance, with most important first, separated by underscore(‘_’). The variant field is case sensitive.

    Extensions

    A map from single character keys to string values, indicating extensions apart from language identification. The extensions in Locale implement the semantics and syntax of BCP 47 extension subtags and private use subtags. The extensions are case insensitive, but Locale canonicalizes all extension keys and values to lower case.

  • Environment Setup

    Local Environment Setup

    If you are still willing to set up your environment for Java programming language, then this section guides you on how to download and set up Java on your machine. Following are the steps to set up the environment.

    Java SE is freely available from the link Download Java. You can download a version based on your operating system.

    Follow the instructions to download Java and run the .exe to install Java on your machine. Once you installed Java on your machine, you will need to set environment variables to point to correct installation directories −

    Setting Up the Path for Windows

    Assuming you have installed Java in c:\Program Files\java\jdk directory −

    • Right-click on ‘My Computer’ and select ‘Properties’.
    • Click the ‘Environment variables’ button under the ‘Advanced’ tab.
    • Now, alter the ‘Path’ variable so that it also contains the path to the Java executable. Example, if the path is currently set to ‘C:\WINDOWS\SYSTEM32’, then change your path to read ‘C:\WINDOWS\SYSTEM32;c:\Program Files\java\jdk\bin’.

    Setting Up the Path for Linux, UNIX, Solaris, FreeBSD

    Environment variable PATH should be set to point to where the Java binaries have been installed. Refer to your shell documentation, if you have trouble doing this.

    Example, if you use bash as your shell, then you would add the following line to the end of your ‘.bashrc: export PATH = /path/to/java:$PATH’

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Popular Java Editors

    To write your Java programs, you will need a text editor. There are even more sophisticated IDEs available in the market. But for now, you can consider one of the following −

    • Notepad − On Windows machine, you can use any simple text editor like Notepad (Recommended for this tutorial), TextPad.
    • Netbeans − A Java IDE that is open-source and free which can be downloaded from https://www.netbeans.org/index.html.
    • Eclipse − A Java IDE developed by the eclipse open-source community and can be downloaded from https://www.eclipse.org/.
  • Overview

    Internationalization

    Internationalization or I18N refers to the capability of an Application to be able to serve users in multiple and different languages. Java has in-built support for Internationalization. Java also provides formatting of numbers, currencies and adjustment of date and time accordingly.

    Java Internationalization helps to make a java application handle different languages, number formats, currencies, region specific time formatting.

    Localization

    Localization or L10N is the adaptability of an application that is how an application adapts itself with a specific language, number formats, date and time settings etc.

    A java application should be internationalized in order to be able to localize itself.

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Culturally Dependent Information

    Following information items often varies with different time zones or cultures.

    • Messages
    • Date
    • Time
    • Number
    • Currency
    • Measurements
    • Phone Numbers
    • Postal Addresses
    • GUI labels

    Internationalization Classes

    Java has a set of built-in classes which help in internationalization of an application. These classes are following −

    Sr.No.Class & Description
    1LocaleRepresents a language along with country/region.
    2ResourceBundleContains localized text or objects.
    3NumberFormatUse to format numbers/currencies as per the locale.
    4DecimalFormatUse to format numbers as per customized format and as per locale.
    5DateFormatUse to format dates as per locale.
    6SimpleDateFormatUse to format dates as per customized format and as per locale.