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

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.

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

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.


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.


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.

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:
  • great post.

    Thanx for the code

  • awesome. Thanks you!

  • Ye Htun


  • jyothi

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

  • 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?

    • For that you need to use cookies and database.

  • Jefferson Greick

    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?

    • Did you follow all the instruction given above?

    • Jefferson Greick

      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.