0

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");
  • Have you rebooted the server after making the changes? – CD001 Mar 24 '17 at 13:12
  • Yes every time. but no response, please help where I'm doing mistake. I tired :( Thanks for reply – Ravi Verma Mar 24 '17 at 13:15
  • Just nosing through... your `$pathToDomain` should be *C:\xampp\htdocs\Test_project\Locale* – CD001 Mar 24 '17 at 13:25
  • ... also, on Windows, the language should probably be something like `eng_gbr` – CD001 Mar 24 '17 at 13:27
  • This also I tries , but same result. before I was using the $pathToDomian = 'C:\xampp\htdocs\Test_project\Locale'; but result was same, and now also I have changed and rebooted the server. again same result. – Ravi Verma Mar 24 '17 at 13:29
  • Actually I have changed the mojila firefox language English(en,US) to German (de_DE), So its tacking browser language , I have done all required process for German – Ravi Verma Mar 24 '17 at 13:32
  • Windows doesn't use standard `LC_ALL` type environment variables you either need to add `putenv("LC_ALL=" . $language);` or use the Windows specific locale code `deu_deu` – CD001 Mar 24 '17 at 14:01
  • UTF-8 doesn't work brilliantly (or at all) on Windows either : http://stackoverflow.com/questions/10995953/php-setlocale-in-windows-7 – CD001 Mar 24 '17 at 14:08
  • Just now this also,but giving same result. Still not getting what is the mistake I'm doing. I have followed https://blog.udemy.com/php-gettext/ completely. – Ravi Verma Mar 24 '17 at 14:13

1 Answers1

1
$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");
CD001
  • 8,332
  • 3
  • 24
  • 28
  • Yes, you are right echo setlocale(LC_ALL, $language) ? "true" : "false"; giving me false answer, so now what to d.? – – Ravi Verma Mar 24 '17 at 14:38
  • I have edited accordingly to you, again. but still getting same result . I created Locale files lie that only for de_DE and checked inside gettext share/Locale folder the "de" folsder is existing with LC_MESSAGES folder – Ravi Verma Mar 24 '17 at 14:45
  • echo setlocale(LC_ALL, $language) ? "true" : "false"; giving me "false" result. it means setlocale is not setting language . – Ravi Verma Mar 24 '17 at 14:58
  • ... annoyingly it's stopped working on my dev box now, there was an issue a while back with a particular version of libxml which meant that gettext didn't work on Windows with PHP 5.3 ... – CD001 Mar 24 '17 at 15:03
  • Nope - gettext is working fine, I can translate from fr_FR to en_GB but I can't get Windows to accept a change in locale via PHP - even when `setlocale` actually works correctly: `echo setlocale(LC_ALL, 0)` results in "French_France.1252" - yet the translations are always being French > English not the other way around ... I'll keep digging, it's annoying me now. – CD001 Mar 24 '17 at 15:14
  • I am using php7 and also , I'm checking the gettext working or not by using this code. if (function_exists("gettext")){ echo "gettext working fine"; }else{ echo "Extra stuff must be installed"; } and getting result as "gettext working fine" so it means?? – Ravi Verma Mar 24 '17 at 15:16
  • Actually I need to translate this first in fr_FR only, but I want to create the system for all languages. and I googled german having de_de code browsers accept. – Ravi Verma Mar 24 '17 at 15:19
  • Seems it hasn't really worked on Windows since PHP 5.3 at all : https://bugs.php.net/bug.php?id=66265 – CD001 Mar 24 '17 at 15:51