简体   繁体   中英

Translation to hebrew with PHP/Gettext fails

I've searched hours for a solution for this including in the documentation.

trying to use gettext for hebrew translations,

using PHP 5.3.1 and wamp,

it prints out "hello world" and not the Hebrew equivalent

$directory = '/locale';
$domain = 'messages';
$locale ="he_IL";

putenv("LANG=".$locale); //not needed for my tests, but people say it's useful for windows

setlocale( LC_ALL, $locale);
bindtextdomain($domain, $directory);
textdomain($domain);
bind_textdomain_codeset($domain, 'UTF-8');

echo _("hello world");

I use poedit to create the mo/po files, they are located on: ./locale/he_IL/LC_MESSAGES/messages.mo

the php file is at "./"

Why don't I get the hebrew text?

Ok solved,

Had to update to PHP version 5.3.5/5.3.10

and because I'm using windows I had to use this locale "Hebrew_Israel.1255" instead of "he_IL" (that's how windows calls the hebrew locale anyway).

Of course I had to also rename the folders in the ./locale to "Hebrew_Israel.1255"

Now the system successfully chooses the locale

as pointed out by MarcB, PHP isn't magically going to translate the words into different language for you. but there are several different solutions out there to help you out. you can make use of google translator API to convert the text between different languages for example.

 try {
       $gt = new Gtranslate;
       echo "Translating [Hello World] from English to German => ".$gt->english_to_german("hello world")."<br/>";
       echo "Translating [Ciao mondo] Italian to English => ".$gt->it_to_en("Ciao mondo")."<br/>";
 } catch (GTranslateException $ge) {
       echo $ge->getMessage();
 }

you can read more about google translator API here http://code.google.com/p/gtranslate-api-php/

You should check that "he_IL" locale is installed in your system. I don't know how to do it in Windows, but in Linux you can run "locale -a"(http://linux.about.com/library/cmd/blcmdl1_locale.htm) to see all locales installed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM