3

I found two approaches to get gettext working in PHP but neither seems to work. How do I make the translated h1 tag visible. The ouput should be Willkommen (Welcome in german = Willkommen). Gettext seems to be installed properly. The only output I get are blank pages.

My specs:

OS: Windows 10
Gettext 0.21 with Libiconv 1.16 (installed with executable)
Dev Environment: PhpStorm
PHP: 8.08

Approach 1:

<?php

//  APPROACH 1:
  error_reporting(E_ALL); // 32767
  ini_set("display_errors", "On"); // On
  $lang = "de"; // de
  $domain = "messages"; // messages
  setlocale(LC_ALL, $lang . ".utf8", $lang . "utf-8", $lang . "UTF-8", $lang); // de.utf8
  putenv("LC_ALL=de"); // 1
  bindtextdomain($domain, __DIR__ . "/locale"); // C:\MAMP\htdocs\test\locale
  bind_textdomain_codeset($domain, "utf-8"); // utf-8
  textdomain($domain); // messages

?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
  <h1><?php _("Welcome") ?></h1>
</body>
</html>

Approach 2:

<?php

//  APPROACH 2:
  $locale = "de"; // de
  putenv("LANG = $locale"); // 1
  putenv("LANGUAGE = $locale"); // 1
  setlocale(LC_ALL, $locale); // de
  $domain = "messages"; // messages
  textdomain($domain); // messages
  bindtextdomain($domain, "locale"); // C:\MAMP\htdocs\test\locale
  bind_textdomain_codeset($domain, "UTF-8"); // UTF-8

?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
  <h1><?php _("Welcome") ?></h1>
</body>
</html>

My dir tree looks like this:

enter image description here

messages.mo:

Þ•          ,      <       P      Q   >  Y   
   ˜                     Welcome Project-Id-Version: 
PO-Revision-Date: 2021-08-10 17:58+0200
Last-Translator: 
Language-Team: 
Language: de
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Generator: Poedit 3.0
X-Poedit-Basepath: ../../..
Plural-Forms: nplurals=2; plural=(n != 1);
X-Poedit-SearchPath-0: .
 Willkommen 

messages.po:

msgid ""
msgstr ""
"Project-Id-Version: \n"
"POT-Creation-Date: 2021-08-10 17:58+0200\n"
"PO-Revision-Date: 2021-08-10 17:58+0200\n"
"Last-Translator: \n"
"Language-Team: \n"
"Language: de\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"
"X-Poedit-Basepath: ../../..\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"X-Poedit-SearchPath-0: .\n"

#: test.php:21
msgid "Welcome"
msgstr "Willkommen"

Edit:

Inspecting the gettext function _(...) in the browser shows this:

<script>
(function() {
  var ws = new WebSocket('ws://' + window.location.host + '/jb-server-page?reloadServiceClientId=65');
  ws.onmessage = function (msg) {
      if (msg.data === 'reload') {
          window.location.reload();
      }
      if (msg.data.startsWith('update-css ')) {
          var messageId = msg.data.substring(11);
          var links = document.getElementsByTagName('link');
          for (var i = 0; i < links.length; i++) {
              var link = links[i];
              if (link.rel !== 'stylesheet') continue;
              var clonedLink = link.cloneNode(true);
              var newHref = link.href.replace(/(&|\?)jbUpdateLinksId=\d+/, "$1jbUpdateLinksId=" + messageId);
              if (newHref !== link.href) {
                clonedLink.href = newHref;
              }
              else {
                var indexOfQuest = newHref.indexOf('?');
                if (indexOfQuest >= 0) {
                  // to support ?foo#hash 
                  clonedLink.href = newHref.substring(0, indexOfQuest + 1) + 'jbUpdateLinksId=' + messageId + '&' + 
                                    newHref.substring(indexOfQuest + 1);
                }
                else {
                  clonedLink.href += '?' + 'jbUpdateLinksId=' + messageId;
                }
              }
              link.replaceWith(clonedLink);
          }
      }
  };
})();
</script>

Edit2: Followed @Alexandre's instructions but the page only shows Welcome not Willkommen

enter image description here

messaged.po:

# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2021-08-16 14:40+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: de_DE \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: index.php:16
msgid "Welcome"
msgstr "Willkommen"

messaged.mo:

��          ,      <       P      Q     Y   
   w                     Welcome Project-Id-Version: PACKAGE VERSION
Report-Msgid-Bugs-To: 
PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE
Last-Translator: FULL NAME <EMAIL@ADDRESS>
Language-Team: LANGUAGE <LL@li.org>
Language: de_DE 
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
 Willkommen 
Artur Müller Romanov
  • 4,417
  • 10
  • 73
  • 132

2 Answers2

1

How to use gettext on Windows.

If you use Linux start from the step 2 and consider cmd as linux shell.

1] First you have to download and install this

https://mlocati.github.io/articles/gettext-iconv-windows.html

Check all options during the installation and go on.

2] Create a index.php file into your website directory with this code inside:

<?php
    echo  _("Welcome");
?>

3] Open cmd and move into your website folder using cd

4] Now create the .mo files directly from the php files using this command

xgettext -n index.php

Use xgettext -help to see how to include more php files.

5] Once finished will be generate a file called messages.mo. Now you have to set the language and the charset. Open messages.mo with notepad and edit the lines:

"Language: \n" BECOMES "Language de\n"
"Content-Type: text/plain; charset=CHARSET\n" BECOMES "Content-Type: text/plain; charset=UTF-8\n"

6] Remember to translate every line where is present msgstr "", translating the instance msgid into language you have chosen. For instance:

#: index.php:2
msgid "Welcome"
msgstr "Willkommen"

7] Once finished open cmd and move into your website folder using cd and then type msgfmt messages.po

This will make a file called messages.mo . Is a binary version of the messages.po file

8] Now you have to create a folder structure like this into your website folder. Do this for each language you want to add.

website/locale/de_DE/LC_MESSAGES

9] Move messages.mo and messages.po in locale/de_DE/LC_MESSAGES

10] Now edit the index.php as follows

<?php

$locale = "de_DE";

    if (defined('LC_MESSAGES')) {
        setlocale(LC_MESSAGES, $locale); // Linux
        bindtextdomain("messages", "./locale");
    } else {
        putenv("LC_ALL={$locale}"); // windows
        bindtextdomain("messages", ".\locale");
    }

   
    textdomain("messages");

    echo _("Welcome");
?>

11] Open index.php in your browser and if you will see "Willkommen" it means everything is okay. If not, start from the step 2 again.

Source : https://www.php.net/manual/en/book.gettext.php#122509

Alexandre Elshobokshy
  • 10,720
  • 6
  • 27
  • 57
0

Just make sure you have the same locale installed on your Dev server. locale -a Will give you list of available locales

Alex Kapustin
  • 1,869
  • 12
  • 15