Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

147
Vistas
How to make gettext() translate strings on my pages in the simplest way

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

over 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda