[SOLVED] Problem with gettext

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Ignatius881
Forum Newbie
Posts: 17
Joined: Thu Jul 14, 2011 9:09 am
Location: Oviedo (Spain)

[SOLVED] Problem with gettext

Post by Ignatius881 »

Hello.

I'll explain my problem.

First of all, I have to say that I'm using Linux and I have Apache, PHP, MySQL, phpmyadmin and two packages called php-gettext and poedit.

Well, I've followed several tutorials in Internet to make a multilingual webpage, or even a file. So, I create a file called test.php and I write this:

Code: Select all

<?
echo _("Hello world");
?>
Then I create the .po file and the directories (locale/language_COUNTRY/LC_MESSAGES). I copy the file messages.po into locale/en_GB/LC_MESSAGES, then I write the translation and move messages.po (which I've made a copy from) into locale/es_ES/LC_MESSAGES. And I create the .mo files.

Finally, I edit the test.php file in this way:

Code: Select all

<?
$language=(isset($_REQUEST['language']))?trim(strip_tags($_REQUEST['language'])):"en_GB";
putenv("LC_ALL=$language");
setlocale(LC_ALL, $language);
bindtextdomain("messages", "./locale");
textdomain("messages");

print "<p><a href=\"".$_SERVER['PHP_SELF']."?language=en_GB\">English</a> -
<a href=\"".$_SERVER['PHP_SELF']."?language=fr_FR\">French</a> -
  <a href=\"".$_SERVER['PHP_SELF']."?language=es_ES\">Espa&ntilde;ol</a></p>\n";

echo _("Hello world");

?>
And here is the problem: I can't see the text translated, only the original version. No matter what I do, I never can see the translations.

Can you help me? I hope I've explained everything well.

EDIT: I've followed this tutorial, step by step: http://mel.melaxis.com/devblog/2005/08/ ... g-gettext/

And I can't see the text in German, only and always in English :banghead:
Last edited by Ignatius881 on Sun Jul 31, 2011 10:49 am, edited 1 time in total.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Problem with gettext

Post by Christopher »

Ignatius881 wrote:And I can't see the text in German, only and always in English :banghead:
If you want to see translations in German, you need to create a file "locale/de_DE/LC_MESSAGES/messages.po" and generate a .mo file for it.
(#10850)
Ignatius881
Forum Newbie
Posts: 17
Joined: Thu Jul 14, 2011 9:09 am
Location: Oviedo (Spain)

Re: Problem with gettext

Post by Ignatius881 »

Christopher wrote:
Ignatius881 wrote:And I can't see the text in German, only and always in English :banghead:
If you want to see translations in German, you need to create a file "locale/de_DE/LC_MESSAGES/messages.po" and generate a .mo file for it.
Yes, that's what I've done time and again, but nothing.

I also added extension=gettext.so into the php.ini file, that is located in /etc/php5/apache2 (and another in /etc/php5/cli), and I restarted apache.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Problem with gettext

Post by Christopher »

What is the value of:

$language=(isset($_REQUEST['language']))?trim(strip_tags($_REQUEST['language'])):"en_GB";
(#10850)
Ignatius881
Forum Newbie
Posts: 17
Joined: Thu Jul 14, 2011 9:09 am
Location: Oviedo (Spain)

Re: Problem with gettext

Post by Ignatius881 »

Christopher wrote:What is the value of:

$language=(isset($_REQUEST['language']))?trim(strip_tags($_REQUEST['language'])):"en_GB";
Mmm, how can I know that? I'm not an expert with PHP :oops:
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Problem with gettext

Post by Christopher »

You don't need to be an expert to know this: ;)

Code: Select all

$language=(isset($_REQUEST['language']))?trim(strip_tags($_REQUEST['language'])):"en_GB";
echo "language=$language<br/>";
(#10850)
Ignatius881
Forum Newbie
Posts: 17
Joined: Thu Jul 14, 2011 9:09 am
Location: Oviedo (Spain)

Re: Problem with gettext

Post by Ignatius881 »

Excuse me, I was on holiday.

Okay, I was working again with gettext, with no success. I've followed this link again, http://mel.melaxis.com/devblog/2005/08/ ... g-gettext/, step by step again.

Even I've installed Chinese and Japanese locales, translations and fonts to work with them in Ubuntu, but nothing.

I don't know if I'm doing something wrong or not (maybe yes).

Can you show me other tutorials, or your .php files?
Ignatius881
Forum Newbie
Posts: 17
Joined: Thu Jul 14, 2011 9:09 am
Location: Oviedo (Spain)

Re: Problem with gettext

Post by Ignatius881 »

Ok, I solved the problem. It was a stupid thing, but I had to write "en_GB.utf8" instead of "en_GB", according to the output command locale -a :oops:

Well, thanks for your help.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: [SOLVED] Problem with gettext

Post by VladSun »

Don't use $_SERVER['PHP_SELF'] - it's XSS vulnerable.
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply