Backend Development 5 min read

Implementing Multilingual Support in PHP Using Gettext

This article explains how to add robust multilingual capabilities to PHP applications by defining language arrays, detecting user preferences, configuring the gettext extension, creating translation files, and providing code examples for loading locales and switching languages.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Implementing Multilingual Support in PHP Using Gettext

PHP, a popular server‑side scripting language, offers robust multilingual capabilities. This article explains the core features for multilingual support in PHP and demonstrates practical usage through concrete code examples.

First, define the list of supported languages in an associative array where the key is the language code and the value is the language name, e.g., “en” for English and “zh” for Chinese.

$languages = [
    'en' => 'English',
    'zh' => '中文'
];

When a user visits the application, the preferred language is obtained from the HTTP Accept‑Language header and mapped to a language code (defaulting to English if Chinese is not detected).

$preferred_language = $_SERVER['HTTP_ACCEPT_LANGUAGE'];

// Determine the language to use based on the header
if (strpos($preferred_language, 'zh') !== false) {
    $language_code = 'zh'; // User prefers Chinese
} else {
    $language_code = 'en'; // Default to English
}

PHP’s gettext extension is used for internationalisation. The extension must be installed and correctly configured on the server.

Translatable strings are marked with the gettext function, for example:

echo gettext("Hello, world!");

Translation files with extensions .mo or .po store the original and translated strings. GNU gettext tools can create and edit these files.

// Load gettext extension
if (!extension_loaded('gettext')) {
    die('Gettext extension is not enabled.');
}

// Set locale environment
putenv('LC_ALL=' . $language_code);
setlocale(LC_ALL, $language_code);

// Specify the location of translation files
bindtextdomain('myapp', 'path/to/locales');

// Use the domain "myapp" to load translations
textdomain('myapp');

// Output translated text
echo gettext("Hello, world!");

The functions putenv , setlocale , bindtextdomain , textdomain , and gettext work together to load the appropriate translation file and translate text at runtime.

Translation files must reside in the correct location, typically a locales directory, and be named according to the domain.

A language‑switch feature can be implemented by changing the language code and re‑initialising the environment, as shown in the following example.

// User selects Chinese
$language_code = 'zh';

// Re‑set locale environment
putenv('LC_ALL=' . $language_code);
setlocale(LC_ALL, $language_code);

By following these steps, developers can implement core multilingual support in PHP, optionally integrating database‑driven dynamic localisation to meet diverse user language requirements.

backendPHPmultilinguali18ngettext
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.