I have a debian + apache2 installation with also gettext package installed. I try to perform a simple task - to translate a cuople of strings with gettext() + msgfmt + .po and .mo files.
So here is the script:
<?php
// Set language to Russian
putenv('LC_ALL=ru_RU');
setlocale(LC_ALL, 'ru_RU');
// Specify location of translation tables
bindtextdomain("test", "./locale");
// Choose domain
textdomain("test");
$pofilename = './locale/ru_RU/LC_MESSAGES/test.po';
$mofilename = './locale/ru_RU/LC_MESSAGES/test.mo';
if (file_exists($pofilename))
{
echo "file .po exists<br>";
}
else
{
echo "file .po does not exist<br>";
}
if (file_exists($mofilename))
{
echo "file .mo exists<br>";
}
else
{
echo "file .mo does not exist, will be created now<br>";
// to generate .mo from .po
exec("msgfmt ./locale/ru_RU/LC_MESSAGES/test.po -o ./locale/ru_RU/LC_MESSAGES/test.mo");
}
// Translation is looking for in ./locale/ru_RU/LC_MESSAGES/test.mo now
// Print a test message
echo gettext("Welcome to My PHP Application");
echo "<br>";
// Or use the alias _() for gettext()
echo _("Have a nice day");
echo "<br>";
?>
Here is test.po file:
# translation to ru_RU
msgid ""
msgstr ""
"Project-Id-Version: test\n"
"Report-Msgid-Bugs-To: \n"
"Last-Translator: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Poedit 3.0\n"
"Plural-Forms: nplurals=1; plural=0;\n"
"POT-Creation-Date: \n"
"PO-Revision-Date: \n"
"Language-Team: \n"
"Language: ru\n"
msgid "Welcome to My PHP Application"
msgstr "Всем привет"
msgid "Have a nice day"
msgstr "Хорошего дня"
But it does not translate when i run the php script - just shows non-translated things. What is wrong here? The "test.php" script and the folder "/locale" are in the same directory, .po and .mo files are inside "/locale/ru_RU/LC_MESSAGES/" folder, .mo file is generated succesfully...