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.

Things Covered in this tutorial

Gettext Extension Installation

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

1

2

3

4

5

# 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.

1

2

3

4

5

6

7

# 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:

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.

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<?phpinclude("translate.php");?>

<html>

<head>

<meta http-equiv="Content-Type"content="text/html; charset=UTF-8"/>

</head>

<body>

<ahref="?locale=en_US">English</a>|

<ahref="?locale=fr_FR">French</a>|

<ahref="?locale=hi_IN">Hindi</a>|

<ahref="?locale=ur_PK">Urdu</a>

<br>

<br>

<?phpechogettext("Hello World");?><br>

<p><?phpechogettext("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.

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.

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).