date:17th May, 16 | by: Joe Karanka | views:
blog website ideas

In one of my recent projects, I was required to implement localization support of three different languages: English, Finnish and Russian. I had a choice between either employing the use of XML or Php Gettext. Against all cons and pros I opted for php Gettext.

Gettext is a powerful and robust internationalization and localization (I18n) system commonly used for writing multi-lingual programs. I bet you knew that already!


So, why Php Gettext?

Well, gettext is what I would love to call a de-facto standard. That is, it is used by lots of applications, in different languages – which of course suggests that many people trust and work with it.
Secondly, and probably most importantly, non-technical people (am assuming your clients), can easily and effectively edit gettext localization files. This is through PoEdit- which is a free cross-platform for editing translations.


Where do we start?

Assuming that you are working on your localhost, then, it goes unsaid that you need either WAMP, MAMP, LAMP OR XAMP installed.

Having said that, your next step is to install php gettext into the server. There are various ways to do so but of course it all varies with your chosen localhost server.
It is worth noting that php Gettext DOES NOT work on Wamp 64 Bits. But, feel free to have a go at it. You may just end up proving me right.

Next, install PoEdit which amongst other things, will help you keep your translations separate from your application code. Download a free copy at:  


Are we there yet?

The last steps involve setting up/ configuring locales for gettext. Locales are merely language libraries that are used by locale-aware programs to render text and other locale-specific standards.

In your web root directory, create a sandbox directory, in my case LOCALES – you can name yours whichever way suits you. Within the directory, create the following hierarchy:




en_US and LC_MESSAGES are standard names used by gettext so re-naming them will render the whole process a fail.
en_US stands for the name of the locale – in this case American English. I also included fi_FI and ru_Ru for the Finnish and Russian languages. The fi_FI and ru_Ru directories both have the LC_MESSAGES sub-directories.

Next, fire up Poedit. Choose File > New Catalog. Fill in the info and then save the file as messages.po inside the LC_MESSAGES directory you created. Finally, open up your text editor and retrieve the messages.po file. You guessed right, it is an ordinary text file! You can either edit it by hand; though to save ourselves from all the hassle, we will let Poedit create the main definitions on our behalf. Leave an empty line for purposes of that. In the next line, add the following:

#Test 1

msgid "hello guys"
msgstr "How ya'll doing?" 


Save messages.po and then re-open it in Poedit. In Poedit, choose File > Save.

Why do we keep re-opening and saving it in different applications?

Well, gettext is only able to understand binary files and thus by saving the messages.po file, Poedit, automatically generates a file that is binary and that it understands and in return it will to display the translations.

Last but not least, we will need to create an initialization file that simply tells the gettext library which locale you want to use and where the language files are stored. It is within here where you can also set the default website language that you want your pages to work with. That said, open up a New File and Save it in your web directory alongside the locales folder. Name it locales.php and paste the following:


if (isset($_GET["lang"])) {
	$language = $_GET["lang"];
  else if (isset($_SESSION["lang"])) {
    $language = $_SESSION["lang"];
  else {
    $language = "fi_FI"; // Default language is Finnish

$_SESSION["lang"] = $language;
$folder = "locale";
$domain = "messages";
$encoding = "iso-8859-1"; // you can opt for UTF-8
$locale = $language.".".$encoding;      
putenv("LANG=" . $language); 
setlocale(LC_ALL, $language);
bindtextdomain($domain, $folder);
bind_textdomain_codeset($domain, $encoding);       


And there you have it!

Hopefully, this helps you implement support for localization in Php with Gettext.
Here is a website fully functional and employing the use of Php Gettext for localization. Make Up For Ever Academy

Content precedes design. Design in the absence of content is not design, it’s decoration.