Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

145
Views
Cómo hacer que gettext() traduzca cadenas en mis páginas de la forma más sencilla

Tengo una instalación de debian + apache2 con el paquete gettext también instalado. Intento realizar una tarea simple: traducir un par de cadenas con archivos gettext() + msgfmt + .po y .mo.

Así que aquí está el guión:

 <?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>"; ?>

Aquí está el archivo test.po:

 # 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 "Хорошего дня"

Pero no se traduce cuando ejecuto el script php, solo muestra cosas no traducidas. ¿Que esta mal aquí? El script "test.php" y la carpeta "/locale" están en el mismo directorio, los archivos .po y .mo están dentro de la carpeta "/locale/ru_RU/LC_MESSAGES/", el archivo .mo se genera correctamente...

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!