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
.propertiesextension that storekey=valuepairs. 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 ApplicationNow 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 Application3. 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:
- First, it looks for a file with an exact match (
Messages_ru_RU.properties). - Then, without the country (
Messages_ru.properties). - Next, the base file (
Messages.properties). - If nothing is found, a
MissingResourceExceptionis thrown.
4. Formatting Messages with MessageFormat
Strings often contain variable parts. MessageFormat is used for this purpose. Example with dynamic data:
<
Also in library
GraphQL Java: Complete Guide to the Library for Creating APIs in Java
JWT Tokens in C# (.NET): Complete Guide with Examples
Caching in Kotlin: Redis, Caffeine and Ehcache — A Complete Guide
Working with Images in Go: Resize and Crop with the imaging Library