Ravi Verma
Ravi Verma

Reputation: 1

gettext is not giving result from .mo file, it is showing same as I am writing in echo

I am beginner and doing first time language translate for my web application. kindly help me , where I am doing mistake, but I am not getting the result i'm expecting. I have installed gettex, I have changed in php.ini file for to remove extention(;), i have restarted many time the server, I creating .po file from poedit and mdifying evrytime the .mo file. I don't know where I am wrong. Please help..!

I would like to share the out put also, as I am printing many things for checking the right thing. in my .po file I wrote:

        # Test 1
        msgid "This is moon"
        msgstr "This is Sun"

        So it must print "This is Sun", but its printing:
        gettext working fine
        Locale Language::de_DE
        C:\xampp\htdocs\Test_project\Locale\de_DE\LC_MESSAGES
        Path is Correct!!
        C:\xampp\htdocs\Test_project\Locale\de_DE\LC_MESSAGES
        This is moon
        [result][1]

        here is my code

if (function_exists("gettext")){
    echo "gettext working fine";
}else{ 
    echo "Extra stuff must be installed";
}
echo "<br>";
$language = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
echo "Locale Language::".$language;
echo "<br>";
putenv("LANG=". $language);
setlocale(LC_ALL, $language);

// set the text domain as "messages"
$domain = "messages";
$pathToDomain = 'C:\xampp\htdocs\Test_project\Locale\de_DE\LC_MESSAGES';
echo $pathToDomain;
echo "<br>";
if ($pathToDomain != bindtextdomain($domain, $pathToDomain)) {
    // Error handling.
    $hi = bindtextdomain($domain, $pathToDomain);
    echo "hello".$hi;
    echo "<br>";
    echo "Path Incorrect";

}
else{
    $hi = bindtextdomain($domain, $pathToDomain);
        echo "Path is Correct!!";
        echo "<br>";
        echo $hi;

    }
    bindtextdomain($domain, $pathToDomain);
    bind_textdomain_codeset($domain, 'UTF-8');
    textdomain($domain);
    echo "<br>";
    echo gettext("This is moon");

Upvotes: 0

Views: 999

Answers (1)

CD001
CD001

Reputation: 8472

$language = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);

Will give you a locale string such as de_DE - however Windows won't accept that string, it would need to be something like deu_deu

You can test this with:

echo setlocale(LC_ALL, $language) ? "true" : "false";

There are two ways around this:

1) use setlocale(LC_ALL, 'deu_deu'); which may upset the live site

2) add putenv('LC_ALL=' . $language);

In a nutshell - when you strip your code right back, this should work on Windows:

$language = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);

putenv('LANG=' . $language);
if(!defined('LC_ALL')) putenv('LC_ALL=' . $language);
setlocale(LC_ALL, $language);

$domain = 'messages';
$pathToDomain = 'C:\xampp\htdocs\Test_project\Locale';

bindtextdomain($domain, $pathToDomain);
bind_textdomain_codeset($domain, 'UTF-8');
textdomain($domain);

echo _("This is moon");

Upvotes: 1

Related Questions