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

276

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

Answer

Solution:

$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) usesetlocale(LC_ALL, 'deu_deu'); which may upset the live site

2) addputenv('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");

People are also looking for solutions to the problem: php - Doctrine ODM (MongoDB) MapReduce + Skip

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.