SrcForge

Lesson 61

Internationalization and Localization (I18N and L10N)

Java Internationalization (I18N) is the process of building an application so it can support multiple languages, regions, and cultural formats without changing the core code. Java Localization (L10N) is the process of adapting that internationalized application for a specific language or region, such as showing local messages, date formats, currencies, and number styles.

Java Track

Save progress as you learn.

61 lessons

Lesson content

Java Internationalization (I18N) is the process of building an application so it can support multiple languages, regions, and cultural formats without changing the core code. Java Localization (L10N) is the process of adapting that internationalized application for a specific language or region, such as showing local messages, date formats, currencies, and number styles.

If you want your Java application to work for users in different countries, internationalization and localization are essential. Java provides built-in support for this using classes such as Locale, ResourceBundle, DateFormat, and NumberFormat.

What is Internationalization in Java?

Internationalization is commonly written as I18N because there are 18 letters between I and N in the word Internationalization.

Internationalization in Java means designing an application so it can easily adapt to different languages, countries, date formats, time formats, number formats, currency formats, and region-specific text and labels.

The goal is to make the application ready for multiple regions without rewriting the program for each country.

What is Localization in Java?

Localization is commonly written as L10N because there are 10 letters between L and N in the word Localization.

Localization means customizing an internationalized application for a specific locale, such as English (United States), French (France), Japanese (Japan), or Hindi (India).

This includes adding locale-specific text messages, currency symbols, date and time formats, number formatting, and labels and UI content.

Key Terms

  • Internationalization (I18N): The process of designing a Java application so it can support multiple languages and regions.
  • Localization (L10N): The process of adapting an internationalized application to a specific language or region.
  • Locale: A Java object that represents a specific language, country, and optional variant.
  • ResourceBundle: A Java class used to store locale-specific text such as translated messages.
  • DateFormat: A class used to format dates and times based on locale.
  • NumberFormat: A class used to format numbers and currencies according to a locale.

Why Internationalization Matters in Java

Before building a multilingual Java application, it helps to understand what kinds of data change from one region to another.

Culturally Dependent Data in Java Applications

The following types of data often need localization:

  • Messages
  • Dates
  • Times
  • Numbers
  • Currencies
  • Measurements
  • Phone numbers
  • Postal addresses
  • Labels on GUI components

For example: the US may display a date as MM/dd/yyyy, while France may display the same date as dd/MM/yyyy. Currency in the US appears as $10.50, while currency in France may appear as 10,50 €.

Locale Class in Java

The Locale class is one of the most important classes in Java internationalization. It represents a geographical, political, or cultural region. Java uses Locale objects to decide how to display data for a specific language or country.

Why Locale is Important

Using Locale, you can get region-specific information such as:

  • Country name
  • Language name
  • Locale display name
  • ISO country code
  • ISO language code

Common Predefined Locale Constants

Java provides many built-in locale constants, such as:

  • Locale.ENGLISH
  • Locale.FRENCH
  • Locale.GERMAN
  • Locale.ITALIAN
  • Locale.JAPANESE
  • Locale.KOREAN
  • Locale.CHINESE
  • Locale.US
  • Locale.UK
  • Locale.CANADA
  • Locale.FRANCE
  • Locale.JAPAN
  • Locale.ROOT

Constructors of Locale Class

Java provides these common constructors for creating a locale:

  • Locale(String language)
  • Locale(String language, String country)
  • Locale(String language, String country, String variant)

Example

Locale locale = new Locale("en", "US");

This creates a locale for English in the United States.

Common Methods of Locale Class

Here are some useful methods of the Locale class:

  • Locale.getDefault() — returns the current default locale
  • Locale.getAvailableLocales() — returns all available locales
  • getDisplayCountry() — returns the country name
  • getDisplayLanguage() — returns the language name
  • getDisplayVariant() — returns the variant
  • getISO3Country() — returns the 3-letter country code
  • getISO3Language() — returns the 3-letter language code

Java Locale Example 1: Get Default Locale Information

This example shows how to get the default locale details from the system.

import java.util.*;

public class ex1 {

	public static void main(String[] args) {  
	Locale locale=Locale.getDefault();  
	//Locale locale=new Locale("fr","fr");//for the specific locale  
	 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());  
	      
	}  

}

Explanation

This program uses Locale.getDefault() to get the current locale of the system and prints the country name, language name, display name, ISO 3-letter country code, ISO 3-letter language code, language code, and country code.

Java Locale Example 2: Display Language Names in Different Languages

