duanpao6163 2010-07-19 22:17
浏览 19
已采纳

gettext在一个文件中工作而不在另一个文件中?

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

            (...)

            }

?>  
  • 写回答

1条回答 默认 最新

  • douwen7905 2010-07-19 22:33
    关注

    Check if your LOCALE_DIR environment variable is actually pointing to the correct place within displayInfo(). From:

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

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器