views:

15

answers:

1

Hi. 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  A: 

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.

Oren