2

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...

creators
  • 179
  • 8
  • Are you by anc chance trying to run this on a Windows system? Because I can't get it to work on my local Laragon-powered server either, and it seems to be a known issue, see https://stackoverflow.com/questions/1473207/php-gettext-on-windows – Constantin Groß Aug 04 '21 at 13:09
  • I have no windows machine, only debian – creators Aug 04 '21 at 13:18
  • 1
    Here are some things you can try: 1) Add the codeset to the locale name: `ru_RU.utf8` 2) rename the folder `/locale/ru_RU/` to just `/locale/ru/` or `/locale/ru_RU.utf8` 3) Also set `putenv("LANGUAGE=ru_RU")` and `putenv("LANG=ru_ru")` – Constantin Groß Aug 04 '21 at 13:22
  • i tried this, nothing happens – creators Aug 04 '21 at 13:29
  • dear Constantin, anyway, you helped me to solve the problem) there was NO ru_RU locale on my new debian installation, only en_US. So i run from the console "dpkg-reconfigure locales", added the ru_RU locale and now it is working! Thank you! – creators Aug 04 '21 at 14:07
  • Glad you were able to figure it out! – Constantin Groß Aug 04 '21 at 14:39

0 Answers0