This example shows how to display a language name in multiple locales.

import java.util.*;

//printing in different languages

public class ex2 {

	    public static void main(String[] args) {  
	        Locale enLocale = new Locale("en", "US");  
	        Locale frLocale = new Locale("fr", "FR");  
	        Locale esLocale = new Locale("es", "ES");  
	        System.out.println("English language name (default): " +   
	                            enLocale.getDisplayLanguage());  
	  
	        System.out.println("English language name in French: " +   
	                            enLocale.getDisplayLanguage(frLocale));  
	        System.out.println("English language name in spanish: " +   
	                enLocale.getDisplayLanguage(esLocale));  
	    }  
	  
}

Explanation

This program creates three locales: English (US), French (France), and Spanish (Spain). Then it prints the name of the English language in different languages using getDisplayLanguage(locale).

Java Locale Example 3: Display Language Names for Multiple Locales

import java.util.*;  

public class ex3 {
 
	public static void main(String[] args) {  
	Locale[] locales = { new Locale("en", "US"),  
	 new Locale("es", "ES"), new Locale("it", "IT") };   
	  
	for (int i=0; i< locales.length; i++) {   
	 String displayLanguage = locales[i].getDisplayLanguage(locales[i]);   
	 System.out.println(locales[i].toString() + ": " + displayLanguage);   
	}   
	}  
	  
}

Explanation

This example stores multiple locale objects in an array and prints the display language for each locale in its own language.

ResourceBundle Class in Java

The ResourceBundle class is used to internationalize messages in Java applications. It helps you store language-specific text in separate properties files, so your Java code stays the same while the displayed messages change based on the user's locale.

Why Use ResourceBundle?

With ResourceBundle, you can:

  • Show messages in different languages
  • Keep translated text outside the Java source code
  • Manage localization more easily
  • Switch language output based on locale

Common Methods of ResourceBundle

  • ResourceBundle.getBundle(String basename) — returns a bundle for the default locale.
  • ResourceBundle.getBundle(String basename, Locale locale) — returns a bundle for a specific locale.
  • getString(String key) — returns the value for a given key from the properties file.

Java ResourceBundle Example

import java.util.Locale;
import java.util.ResourceBundle;

public class ex4 {

public static void main(String[] args) {

ResourceBundle bundle = ResourceBundle.getBundle("Internationalization_Localization/MessageBundle", Locale.US);

        System.out.println("Message in " + Locale.US + ": " + bundle.getString("greeting"));

        // changing the default locale to Indonesia
        Locale.setDefault(new Locale("id", "ID"));

        bundle = ResourceBundle.getBundle("Internationalization_Localization/MessageBundle");

        System.out.println("Message in " + Locale.getDefault() + ": " + bundle.getString("greeting"));

    }

}

Properties Files Used in the Example

Save these as separate .properties files:

MessageBundle_en_US.properties
greeting=Hello, how are you?

MessageBundle_in_ID.properties
greeting=Halo, apa kabar?

Explanation

  • First, the program loads the message bundle for Locale.US
  • Then it changes the default locale to Indonesian (id_ID)
  • Java automatically loads the correct localized message from the matching properties file

Internationalizing Date in Java

Date formats vary by country, so Java provides a way to display dates based on locale. Use the DateFormat.getDateInstance() method to format dates for different regions.

Java I18N Date Example

import java.text.DateFormat;  
import java.util.*;  

public class InternationalizationDate {  

static void printDate(Locale locale){  

DateFormat formatter=DateFormat.getDateInstance(DateFormat.DEFAULT,locale);  

Date currentDate=new Date();  

String date=formatter.format(currentDate);  

System.out.println(date+" "+locale);  

}  

 public static void main(String[] args) {  

    printDate(Locale.UK);  

    printDate(Locale.US);  

    printDate(Locale.FRANCE);  

}  

}  

Explanation

This program formats the current date for UK, US, and France. The same date will appear differently depending on the locale.

Internationalizing Time in Java

Time format also changes from region to region. Some locales use a 12-hour format, while others use a 24-hour format. Use DateFormat.getTimeInstance() for locale-based time formatting.

Java I18N Time Example

import java.text.DateFormat; 
import java.util.*;  

  public class InternationalizingTime {  

  static void printTime(Locale locale){  

DateFormat formatter=DateFormat.getTimeInstance(DateFormat.DEFAULT,locale);  

Date currentDate=new Date();  

String time=formatter.format(currentDate);  

System.out.println(time+" in locale "+locale);  

}  

  public static void main(String[] args) {  

printTime(Locale.UK);  

printTime(Locale.US);  

printTime(Locale.FRANCE);  

}  

}  

