开发者

PHP & GetText not working properly on my server

开发者 https://www.devze.com 2023-02-18 18:38 出处:网络
I\'m trying to setup gettext to translate my website but I keep on getting weird behaviour on my server.

I'm trying to setup gettext to translate my website but I keep on getting weird behaviour on my server.

Locally on my ubuntu machine it works fine but on my centos 5.5 dedicated server, only parts of my page are translated and when I refresh the page it actually change what part are translated.

It sounds crazy I know.

Nothing in apache logs.

Help , I'm lo开发者_StackOverflow社区st :'(


Gettext in PHP has issues. http://bugs.php.net/search.php?cmd=display&search_for=gettext&x=0&y=0 - You probably ran into one.

I cannot offer a fix nor a good debugging approach, but:

  • Make a test script, run through all known strings. Test with different charsets.
  • Try upgrading - from 5.2 to php 5.3 if that's a case.
  • Or unload the gettext module in the php.ini. And instead load it dynamically via dl().
    • Bad performance-wise, but might mitigate runtime/memory-related errors. (Which this sounds like.)
  • Don't use gettext.so, but the script implementations php-gettext or upgradephp gettext.php (stop gap measure)


1a) Check with "locale -a" if the locale exists on your webserver.
1b) Without shell try with this code, if you can set the locale

echo setlocale(LC_ALL, 0);     // read the current locale
setlocale(LC_ALL, "fa");       // try to write your locale
echo setlocale(LC_ALL, 0);     // check if your was accepted

2a) If the locale is missing add it, e.g. with "apt-get install locales".
2b) If you have no root rights, use https://launchpad.net/php-gettext as a substitute.

<?php

$locale = "fa";

// Define location and domain of translations
$directory = realpath('./') .'/translations/LOCALES';
$domain    = 'php-addressbook';
$default_locale = $locale;
$supported_locales = array($locale);


// Prepare "php-gettext"
require_once('gettext/gettext.inc');


// Prepare "native gettext" setup
T_setlocale(LC_ALL, $locale);
T_bindtextdomain($domain, $directory);
T_textdomain($domain);
T_bind_textdomain_codeset($domain, 'UTF-8');

echo T_gettext('ADDRESS');

if (!locale_emulation()) {
    print "<p>locale '$locale' is supported by your system, using native gettext implementation.</p>\n";
}
else {
    print "<p>locale '$locale' is _not_ supported on your system, using the default locale '". $default_locale ."'.</p>\n";
}

?>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号