Search code examples
phpgettext

PHP gettext - basic help needed


I'm trying to get a very basic setup of gettext working on my local server.

I've installed it and I can see it available when I run phpinfo().

My test php file is:

<?php
$language = 'fr';
putenv("LANG=$language"); 
setlocale(LC_ALL, $language);

// Set the text domain as 'messages'
$domain = 'messages';
bindtextdomain($domain, "/Tom/Web/locale/"); 
textdomain($domain);

echo gettext("A string to be translated would go here");
?>

In /Tom/Web/locale I have two folders en and fr

(this web root is /Tom/Web/)

Within each of those folders I have LC_MESSAGES and within that I have messages.po & messages.mo

Within the message.po file in fr/LC_MESSAGES I have :

# 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.
#
msgid ""
msgstr ""
"Project-Id-Version: TestProject\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-09-18 11:41+0100\n"
"PO-Revision-Date: 2017-09-18 12:02+0100\n"
"Language: fr\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"
"Last-Translator: \n"
"Language-Team: \n"
"X-Generator: Poedit 2.0.3\n"
"X-Poedit-SourceCharset: UTF-8\n"

#: test.php:12
msgid "A string to be translated would go here"
msgstr "Une chaîne à traduire irait ici"

Yet when I open the tstp.php page in my browser I get the english text not the French.

What have I done wrong ?

Thanks.


Solution

  • The language and directory should be fr_FR. It's a long time since I used gettext(), but I think you have the specify the country and language. (e.g. en_GB and en_US are different versions of English)