简体   繁体   中英

Internationalization with gettext on Windows 7 home and PHP 7.0.8 fails

At work we have decided to translate our site into 3 additional languages and I have been looking into how to do this. I've spent the better part of the day searching google and stackoverflow, trying every different solution but I can't make it work.

I'm starting to fear that it is impossible to do in Windows 7 Home edition (My development machine).

// C:\Apache24\htdocs\locale\en_GB\LC_MESSAGES\messages.mo
// C:\Apache24\htdocs\locale\en_GB\LC_MESSAGES\messages.po
msgid ""
msgstr ""
"Project-Id-Version: iArbete 1.0.0\n"
"Report-Msgid-Bugs-To: ******** \n"
"Last-Translator: ********\n"
"Language: en_GB\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

msgid "iArbete"
msgstr "workIng"


// C:\Apache24\htdocs\index.php
if(isset($_GET["locale"]))
{
    $locale = $_GET["locale"];
}
else
{
    $locale = 'en_GB';
}

putenv("LC_ALL=$locale");
setlocale(LC_ALL, $locale);
echo getenv("LC_ALL");

$domain = 'messages';
bindtextdomain($domain, './locale');
textdomain($domain);
bind_textdomain_codeset($domain, 'UTF-8');

echo _("iArbete");

echo _("iArbete"); Should translate to "workIng" but it does not. Or am I not getting the concept?

--- More information --------------------------------------------------------

I've made some changes and recompiled the mo file and this is how it looks now.

// C:\Apache24\htdocs\locale\en_GB\LC_MESSAGES\messages.mo
// C:\Apache24\htdocs\locale\en_GB\LC_MESSAGES\messages.po
msgid ""
msgstr ""
"Project-Id-Version: iArbete 1.0.0\n"
"Report-Msgid-Bugs-To: ******** \n"
"Last-Translator: ********\n"
"Language: en_GB\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

msgid "workIng"
msgstr "iArbete"

msgid "Menu"
msgstr "Meny"


// C:\Apache24\htdocs\index.php
if(isset($_GET["locale"]))
{
    $locale = $_GET["locale"];
}
else
{
    $locale = 'sv_SE';
    $locale = 'en_GB';
//  $locale = 'ar_AE';
}

putenv("LC_ALL=$locale");
setlocale(LC_ALL, $locale);
//  echo getenv("LC_ALL");

$domain = 'messages';
bindtextdomain($domain, './locale');
textdomain($domain);
bind_textdomain_codeset($domain, 'UTF-8');

echo _("workIng");
echo _("Menu");

msgid "workIng" is translated to "iArbete" msgid "Menu" is not translated to "Meny"

What is going on? It feels so weird that only the first word get translated. So I rearranged the words in the po and recompiled but still "workIng" is the only word that gets translated??

I've uploades my files to google drive here if anyone would like to have a look.

You'll need to convert your PO file into a MO file using msgfmt :

The letters PO in '.po' files means Portable Object, to distinguish it from '.mo' files, where MO stands for Machine Object. [...]

PO files are meant to be read and edited by humans, and associate each original, translatable string of a given package with its translation in a particular target language. [...]

MO files are meant to be read by programs, and are binary in nature. [...]

Once the PO file is complete and dependable, the 'msgfmt' program is used for turning the PO file into a machine-oriented format, which may yield efficient retrieval of translations by the programs of the package, whenever needed at runtime (see MO Files ). See msgfmt Invocation , for more information about all modes of execution for the 'msgfmt' program.

— The GNU gettext manual, sections 1.4 and 1.5

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