i am trying to use the php gettext extension in php 5.5 (on win2008 server, using IIS7). I am doing this:
<?php
$locale = "es";
if (isSet($_GET["locale"])) $locale = $_GET["locale"];
putenv("LC_ALL=$locale");
setlocale(LC_ALL, $locale);
bindtextdomain("messages", "./locale");
textdomain("messages");
echo gettext("Hello world");
?>
With this folder structure in place:
locale/es/LC_MESSAGES/messages.mo
But it always just returns Hello world and not the correct translation which for now (based on my lack of spanish skills) is this in the messages.po file:
msgid ""
msgstr ""
"Project-Id-Version: TestXlations\n"
"POT-Creation-Date: 2014-04-19 08:15-0500\n"
"PO-Revision-Date: 2014-04-19 09:18-0500\n"
"Language-Team: \n"
"Language: es\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Poedit 1.6.3\n"
"X-Poedit-Basepath: .\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"X-Poedit-SearchPath-0: c:/dev\n"
msgid "Hello world"
msgstr "Hola World"
This fails from the cmd line and via IIS. So i it's seeing the gettext call, etc and executing it but it's not reading the translation file. how can i debug this further? even if remove the translation file, i get the same behavior.
The gettext module in PHP only supports languages and language identifiers that are installed on your server. on the command line of your server (e.g., by logging in with SSH). The command may require admin privileges and maybe not work on a hosted shared server.
October 28, 2020 · 16 min read. GNU gettext is a package that offers to programmers, translators and even users a well integrated set of tools that provide a framework within which other free packages may produce multi-lingual messages.
You should check returned values and know which function failed. It is not i18n specific but useful for any PHP scripts, or any programming language debugging.
<?php
$locale = 'es';
if (isset($_GET["locale"])) $locale = $_GET["locale"];
$domain = 'messages';
$results = putenv("LC_ALL=$locale");
if (!$results) {
exit ('putenv failed');
}
// http://msdn.microsoft.com/en-us/library/39cwe7zf%28v=vs.100%29.aspx
$results = setlocale(LC_ALL, $locale, 'spanish');
if (!$results) {
exit ('setlocale failed: locale function is not available on this platform, or the given local does not exist in this environment');
}
$results = bindtextdomain($domain, "./locales");
echo 'new text domain is set: ' . $results. "\n";
$results = textdomain($domain);
echo 'current message domain is set: ' . $results. "\n";
$results = gettext("Hello world");
if ($results === "Hello world") {
echo "Original English was returned. Something wrong\n";
}
echo $results . "\n";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With