1

I'm trying to setup localization using php-gettext, but it doesn't seem to work no matter what.

I have an index.php:

<?php require_once "localization.php";?>

<a href="?locale=en_US">English</a> |
<a href="?locale=de_DE">German</a> 

<br>
<?php echo _("Hello World!"); ?><br>
<?php echo _("My name is"); ?> Bob.

and the localization.php

<?php $locale = false;
if (isset($_GET["locale"])) { $locale = $_GET["locale"];}
putenv("LC_ALL=$locale");
setlocale(LC_ALL, $locale);
bindtextdomain("messages", "./locale");
textdomain("messages");

I also created the translation files under ./locale/de_DE/LC_MESSAGES/messages.po / .mo

I'm trying this under Ubuntu 11.04 (natty), PHP Version 5.3.5-1ubuntu7.3, apache2

Any suggestions?

stamas
  • 397
  • 1
  • 3
  • 16
  • 1
    Any errors? How do you know it's not working? Are they simply staying in English? – Tom van der Woerdt Dec 04 '11 at 17:19
  • It might help to check the return value of `setlocale()`. I believe this can return FALSE in some cases. Moving on, I had similar problems recently. Using Zend_Translate with Zend_Locale (from the Zend Framework) seemed to solve all the problems I was having in getting gettext to recognise my locale; plenty of documentation can be found through Google if you choose to look it up. – pb149 Dec 04 '11 at 17:25
  • Yes, they simply stay in English. edit: checked, it returns false indeed. – stamas Dec 04 '11 at 17:38
  • You could also try my [`gettext.php`](http://include-once.org/p/upgradephp). - It expects the language flag in `$_ENV["LANGUAGE"] = "de_DE";` instead of `setlocale` or `putenv`. – mario Dec 04 '11 at 19:44

2 Answers2

1

Try:

<?php
$directory = dirname(__FILE__).'/locale';
$domain = 'messages';
$locale ="your_locale"; //like pt_BR.utf8";

putenv("LANG=".$locale); //not needed for my tests, but people say it's useful for windows

setlocale( LC_MESSAGES, $locale);
bindtextdomain($domain, $directory);
textdomain($domain);
bind_textdomain_codeset($domain, 'UTF-8');
?>
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
  • 3
    Copy&pasting from the PHP manual comments does require attribution. And an answer that is just `Try:` and a blob of code isn't very pretty anyway. (It's also meant for the actual gettext extension, less likely to help with the userland php-gettext.) – mario Dec 04 '11 at 17:35
  • This gentleman's answer helped me ! Make sure your locale is recognized by your server using the 'locale -a' command ? And check the return values of all these methods to see where it bugs :) – Laila Nov 05 '14 at 23:33
  • What Laila said, and `putenv("LANG=".$locale)` is not only useful for Windows. It is the reason why so many users complain that MAMP crashes on the simple call of gettext() even if package installed. Without it, for whatever reason MAMP will immediately crash with an Internal Server Error 500 and NOTHING logged to error logs. –  Jul 04 '22 at 06:44
0

Maybe much too late, but I had the same problem:

1st -You need to add .utf8 to your $locale string 2nd - check that your system supports the language using locale -a if not install the language e.g. de_DE: sudo apt-get install language-pack-de-base

$locale = "de_DE.utf8";
if (isset($_GET["locale"])) $locale = $_GET["locale"];

$domain = 'messages';
$directory = dirname(__FILE__);//your path to locale folder may differ

setlocale( LC_MESSAGES, $locale);
bindtextdomain($domain, $directory);
textdomain($domain);
bind_textdomain_codeset($domain, 'UTF-8');
Karl Adler
  • 15,780
  • 10
  • 70
  • 88