Internationalization (i18n) & Localization (l10n) in Java
Internationalization (i18n) and Localization (l10n) in Java are used to develop applications that support multiple languages, regions, and cultural formats without changing the core code.
1. What is Internationalization (i18n)?
✅ Internationalization (i18n) is the process of designing an application so that it can be easily adapted to different languages and regions without modifying the codebase.
🔹 Key Features of i18n:
- Separates code from language-specific data.
- Uses resource bundles to store language-specific messages.
- Supports date, time, number, and currency formatting.
✅ Example of Internationalization:
import java.util.*;
public class InternationalizationExample {
public static void main(String[] args) {
ResourceBundle bundle = ResourceBundle.getBundle("Messages", Locale.FRANCE);
System.out.println(bundle.getString("greeting"));
}
}
ResourceBundle.getBundle("Messages", Locale.FRANCE)
→ Loads French translations.
2. What is Localization (l10n)?
✅ Localization (l10n) is the process of translating and adapting an internationalized application for a specific language or region.
🔹 Key Features of l10n:
- Provides translated text in different languages.
- Customizes date, number, and currency formats.
- Adapts regional settings (e.g., decimal separator in
1,000.50
vs.1.000,50
).
✅ Example: Resource Bundle for English (Messages_en.properties
)
greeting=Hello, welcome to our application!
✅ Example: Resource Bundle for French (Messages_fr.properties
)
greeting=Bonjour, bienvenue dans notre application !
✅ Example: Switching Language at Runtime
import java.util.*;
public class LocalizationExample {
public static void main(String[] args) {
Locale locale = new Locale("fr", "FR"); // Change to French
ResourceBundle bundle = ResourceBundle.getBundle("Messages", locale);
System.out.println(bundle.getString("greeting")); // Output: Bonjour, bienvenue...
}
}
🔹 Key Takeaway: The same application displays messages in different languages based on the Locale
.
3. Difference Between Internationalization & Localization
Feature | Internationalization (i18n) | Localization (l10n) |
---|---|---|
Definition | Designing software to support multiple languages | Translating & adapting the application for a specific language |
Goal | Make code language-independent | Provide region-specific content |
Who Does It? | Developers | Translators/Localizers |
Example | Using ResourceBundle to separate text | Creating Messages_fr.properties for French |
4. Formatting Numbers, Dates, and Currency Based on Locale
Java provides the NumberFormat
and DateFormat
classes to format data based on regional settings.
✅ Example: Formatting Currency Based on Locale
import java.text.NumberFormat;
import java.util.Locale;
public class CurrencyLocalization {
public static void main(String[] args) {
double amount = 1234.56;
NumberFormat usFormat = NumberFormat.getCurrencyInstance(Locale.US);
NumberFormat frFormat = NumberFormat.getCurrencyInstance(Locale.FRANCE);
System.out.println("US: " + usFormat.format(amount)); // Output: $1,234.56
System.out.println("France: " + frFormat.format(amount)); // Output: 1 234,56 €
}
}
✅ Example: Formatting Dates Based on Locale
import java.text.DateFormat;
import java.util.*;
public class DateLocalization {
public static void main(String[] args) {
Date today = new Date();
DateFormat usFormat = DateFormat.getDateInstance(DateFormat.LONG, Locale.US);
DateFormat frFormat = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);
System.out.println("US: " + usFormat.format(today)); // Output: January 12, 2025
System.out.println("France: " + frFormat.format(today)); // Output: 12 janvier 2025
}
}
🔹 Key Takeaway: The same number and date are formatted differently based on the locale.
5. Summary
Feature | Internationalization (i18n) | Localization (l10n) |
---|---|---|
Purpose | Make the application language-independent | Adapt the application for a specific language |
Process | Use ResourceBundle , Locale , NumberFormat , DateFormat | Create translated properties files |
Who Does It? | Developers | Translators |
Example | ResourceBundle.getBundle("Messages", locale) | Messages_fr.properties for French |
Final Thoughts
✔ Internationalization makes code flexible, and localization makes it usable worldwide.
✔ ResourceBundle
+ Locale
= Easy text translation.
✔ NumberFormat
, DateFormat
, CurrencyFormat
= Region-based formatting.