Multi-lingual website with PHP-gettext() on Linux System

Wednesday June 24th, 2015 | by PhilVanB | de_DE Dieser Artikel auf deutsch


Multi-lingual websites or webapplications are of quite an importance for webdevelopment. Even if it is not absolutely necessary, multilingual versions of a website never are a disadvantage. How one and the same content can be displayed in different languages technically on a Linux-System is the topic of this article.


Implementation with the PHP-gettext()-function

In order to basically apply the multilingualism add the following PHP-code for instance to the index.php-file in the root directory of your webapplication (you may of course add it to any other PHP-file as well):

 

putenv('LC_ALL=de_DE.utf8');

setlocale(LC_ALL, 'de_DE.utf8');

bindtextdomain('index', '/var/www/myApp/locale');

bind_textdomain_codeset('index', "UTF-8");

textdomain('index');

echo _('Hello World');



The current language of the website is passed to the functions putenv() and setlocale() in lines 1 and 2. For English you pass "en_GB" for spanisch "es_ES" and so on. This language code should be generated dynamically with PHP, when the language of the website ist changed. 


mo.- and po.-files

In the next step a .po-file needs to be created via the linux terminal. Therefore, in the terminal, change directories to the folder where the file with the PHP-Code from above with the gettext()-function is located. Then run this command:

xgettext --default-domain=index -k_ index.php


Now an index.po-file has been created in this directory an it should look something like this:


# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2014-11-11 11:56+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: 8bit\n"

#: index.php:25
msgid "Hello World"
msgstr ""


The directory structure for the language files


All the different language files, that consist of .po- and .mo-files, need to be placed in a certain directory structure. The path to the language files has been passed to the bindtextdomain()-function mentioned in the PHP-code above in line 5 In the directory „locale“ for every single language a folder needs to be created. The name of each folder has to be the language code for that certain language. Thus the folder for english is named „en_GB“, the folder for spanish „es_ES“ and so on. In every language folder another folder named „LC_MESSAGES“ has to be created. Move the .po-file created with the linux terminal as explained above in this directory „LC_MESSAGES“ .

Translate the language files with Poedit

In order to be able to translate the language files comfortably the program Poedit is helpful. This program should be installed next, if not already available. The .po-files may of course also be translated in a common texteditor of your choice. The line with „msgid“ is the placeholder, e.g. „Hello World“. It is the same in every language an it is passed to the PHP-gettext()-function as a string. The second line „msgstr“ contains the translation. The .po-file for every single language can be opened up, translated and stored with Poedit. In doing so a .mo-file is created. If all texts are translated and all .mo-files created, then restart the server. Now all translations should work being called on the website.

Troubleshooting

In case errors occur, check if the line "Project-Id-Version" in the .po-file has been set to something different than  PACKAGE VERSION.
Restarting the server or setting the file permissions of the concerned files to 777 may help.
It is also important to check, if the required language file is installed on the system and if the correct file name, e.g. "en_GB.utf8" is passed to the functions putenv() and setlocale().

This linux command helps to find out if a language file is installed on the system:


locale -a

 

Helpful Links:

http://mel.melaxis.com/devblog/2005/08/06/localizing-php-web-sites-using-gettext/

http://www.phpbar.de/w/Gettext

http://phptal.org/manual/de/split/gettext.html

https://lingohub.com/blogs/2013/07/php-internationalization-with-gettext-tutorial/

http://stackoverflow.com/questions/5657654/gettext-setup-not-working



>