【问题标题】:gettext works in one file and not the other?gettext 在一个文件中工作,而不在另一个文件中工作?
【发布时间】:2010-07-19 22:17:44
【问题描述】:

让 gettext 工作有点麻烦。我做了一个简单的测试文件,我在其中调用 translate.php 和 echo T_("XXXXX") 并且它被翻译了,但是当我尝试在函数中使用 echo T_ 时它不起作用..

翻译.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 - 这里的回声没有被翻译!

<?php

require("translate.php"); 

echo T_("test"); 

            function displayInfo()
            {

            (...)

            echo T_("test"); 

            (...)

            }

?>  

【问题讨论】:

    标签: php class function translation gettext


    【解决方案1】:

    检查您的LOCALE_DIR 环境变量是否实际上指向displayInfo() 中的正确位置。来自:

    // define constants
    define('PROJECT_DIR', realpath('./functions/'));
    //define('LOCALE_DIR', PROJECT_DIR .'/functions/locale');
    define('LOCALE_DIR', PROJECT_DIR .'locale');
    

    看起来它可能是在 info_functions.php 中不起作用的相对路径,因为它与您的其他(测试)文件位于不同的目录中。

    【讨论】:

      猜你喜欢
      • 2014-09-20
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-27
      • 2021-10-14
      • 2020-06-29
      • 1970-01-01
      相关资源
      最近更新 更多