Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php web application internationalisation with Gettext();

I am currently developping a PHP web app that needs to be available in French and English. I looked around and found that the Gettext() functionality offers the best performance.

I generated my .pot file with the command

 xgettext PhpFileToTranslate.php -o NameOfTemplateFile.pot

and successfuly generated a .pot file. I used PoEdit to generate messages.po file and created messages.mo

Here are my server files :

/app
    /locale
        /en_US
              /LC_MESSAGES
              messages.po
              messages.mo
        /fr_FR
              /LC_MESSAGES
              messages.po
              messages.mo
        template.pot

And i set the locale in the php file like this :

$language = $_SESSION['language'];  //session contains 'fr_FR.UTF-8' or 'en_US.UTF-8'
setlocale(LC_ALL, $language);
bindtextdomain('messages', './locale');  
textdomain('messages');

but i only see the label and not the translation... I tried putting 'fr_FR' instead of 'fr_FR.UTF-8' and all kinds of strings but it's not working.

Am I missing something? My server is Ubuntu 12.04 Server with Apache and php5

like image 771
William Fortin Avatar asked Aug 15 '12 17:08

William Fortin


People also ask

Can PHP use gettext?

The gettext module in PHP only supports languages and language identifiers that are installed on your server. on the command line of your server (e.g., by logging in with SSH). The command may require admin privileges and maybe not work on a hosted shared server.

How does gettext () work?

The Full Gettext Process Gettext works by, first, generating a template file with all the strings to be translated directly extracted from the source files, this template file is called a . pot file which stands for Portable Object Template.

What is gettext package?

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.

What is localization PHP?

Localization in software engineering is the process of adapting the content of a system to a particular locale so the target audience in that locale can understand it. It is not just about changing content from one language to another.


1 Answers

To compile your .mo files on linux you need to have the locales installed on the server and on the machine that compiles it. Locales are the languages supported by your OS.

As stated in this tutorial : http://dev.jimdo.com/archive-old-blog/tutorial-for-the-easy-use-of-gettext-for-internationalization-of-php-apps/ Verify that you have the locales installed on your linux

Be sure that the locales you want to use are installed in your linux and that you use the .UTF-8 (you want i18n so please use UTF!):

# Example for debian
dpkg-reconfigure locales

-- To install new locales on linux

List the languages actually installed on your system:

locale -a

Example to install fr_FR:

#List the available i18n locales
less /usr/share/i18n/SUPPORTED

Install the locale (here fr_FR.utf8)

locale-gen fr_FR.utf8

Reconfigure the locales on your system

sudo  dpkg-reconfigure locales

Recompile your .mo file with PoEdit and that should do the trick. Make sure you install locales both on server and on your machine compiling .mo files.

Hope that helps

like image 61
Bruno Gagnon-Adam Avatar answered Sep 20 '22 16:09

Bruno Gagnon-Adam