Explanation

This example prints the current time in different locale formats.

Internationalizing Numbers in Java

Different countries use different number formats. For example, the US formats a number as 105,000.324, while France formats it as 105 000,324. Java uses the NumberFormat class to handle locale-specific number formatting.

Java I18N Number Example

import java.text.NumberFormat;
import java.util.*;  

public class InternalizationNumber {  

static void printNumber(Locale locale){  

 double dbl=105000.3245;  

 NumberFormat formatter=NumberFormat.getNumberInstance(locale);  

 String number=formatter.format(dbl);  

 System.out.println(number+" for the locale "+locale);  

}  

public static void main(String[] args) {  

    printNumber(Locale.UK);  

    printNumber(Locale.US);  

    printNumber(Locale.FRANCE);  

    printNumber(Locale.JAPAN);  

  

}  

}  

Explanation

This program formats the same number for multiple locales using NumberFormat.getNumberInstance(locale).

Internationalizing Currency in Java

Currency symbols and formatting rules are different in every country. Java makes it easy to display the correct currency format for a locale. Use NumberFormat.getCurrencyInstance(locale) to format currency values.

Java I18N Currency Example

import java.text.NumberFormat;
import java.util.*;  

public class InternalizationCurrency {  

  static void printCurrency(Locale locale){  

 double dbl=10500.3245;  

 NumberFormat formatter=NumberFormat.getCurrencyInstance(locale);  

 String currency=formatter.format(dbl);  

 System.out.println(currency+" for the locale "+locale);  

}  

  public static void main(String[] args) {  

    printCurrency(Locale.UK);  

    printCurrency(Locale.US);  

    printCurrency(Locale.FRANCE);  

}  

}  

Explanation

This example formats the same amount as currency for different locales. For example: UK uses Pound format, US uses Dollar format, and France uses Euro format.

Key Java Classes Used in Internationalization

Locale

Represents a specific language and region.

ResourceBundle

Loads locale-specific text from properties files.

DateFormat

Formats dates and times according to locale.

NumberFormat

Formats numbers and currencies according to locale.

Best Practices for Java Internationalization

Separate Messages from Code

Use ResourceBundle and .properties files for translated text instead of hardcoding strings.

Use Locale-Aware Formatting

Always format dates, numbers, and currency with Java locale classes.

Avoid Hardcoding Region-Specific Formats

Do not manually write date or currency formats unless necessary.

Test with Multiple Locales

Run your application with different locales to verify output.

Use Meaningful Message Keys

For example:

greeting=Hello
login.button=Login
error.invalid=Invalid input

Java Internationalization vs Localization

FeatureInternationalization (I18N)Localization (L10N)
PurposePrepare the application for multiple regionsAdapt the application to one specific region
FocusDesign and structureRegion-specific customization
ExamplesUsing Locale, ResourceBundle, NumberFormatTranslating text, changing currency/date format
Code changesUsually done during developmentUsually done by adding locale-specific files

FAQ

1. What is the difference between I18N and L10N in Java?

Internationalization (I18N) prepares the application for multiple languages and regions, while Localization (L10N) customizes it for one specific locale.

2. What is the use of the Locale class in Java?

The Locale class represents a language and region and helps Java format dates, times, numbers, currencies, and messages correctly for that locale.

3. Why is ResourceBundle used in Java?

ResourceBundle is used to store translated messages in separate properties files, making it easy to display text in different languages without changing the Java code.

Internationalization (I18N) prepares an application to support multiple languages and regions without code changes.
Localization (L10N) adapts an internationalized application for a specific language or region.
The Locale class represents a language, country, and optional variant used to drive region-specific formatting.
ResourceBundle stores translated messages in properties files, separating text from Java code.
DateFormat and NumberFormat classes format dates, times, numbers, and currencies according to locale.
Best practice favors locale-aware formatting and externalized messages over hardcoded region-specific formats.

Where Java is used

Java in real-world development

Internationalization (I18N)

Designing a Java application so it can support multiple languages and regions without changing core code.

Localization (L10N)

Adapting an internationalized application for a specific language or region, including text, dates, and currency.

Locale

Java object representing a specific language, country, and optional variant used for region-aware formatting.

ResourceBundle

Class used to store locale-specific translated text in separate properties files outside the Java code.

DateFormat

Class used to format dates and times according to a given locale's conventions.

NumberFormat

Class used to format numbers and currencies according to a locale's regional conventions.