July 9, 2014 10:47 pm

How to Internationalize your PHP web sites using gettext

Developing multi language PHP application is very easy and useful so today I am going to write a tutorial on how to Internationalize your PHP websites using gettext function. Using gettext(“hello”) function your string inside that function will be translated to your defined translation you can use underscore character ‘_’  as an alias _(“hello”). To use PHP gettext in your applications you need to enable gettext extension first.

How to Internationalize your PHP web sites using gettext

[wpdm_file id=106]DEMO

Things Covered in this tutorial

  1. Gettext Extension Installation
  2. Translation files creation
  3. Final Implementation

Gettext Extension Installation

Download extension for Linux

Download extension for Windows

After downloading extension add them in there respective directories and enable in your php.ini as written below.

# for Linux
extension=gettext.so

# for Windows
extension=php_gettext.dll

Translation files creation

Big question how do we create translation files?

PHP gettext read .po files and .mo file, .po file contain plain strings and its translation and .mo file is the compiled version that will be used by PHP.

Its very easy and simple to create translation files there is a software called poedit you can download this software and install in your computer and create you translation file with name messages.po with below code and save in a directory.

internationalize php poedit phpgang

# word 1
msgid "Hello World"
msgstr ""

# word 2
msgid "My name is"
msgstr ""

Now open that file in poedit software and you can see yours strings without any translation, now add you translations in front of each of your string and save it. After saving that file poedit software create messages.mo file itself and our translation phase end now go for PHP script.

You have created translation files messages.mo and messages.po now come to the directory structure and put files in there translation file:

internationalize php directory structure phpgang

Above image of directory structure show that we are using 4 languages translation in locale directory its upto you to name locale directory as per your requirement but en_US English (US), fr_FR French, hi_IN Hindi and ur_PK Urdu and LC_MESSAGES  these are the standard names used by gettext. LC_MESSAGES this directory in all language directory contans our messages.po and messages.mo files.

Final Implementation

Now we have translation files ready extension enabled and added files in language directory, create translation.php file and put below code in that file.

translation.php

<?php
$locale = false; // initilize language parameter
if (isSet($_GET["locale"])) // check if getting locale value in query string
{
    $locale = $_GET["locale"]; // add selected language in variable
}
else
{
    $locale = "en_US"; // set English US as default language
}

putenv("LANGUAGE=$locale");
setlocale(LC_ALL, $locale);

$domain = "messages";
bindtextdomain($domain, "locale");

bind_textdomain_codeset($domain, "UTF-8");

textdomain($domain);
?>

In this code we use some functions like putenv() Change the value of environment variable LANGUAGE and gettext will use that locale language value for current session.

Used setlocale() to update locale language value for current session of application.

Bindtextdomain() tells the gettext where to get translation files.

Used bind_textdomain_codeset() to define encoding for the domain messages.

Function textdomain() used to tells gettext which domain to use for translation here is our example we used messages domain.

Now create index.php file which contains text in gettext() function.

index.php

<?php include("translate.php"); ?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<a href="?locale=en_US">English</a> |
<a href="?locale=fr_FR">French</a> |
<a href="?locale=hi_IN">Hindi</a> |
<a href="?locale=ur_PK">Urdu</a>
<br>
<br>
<?php echo gettext("Hello World");?><br>
<p><?php echo gettext("My name is");?> Huzoor Bux.</p>
</body>
</html>

In above HTML we add 2 gettext function call first for “Hello World” and end for “My name is” and anchor texts used to change languages as you can see locale parameter value set for all languages. I have created 2 strings translation in my demo you can check it how it works and also added source to download for free.

[wpdm_file id=106]DEMO

That’s all for translation procedure I hope it helps you in your projects, please don’t forget to follow us on twitter PHPGang and share it with your friends.

Feel free to comment below your problems and suggestions.

Author Huzoor Bux

I am Huzoor Bux from Karachi (Pakistan). I have been working as a PHP Developer from last 5+ years, and its my passion to learn new things and implement them as a practice. Basically I am a PHP developer but now days exploring more in HTML5, CSS and jQuery libraries.


Tutorial Categories:

10 responses to “How to Internationalize your PHP web sites using gettext”

  1. Rahul Sati says:

    great post.

    Thanx for the code

  2. hoa_doi_2911 says:

    awesome. Thanks you!

  3. Ye Htun says:

    ျမန္မာဘာသာစကား

  4. jyothi says:

    Hi,
    i have design a admin panel (using html, php,mysql) how to design internationalization mysql database

  5. This is a very good code snipped… thanks for it.
    Just wonder how it can remember the last choice of language you choose when coming back to the site?

  6. Jefferson Greick says:

    Hello, i unziped the file and uploaded it to my site but it doesn’t work, no error message, nothing…. Where to start look for the problem?

    • Huzoor Bux says:

      Did you follow all the instruction given above?

    • Jefferson Greick says:

      i created a script with phpinfo() and there i found: GetText Support – enabled. So, i decided that i didnt need to download the dll (for windows) ou so (for linux).

      Than i createt a folder called gettext and unziped all files there. Open index.php on my browser (xampp – windows) and when i click on another language it changes the page, put a GET parameter (ex: fr_FR) but the language dont change. In firebug no warning, error, nothing. The same happens on my site (hosted in linux).

      No changes where made to any file.

      Thanks againd.

  7. Max John says:

    The demo is not working

Leave a Reply

Your email address will not be published. Required fields are marked *