Nearby lessons

122 of 125

Java - Internationalization (I18N)

📌 What You Will Learn
  • What internationalization means
  • How Java separates language from code
  • The Locale class
  • Resource bundles and properties files

Internationalization (I18N) is a core concept of the Java language. This lesson explains What is Internationalization?, Internationalization vs Localization and The Two Key Tools: Locale and ResourceBundle with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

What is Internationalization?

Internationalization means designing a program so that it can work in many languages and regions without changing the code. In short form it is written I18N — because the word Internationalization starts with I, ends with N, and has 18 letters in between.

In simple words: Internationalization (I18N) means writing the program once so it can work in many languages. The code never changes — only the language files do.

Real-life example: an app written in English can also show its menus in Hindi, Telugu, French or Arabic — the code stays the same; only the language files change.

A related word is localization (L10N) — the actual work of translating a program into a specific language and making it fit that country's culture (date formats, currency, number formats).

Internationalization vs Localization

PointInternationalization (I18N)Localization (L10N)
Full meaningDesigning the program to SUPPORT many languagesActually ADAPTING it to one specific language/country
What you doWrite code so text lives in separate filesCreate the translated files (Hindi, Telugu...)
Done byDevelopersDevelopers + translators
RelationshipThe preparation (the ground work)The translation (the result)

The Two Key Tools: Locale and ResourceBundle

  • Locale — identifies a language + country combination, like en_US (English, United States) or hi_IN (Hindi, India).
  • ResourceBundle — loads the correct language file automatically, based on the Locale.

The idea: keep all text in separate files (called resource bundles), one per language. The program picks the right file at run time.

In simple words: The `Locale` says which language and country you want; the `ResourceBundle` fetches the matching language file. That pairing is the whole trick of I18N.

The Properties Files (Our Language Files)

Create one .properties file per language. Each file has key=value pairs.

The file name rule: messages is the base name, then _en, _hi is the locale, then .properties.

Example04
JCode Cell
1# messages_en.properties (English - default)
2welcome=Welcome to our Application
3logout=Logout
4 
5# messages_hi.properties (Hindi)
6welcome=स्वागत है
7logout=लॉग आउट

Using Locale and ResourceBundle

Example05
JCode Cell
1import java.util.*;
2 
3class I18NDemo {
4 public static void main(String[] args) {
5 // English locale
6 Locale en = new Locale("en", "US");
7 ResourceBundle enBundle = ResourceBundle.getBundle("messages", en);
8 System.out.println(enBundle.getString("welcome"));
9 
10 // Hindi locale
11 Locale hi = new Locale("hi", "IN");
12 ResourceBundle hiBundle = ResourceBundle.getBundle("messages", hi);
13 System.out.println(hiBundle.getString("welcome"));
14 }
15}
Output
Welcome to our Application स्वागत है

Numbers, Currency and Dates

The NumberFormat and DateFormat classes automatically show values according to the locale — including the correct currency symbol and date style.

In simple words: `NumberFormat` and `DateFormat` change how numbers, currency and dates look by themselves — the same value 1250.50 becomes $1,250.50 in the US and ₹1,250.50 in India.
Example06
JCode Cell
1import java.util.*;
2import java.text.*;
3 
4class FormatDemo {
5 public static void main(String[] args) {
6 double price = 1250.50;
7 
8 NumberFormat nf = NumberFormat.getCurrencyInstance(new Locale("en", "US"));
9 System.out.println("US: " + nf.format(price));
10 
11 NumberFormat nfIn = NumberFormat.getCurrencyInstance(new Locale("en", "IN"));
12 System.out.println("India: " + nfIn.format(price));
13 }
14}
Output
US: $1,250.50 India: ₹1,250.50

The Benefit of I18N

  • No code changes — to support a new language, just add a new properties file.
  • One product, many markets — the same app sells worldwide.
  • Easy translation — translators only edit the properties files, never the code.
Trainer's Note: Trainer tip: in college projects, 'messages.properties' (default), 'messages_hi.properties', 'messages_te.properties' etc. is a beautiful pattern to show in your final year project — it demonstrates professional thinking. Also, a great modern library that does this automatically is Spring's MessageSource — same idea, used in web apps.

The Locale Class — Constructors

A Locale can be built with one, two or three parts. The classic material uses all three forms:

ConstructorPartsExample
Locale(language)Only languagenew Locale("hi")
Locale(language, country)Language + countrynew Locale("hi", "IN")
Locale(language, country, variant)Language + country + variant (OS/region detail)new Locale("it", "IT", "win")

Java also gives predefined constants like Locale.US, Locale.UK, Locale.ITALY — useful shortcuts when the locale you need is built-in.

Example08
JCode Cell
1new Locale("it") // language only: Italian
2new Locale("it", "IT") // language + country: Italian, Italy
3new Locale("it", "IT", "win") // language + country + variant: Windows Italian
4 
5new Locale("hi", "IN") // Hindi, India
6new Locale("en", "US") // English, United States
📝 Key Takeaways
  • I18N = writing code that supports many languages; L10N = translating to one specific language.
  • Locale = language + country (en_US, hi_IN).
  • ResourceBundle loads the right properties file for the locale.
  • Properties files hold key=value text, one file per language.
  • NumberFormat / DateFormat render currency and dates per locale.
  • Supporting a new language = adding one properties file, no code changes.

🧠 Test Your Knowledge

8 Questions
Progress: 0 / 8