gettext 在一个文件中有效,而在另一个文件中无效?
我在让 gettext 工作时遇到了一些麻烦。我制作了一个简单的测试文件,我在其中调用 translate.php 和 echo T_("XXXXX") 并对其进行翻译,但是当我尝试在函数中使用 echo T_ 时,它不起作用。.translate.php
:
<?php
error_reporting(E_ALL | E_STRICT);
// define constants
define('PROJECT_DIR', realpath('./functions/'));
//define('LOCALE_DIR', PROJECT_DIR .'/functions/locale');
define('LOCALE_DIR', PROJECT_DIR .'locale');
define('DEFAULT_LOCALE', 'en_US');
require_once('gettext.inc');
$supported_locales = array('en_US', 'sr_CS', 'de_CH');
$encoding = 'UTF-8';
$locale = (isset($_GET['lang']))? $_GET['lang'] : DEFAULT_LOCALE;
// gettext setup
T_setlocale(LC_MESSAGES, $locale);
// Set the text domain as 'messages'
$domain = 'messages';
T_bindtextdomain($domain, LOCALE_DIR);
T_bind_textdomain_codeset($domain, $encoding);
T_textdomain($domain);
//header("Content-type: text/html; charset=$encoding");
?>
工作测试文件:
<?php
require("translate.php");
echo T_("test");
?>
这只是一个测试,看看它是否有效,并且“测试”一词得到了翻译,正如我希望实现的那样。对于实际的 php 文件来说,它会变得稍微复杂一些。
info.php
<?php
require("functions\info_functions.php");
(...)
class infopage extends Page
{
public function display()
{
(...)
displayInfo();
(...)
}
}
$homepage = new infopage();
$homepage->display();
?>
info_functions.php - 这里的 echo 没有被翻译!
<?php
require("translate.php");
echo T_("test");
function displayInfo()
{
(...)
echo T_("test");
(...)
}
?>
I have a little trouble getting gettext to work. I made a simple test file where I call the translate.php and echo T_("XXXXX") and It get translated, but when I try to use echo T_ in a function it doesn't work..
translate.php:
<?php
error_reporting(E_ALL | E_STRICT);
// define constants
define('PROJECT_DIR', realpath('./functions/'));
//define('LOCALE_DIR', PROJECT_DIR .'/functions/locale');
define('LOCALE_DIR', PROJECT_DIR .'locale');
define('DEFAULT_LOCALE', 'en_US');
require_once('gettext.inc');
$supported_locales = array('en_US', 'sr_CS', 'de_CH');
$encoding = 'UTF-8';
$locale = (isset($_GET['lang']))? $_GET['lang'] : DEFAULT_LOCALE;
// gettext setup
T_setlocale(LC_MESSAGES, $locale);
// Set the text domain as 'messages'
$domain = 'messages';
T_bindtextdomain($domain, LOCALE_DIR);
T_bind_textdomain_codeset($domain, $encoding);
T_textdomain($domain);
//header("Content-type: text/html; charset=$encoding");
?>
working test file:
<?php
require("translate.php");
echo T_("test");
?>
That was just a test to see if it worked and the "test" word got translated as I was hoping to achieve. It gets a little bit more complicated with actual php files.
info.php
<?php
require("functions\info_functions.php");
(...)
class infopage extends Page
{
public function display()
{
(...)
displayInfo();
(...)
}
}
$homepage = new infopage();
$homepage->display();
?>
info_functions.php - Here the echo doesn't get translated!
<?php
require("translate.php");
echo T_("test");
function displayInfo()
{
(...)
echo T_("test");
(...)
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检查您的
LOCALE_DIR
环境变量是否确实指向displayInfo()
中的正确位置。来自:看起来它可能是一个相对路径,在
info_functions.php
中不起作用,因为它与其他(测试)文件位于不同的目录中。Check if your
LOCALE_DIR
environment variable is actually pointing to the correct place withindisplayInfo()
. From:It looks like it may be a relative path which doesn't work from within
info_functions.php
since it is in a different directory to your other (test) files.