John Davidson

GetText and PO files not working on XAMPP, PHP 7.3


I know there's a bunch of posts out there about the same problem, but I really can't figure it out.

Running XAMPP on OS X, with PHP 7.3.23

PHP snipet :

$language = "fr_CA";
//if(!defined('LC_ALL')) putenv('LC_ALL=' . $language);
$setlocale = setlocale(LC_ALL, $language);
$domain = "textdomain";
$bindtextdomain = bindtextdomain($domain, realpath(dirname(__FILE__)."/locale"));
$textdomain = textdomain($domain);
bind_textdomain_codeset($domain, 'UTF-8');

var_dump($setlocale); // Returns : string(5) "fr_CA"
var_dump($bindtextdomain); // Returns : string(57) "/ABSOLUTE_PATH_TO_WEBSITE/locale"
var_dump($textdomain); // Returns : my textdomain

echo _("Ceci est un test"); // Returns : the same (instead of "TEST REUSSI")

PO file:

"Project-Id-Version: Projet\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2020-11-29 10:56-0500\n"
"PO-Revision-Date: 2020-11-29 10:57-0500\n"
"Last-Translator: \n"
"Language-Team: \n"
"Language: fr_CA\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Poedit 2.2\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"
"X-Poedit-KeywordsList: _;__;_e;esc_html__\n"
"X-Poedit-Basepath: ../../..\n"
"X-Poedit-SearchPath-0: .\n"

#: _test.php:7 _test_locale.php:18
msgid "Ceci est un test"
msgstr "TEST REUSSI"

Folder structure is like this :

locale/fr_CA/LC_MESSAGES/textdomain.po  and .mo

The locale exists (if I look up locale -a), tried to restart Apache also. I'm out of ideas.

My Answer:
// Set language to English
setlocale(LC_ALL, 'en_US');

// Specify the location of the translation files
bindtextdomain('messages', 'path/to/your/translations');

// Gettext example
echo gettext('Hello, world!');

Rate this post

3 of 5 based on 4981 votes


© 2024 - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us