Internationalization (i18n) in Java: A Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Internationalization (i18n) in Java: A Complete Guide



Internationalization (abbreviated as i18n — from the word internationalization, where there are 18 letters between the first letter 'i' and the last letter 'n') is the process of designing software so that it can be adapted to various languages and regions without making changes to the source code. Java provides a powerful and built-in set of tools for internationalization that allows developers to easily create multilingual applications.



In this article, we will thoroughly examine the main components of i18n in Java: Locale, ResourceBundle, properties files, and MessageFormat. You will learn how to properly structure localized resources and handle exceptions related to missing translations.



1. Main Components of i18n in Java



Internationalization in Java is based on three key elements:



  • Locale — an object representing a specific geographic, political, or cultural region. It contains information about the language, country (optional), and variant (optional).
  • ResourceBundle — a class that loads localized resources (strings, formats) from properties files (.properties) or other sources.
  • Properties files — text files with the .properties extension that store key=value pairs. The file name follows the pattern: BaseName_language_COUNTRY.properties.


Example file structure for Russian and English languages:



Messages.properties          // Default fileMessages_en.properties       // English (general)Messages_en_US.properties    // English (US)Messages_ru_RU.properties    // Russian (Russia)


2. Creating Properties Files



Let's create a base file Messages.properties (default values in English):



greeting=Hellofarewell=Goodbyeapp.name=My Application


Now let's create the Russian localization Messages_ru_RU.properties:



greeting=Здравствуйтеfarewell=До свиданияapp.name=Моё приложение


For English (US) — Messages_en_US.properties:



greeting=Hellofarewell=Goodbyeapp.name=My Application


3. Using Locale and ResourceBundle



To load localized strings, use the following code:



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

public class I18nExample { public static void main(String[] args) { // Set the locale (e.g., Russian language, Russia) Locale ruLocale = new Locale("ru", "RU"); ResourceBundle bundle = ResourceBundle.getBundle("Messages", ruLocale); // Get localized strings String greeting = bundle.getString("greeting"); String farewell = bundle.getString("farewell"); String appName = bundle.getString("app.name"); System.out.println(greeting + " " + farewell); // Здравствуйте До свидания System.out.println(appName); // Моё приложение }}


If the locale is not found, Java automatically uses a search chain:

  1. First, it looks for a file with an exact match (Messages_ru_RU.properties).
  2. Then, without the country (Messages_ru.properties).
  3. Next, the base file (Messages.properties).
  4. If nothing is found, a MissingResourceException is thrown.


4. Formatting Messages with MessageFormat



Strings often contain variable parts. MessageFormat is used for this purpose. Example with dynamic data:



<

Recommendations