Build multilanguage websites with gettext

→ Are you a new visitor? Please visit the page guidance for new visitors ←

Build multilanguage websites with gettext

Have you ever tried to build a multilanguage website using php ? Whether you did it or not, you should know that this thing requires some thinking before you start coding. I will present you a very simple, fast and scalable solution for this.

What is gettext ?

Gettext is a set of tools that provides a framework to help other GNU packages produce multi-lingual messages. These tools include a set of conventions about how programs should be written to support message catalogs, a directory and file naming organization for the message catalogs themselves, a runtime library supporting the retrieval of translated messages, and a few stand-alone programs to message in various ways the sets of translatable strings, or already translated strings.

Using gettext

Lots of big applications use gettext to translate the messages and strings shown in their user interface to several languages,  for eg. the popular Xchat is one of them. When I first saw a website built with gettext I was perplexed. I had no idea how this works. Later on I studied and saw that the concept is very simple and easy to understand.


First you must enable the Gettext PHP extension into PHP to have access to its functions. If you are using Windows, you probably already have the Gettext DLL and only need to change your php.ini configuration file to enable this extension. Do so by removing the semicolon from the front of the line where php_gettext.dll is located. After that, save the file and restart your Web server software.

Using .mo and .po files

The whole idea behind multilanguage with gettext is to keep the strings and their translation in separate files. These informations are stored in files with .po extension.


Enough with the talk. Let’s see an example of how this runs.

First of all you should establish a predefined directory structure in your application like the following:

In your application folder, you create a “locale” folder in which you put a directory called like the variable “lang” you get from the link. Inside it you create another directory called “LC_MESSAGES” and inside it two files. One with .mo and another with .po extension, both called messages. Then you can write the following lines in order to proceed with the translations:

The above example is getting the language from the link and set that as the language for gettext. So if in the link you will have a parameter called ‘lang=en’, that one will be set as the gettext language. The next step, the directory with the name “en” from locale directory will be chosen for obtaining the translations.

Translations strings are stored in the .po file itself in this form:

The first line which is called “msgid” is the text that is inside your document and will receive the translated string from “msgstr” line. So in your application,  you will write the following line :

and the output will be a string with the translation from the .po file:  “The translation for the above string”;


Poedit is a tool for .po files editing and visualization. It  is cross-platform gettext catalogs (.po files) editor. It is built with wxWidgets toolkit and can run on any platform supported by it (although it was only tested on Unix with GTK+ and Windows). It aims to provide more convenient approach to editing catalogs than launching vi and editing the file by hand.

Every time when you modify a .po file, you should recompile the .mo file in order to have that translation available on the page with gettext. This can be done easily from POEDIT just by pressing the second button from top left (the “SAVE” button ).

Heads up! On windows machine, after you compiled your .mo file, you should restart Apache or your HTTP server in order to take effect. On linux machine you don’t need to do this, because it is already running.



In this article, I presented to you a very powerful tool for building advanced multilanguage web applications. Hope you enjoyed it 🙂

Request an article ←