The ideal localization tool for PHP developers and their translators

Usually, programs are written and documented in English, and use English at execution time for interacting with users. On the other hand, many customers are less comfortable with English than want their solution software to work in their country language.
So GNU `gettext’ is an important step for the GNU Translation Project, as it is an asset on which we may build many other steps. This package offers to programmers, translators, and even users, a well integrated set of tools and documentation. Specifically, the GNU `gettext’ utilities are a set of tools that provides a framework to help other GNU packages produce multi-lingual messages.

First of all, we will need Poedit program :
Poedit, a cross-platform editor for gettext catalogs. It’s a really nice tool that lets you keep your translations separate from your application code. You can get a copy from www.poedit.net

To activate gettext :

# for Windows users
extension=php_gettext.dll
# for *nix users
extension=gettext.so

Directory structure :
In future projects, you can really name the parent directory (Languages here), but es_ES and LC_MESSAGES are standard names which are used by gettext. es_ES stands for the name of the locale and is made up of two parts. The first part is a two-letter lowercase abbreviation for the language according to the ISO 639-1 specification.

gettext

Next, make sure Poedit is working correctly on your platform. Launch the program and choose from the top menu bar File > New Catalog. In the settings window, fill in the information below skipping the plural forms field for now:
poedit
Click OK and then save the file as messages.po inside the LC_MESSAGES (es-ES)directory you created earlier.

Now close Poedit, and use your favorite text editor to open messages.po… yes, it is an ordinary text file! you can edit it by hand, but to save ourselves some hassle we let Poedit create the main definitions for us. Leave an empty line after the lines that are already in the file and add the following:

#Test text 1
msgid "HELLO_WORLD"
msgstr "hola!"

#Test text 2
msgid "THANKS"
msgstr "Gracias"

Save messages.po and close it, and then re-open it in Poedit.

In Poedit, choose File > Save or click the Save Catalog entry in the icon bar. The reason we are saving the PO file in Poedit is because they need to be compiled into a special format usable by gettext. After you save it in Poedit, you will see a new file has been created in the same directory with the same name but with the extension .mo

Back in the Languages directory, create a file named test-language.php with the following code:

<?php
// I18N support information here
$language = "es_ES";
putenv("LANG=" . $language);
setlocale(LC_ALL, $language);

// Set the text domain as "messages"
$domain = "messages";
bindtextdomain($domain, "Locale");
bind_textdomain_codeset($domain, 'UTF-8');

textdomain($domain);

echo _("HELLO_WORLD");
?>

Open Languages/test-language.php in your browser. If everything is installed correctly and working fine, you will see Hola! displayed on the page. However, On Wampserver and Xampp some problems may occur and the solution to get it work locally is a littl bit different. So You can read the post about gettext not working on wampserver here.
There are many issues about gettext configuration especially when you work with hosting company. Later on I encoutered for example for same code on two different platforms that I must define :

 fr-FR.utf8

instead normally :

 fr-FR

Final thoughts

When it comes to localizing your PHP application, it is better to use the GNU gettext library and its PHP extension, as a powerful and easy approach that localizes especially the big and complex web applications.
Applications like Drupal and wordpress use also gettext for multilingual purposes. In the other hand, Famous new version Unicode Oscommerce 2.3 encounters many difficulties because it uses mixed define text translations contributions with different encodings. You an read my post about that subject HERE.

Cheers,

extradrmtech

Since 30 years I work on Database Architecture and data migration protocols. I am also a consultant in Web content management solutions and medias protecting solutions. I am experienced web-developer with over 10 years developing PHP/MySQL, C#, VB.Net applications ranging from simple web sites to extensive web-based business applications. Besides my work, I like to work freelance only on some wordpress projects because it is relaxing and delightful CMS for me. When not working, I like to dance salsa and swing and to have fun with my little family.

You may also like...

Leave a Reply