Implementing Multilingual Support in PHP Using Gettext
This article explains how to add multilingual capabilities to PHP applications by defining language arrays, detecting user preferences, installing and configuring the gettext extension, creating .po/.mo translation files, and providing code examples for loading translations and switching languages.
With the rise of globalization, providing multilingual support has become a crucial requirement for modern applications, allowing users to interact in their preferred language.
PHP, a popular server‑side scripting language, offers robust multilingual features; this article introduces its core capabilities and demonstrates practical usage through 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:
<code>$languages = [
'en' => 'English',
'zh' => '中文'
];
</code>When a user accesses the application, retrieve the preferred language from the HTTP request header and decide which language code to use:
<code>$preferred_language = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
// Determine language based on the header
if (strpos($preferred_language, 'zh') !== false) {
$language_code = 'zh'; // User prefers Chinese
} else {
$language_code = 'en'; // Default to English
}
</code>PHP’s gettext extension provides the internationalization mechanism. Ensure the extension is installed and enabled on the server.
Mark translatable strings with the gettext function, for example:
<code>echo gettext("Hello, world!");
</code>For each supported language, create translation files with .po or compiled .mo extensions that map original strings to their translations. These files can be generated and edited using GNU gettext tools.
Load and configure the translation environment in PHP as follows:
<code>// 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 directory containing translation files
bindtextdomain('myapp', 'path/to/locales');
// Select the domain for translations
textdomain('myapp');
// Output translated text
echo gettext("Hello, world!");
</code>The code uses putenv and setlocale to set the language environment, then bindtextdomain and textdomain to load the appropriate translation files, and finally gettext to translate strings.
Translation files must be placed in the application’s locales directory and named according to the domain (e.g., myapp ).
To allow users to switch languages at runtime, update the language code and re‑initialize the locale settings:
<code>// User selects Chinese
$language_code = 'zh';
// Re‑set locale environment
putenv('LC_ALL=' . $language_code);
setlocale(LC_ALL, $language_code);
</code>By following these steps, developers can implement core multilingual support in PHP, optionally integrating database‑driven dynamic internationalization to meet diverse user language needs.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.