Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I analyze why gettext doesn't work?

I am currently trying to use gettext with PHP and poedit. I wrote the following test.php file:

<?php

error_reporting(E_ALL | E_DEPRECATED | E_USER_DEPRECATED | -1);
bindtextdomain('messages', './i18n/');
textdomain('messages');
setlocale(LC_ALL, $_GET['l']);
putenv("LANG=".$_GET['l']);

echo _('test :-(');

?>

and this is my messages.po:

msgid ""
msgstr ""
"Project-Id-Version: Community Chess\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2011-10-07 18:34+0100\n"
"PO-Revision-Date: \n"
"Last-Translator: Martin Thom <[email protected]>\n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Poedit-SourceCharset: utf-8\n"
"X-Poedit-Basepath: /var/www/community-chess\n"
"X-Poedit-SearchPath-0: .\n"

#: test.php:8
msgid "test :-("
msgstr "Juhu :-)"

#~ msgid "test"
#~ msgstr "Juhu!"

My directory structure is

community-chess
    test.php
    i18n
        de_DE
            LC_MESSAGES
                messages.po
                messages.mo

As soon as I look at http://localhost/community-chess/test.php?l=de_DE I get "test :-("

I have generated the locale with

sudo locale-gen de_DE

and checked with

locale -a

Why doesn't it work? How can I get some feedback from gettext?

like image 538
Martin Thoma Avatar asked Nov 04 '22 13:11

Martin Thoma


2 Answers

This is what work for me on CE ZendServer on linux and Apache server on NetBsd

File "message.po" is generated from the root of application:

#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2013-04-30 19:38+0200\n"
"PO-Revision-Date: 2013-04-12 14:00+0000\n"
"Last-Translator: gin(e) <[email protected]>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"Language: eo\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: include/apteryx_text.php:3
msgid "email:"
msgstr "Retpoŝtadreso:"

This is the new tree-directory:

res/locale/
    de_DE/
       LC_MESSAGES/
            messages.mo
    eo_EO/
       LC_MESSAGES/
            messages.mo
    eo->eo_EO (symlink)

I must add the "eo" symlink because the only three esperanto locales on my system supported are:

eo
eo.iso88593
eo.utf8

compiled whit:

sudo locale-gen eo
sudo locale-gen eo.iso88593
sudo locale-gen eo.utf8
sudo update-locale
sudo dpkg-reconfigure locales

and at the moment my locale.php code is:

$charset="UTF-8";
$gettext_domain="messages";
$locale_dir="res/locale";
putenv("LC_ALL=$lang");
$l=split("_",$lang);
/* not in all system and not all locales got the classic name this stupid method try to solve it*/
if(! setlocale(LC_ALL, $lang.".".$charset)) 
  if(! setlocale(LC_ALL, $lang)) 
    if(! setlocale(LC_ALL,$l[0].".".$charset))  
        setlocale(LC_ALL,$l[0]);

bindtextdomain($gettext_domain, "res/locale");
textdomain($gettext_domain);
bind_textdomain_codeset($gettext_domain, $charset);

because whitout it gettext doesn't work. I think that the locale directory must have the same name of the language setted whit setlocale. Another things is to test every function called what return. Them must never return NULL or FALSE. You can do that in simple way:

var_dump(bindtextdomain($gettext_domain, "res/locale"));
var_dump(textdomain($gettext_domain)); 
..and so on..

The last but not the list, remember to set the right apache permission to all .mo file, to restart your apache server and verify by phpinfo() that "GetText Support" is "enabled".

like image 69
jedi Avatar answered Nov 15 '22 09:11

jedi


When I restart nginx through the cmd "/path/to/nginx -s reload",it dosen't work,but after I restart php-fpm with the cmd "/etc/init.d/php-fpm restart",it worked! Hope my experience will be helpful to someone with the issue :)

like image 25
Lordran Avatar answered Nov 15 '22 08:11

Lordran