Internationalization (i18n) in PHP: 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 PHP: How to Make a Website Multilingual



Internationalization (i18n) is the process of designing software so that it can be easily adapted for different languages and regions without making changes to the code. In PHP, there are several powerful tools for this that allow you to separate code and text, and then dynamically load the required language.



In this article, we will look at the main approaches and libraries for implementing i18n in PHP, including the built-in gettext functions, the intl extension, and popular libraries such as Symfony Translation.



1. Basic i18n Concepts



Before moving on to the tools, it is important to understand the key terms:



  • Locale — a combination of language and region (e.g., ru_RU, en_US, de_DE).
  • Translation — replacing the source string with its equivalent in another language.
  • Pluralization — correct word declension depending on the number (e.g., "1 file", "2 files", "10 files").
  • Formatting — adapting dates, numbers, currencies to the locale.


2. Built-in gettext Library



gettext is a classic tool for i18n, built into PHP. It uses .po (Portable Object) and .mo (Machine Object) files to store translations.



Installation and Setup



Make sure the gettext extension is enabled in php.ini:



extension=gettext


Example of basic setup:



<?php// Set the localesetlocale(LC_ALL, 'ru_RU.UTF-8');// Specify the domain (name of the .mo file)bindtextdomain('messages', './locale');// Encodingbind_textdomain_codeset('messages', 'UTF-8');// Select the domaintextdomain('messages');

// Use translationecho gettext("Hello, World!"); // outputs "Здравствуй, Мир!"// or shorthandecho _("Hello, World!");?>


File Structure



Your translation files should be located in the locale folder:



locale/├── ru_RU/│   └── LC_MESSAGES/│       └── messages.po│       └── messages.mo└── en_US/    └── LC_MESSAGES/        └── messages.po        └── messages.mo


Example of a messages.po file:



msgid "Hello, World!"msgstr "Здравствуй, Мир!"

msgid "Files: %d"msgstr "Файлы: %d"


To compile .po to .mo, use the msgfmt utility:



msgfmt messages.po -o messages.mo


3. intl Extension (Internationalization Functions)



The intl extension provides more modern capabilities for working with locales, including formatting numbers, dates, and messages.



Installation



sudo apt-get install php-intl  # for Ubuntu/Debian


Message Formatting with MessageFormatter



<?php$locale = 'ru_RU';$message = '{0} greets {1}!';$fmt = new MessageFormatter($locale, $message);echo $fmt->format(['PHP', 'World']);// Outputs: PHP greets World!?>


Pluralization



<?php$locale = 'ru_RU';$pattern = '{0, plural, one {# file} few {# files} other {# files}}';$fmt = new MessageFormatter($locale, $pattern);echo $fmt->format([1]); // 1 fileecho $fmt->format([2]); // 2 filesecho $fmt->format([10]); // 10 files?>


Date and Number Formatting



<?php$locale = 'ru_RU';$fmt = new NumberFormatter($locale, NumberFormatter::DECIMAL);echo $fmt->format(1234567.89); // 1 234 567,89

$dateFmt = new IntlDateFormatter($locale, IntlDateFormatter::LONG, IntlDateFormatter::NONE);echo $dateFmt->f

Recommendations