Gettext does not translate into PHP web

2

My system is Ubuntu 14.04.1, with apache 2 with PHP 5.5.9-1ubuntu4.22

Problem: I want to translate my web application made in PHP with Gettext and I can not get it translated. So I created a new project with the minimum necessary to make translations work.

My project has the following structure:

TESTGETTEXT (nombre del proyecto)
   - Locale
       - en_GB
            LC_MESSAGES
               en_GB.mo
               en_GB.po
       - es_ES
            LC_MESSAGES
               es_ES.mo
               es_ES.po
   - index.php

The content of my index.php is as follows:

session_start();
$language = "es_ES";

if (isset($_GET['language']))
{
    $language = $_GET['language'];
}

putenv("LANG=" . $language);
putenv("LC_ALL={$language}");
setlocale(LC_ALL, $language);

$domain = $language;
bindtextdomain($domain, "./Locale/");
textdomain($domain);
bind_textdomain_codeset($domain, 'UTF-8');

if (function_exists("gettext"))
{
    echo _("La función gettext existe");
}
else
{
    echo _("La función gettext no existe");
}


echo '<br>' . $language . '<br>';
echo _("Hola mundo");

The content of my en_GB.po file is as follows:

msgid ""
msgstr ""
"Project-Id-Version: test\n"
"POT-Creation-Date: 2017-08-31 10:38+0100\n"
"PO-Revision-Date: 2017-08-31 10:39+0100\n"
"Last-Translator: Jonathan López <[email protected]>\n"
"Language-Team: jlu <[email protected]>\n"
"Language: en_GB\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Poedit 1.5.4\n"
"X-Poedit-KeywordsList: _;gettext;gettext_noop\n"
"X-Poedit-Basepath: .\n"
"X-Poedit-SourceCharset: UTF-8\n"
"X-Poedit-SearchPath-0: /home/jonathan/www/testGettext\n"

#: /home/jonathan/www/testGettext/index.php:22
msgid "La función gettext existe"
msgstr "The function gettext exist"

#: /home/jonathan/www/testGettext/index.php:26
msgid "La función gettext no existe"
msgstr "The function gettext doesn't exists"

#: /home/jonathan/www/testGettext/index.php:31
msgid "Hola mundo"
msgstr "Hello word!"

#: /home/jonathan/www/testGettext/index.php:32
msgid "Hola Jose"
msgstr "Hello Jose"

#~ msgid "Vamos a hacer una web multi-idioma."
#~ msgstr "We will make a multilingual website"

To verify that it works use the following URLs to modify the language:

  • localhost / index.php? language = en_ES
  • localhost / index.php? language = en_GB

The result I get:

La función gettext existe
en_GB
Hola mundo

and

La función gettext existe
es_ES
Hola mundo

In no case do you perform the translations. What is wrong with me? How can I find out the error?

    
asked by Jonathan 31.08.2017 в 10:34
source

1 answer

1

Gettext does not work with locales that are not installed on the computer.

Use locale -a to find the ones available in your system:

$ locale -a | egrep "^.._..$"
en_AG
en_IN
en_NG
en_ZM
es_CU

As you can see in mine, I only have those. The rest of the premises are of the type:

$ locale -a | egrep "^es_.."
es_AR.utf8
es_BO.utf8
es_CL.utf8
es_CO.utf8
es_CR.utf8
es_CU
es_CU.utf8
es_DO.utf8
es_EC.utf8
es_ES.utf8
[...]
es_US.utf8
es_UY.utf8
es_VE.utf8

To install new packages of locales you must install them, for example in debian / ubuntu:

$ apt-cache search language-pack | egrep "^language-pack-(es|en|fr) "
language-pack-en - actualizaciones de traducción para inglés
language-pack-es - actualizaciones de traducción para el idioma español; castellano
language-pack-fr - actualizaciones de traducción para francés
# Para instalar el paquete de idiomas francés
$ sudo apt-get install language-pack-fr

To activate more available (but disabled by default) you must use:

$ sudo locale-gen en_GB en_GB.utf8
Generating locales...
  en_GB.ISO-8859-1... done
  en_GB.UTF-8... up-to-date
Generation complete.

We check that it is already available:

$ locale -a | egrep "^.._..$"
en_AG
en_GB
en_IN
en_NG
en_ZM
es_CU

From this moment I can run your program without problems.

    
answered by 31.08.2017 / 11:04
source