ConFoo Montreal 2026: Call for Papers

Voting

: nine minus zero?
(Example: nine)

The Note You're Voting On

marioandrea dot petruccelli at gmail dot com
7 years ago
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 _("Good Morning");
?>

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 fr\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 "Good Morning"
msgstr "Bonjour"

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/fr_FR/LC_MESSAGES

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

10] Now edit the index.php as follows

<?php

$locale
= "fr_FR";

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


textdomain("messages");

echo
_("Good Morning");
?>

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

If it was usefull for you vote up!

Visit my github profile https://github.com/TheoRelativity/

<< Back to user notes page

To Top