php-gettext isn't working

試著忘記壹切 提交于 2019-12-24 19:03:30

问题


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?


回答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');
?>



回答2:


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');


来源:https://stackoverflow.com/questions/8377072/php-gettext-isnt-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!