0

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 :

<?php
$language = "fr_CA";
putenv("LANGUAGE=".$language);
//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.

guylabbe.ca
  • 871
  • 2
  • 11
  • 21

1 Answers1

2

You need to add the textdomain to your calls:

// No good
echo _("Ceci est un test");
// This will work!
echo _($domain . "\004" . "Ceci est un test");

Now add the textdomain as msgctxt "textdomain" in your PO file and save it.

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

Remember that $domain must match the msgctxt value.

This answer was adapted from the PHP official docs comment by "kingjackal at gmail dot com" and can be viewed here

bpanatta
  • 509
  • 3
  • 12
  • Thank you so much for your reply. But when I try this, it echoes : "textdomain Ceci est un test" And msgctxt "textdomain" is correctly added in the po file (and mo file generated after). Is my folder structure viable? – guylabbe.ca Dec 10 '20 at 17:25
  • Yes, I think so. To solve this, I've created a `test.php` file, then a folder structure exactly as the paths you provided with the `.po` and compiled it to `.mo`, gave group access permissions to nginx, to all these files. I had to install the french lang (which I didn't had). Until here was getting same results as you. Then I've change the code to the above and it worked. – bpanatta Dec 10 '20 at 17:44
  • I've also tried with the `gettext` function from the example and with my own language (pt_BR.utf8). All got the same results. – bpanatta Dec 10 '20 at 17:48
  • When I run locale -a, I do have fr_CA, fr_CA.utf8 in the list, so it's likely installed ... I do not know where to look anymore :( – guylabbe.ca Dec 11 '20 at 03:27
  • The msgctxt has nothing to do with the textdomain, but it's for distinguishing two messages that have teh same msgid, but different translations based on a context (think "coin", which can be a noun or a verb, based on context, and translates to German as either "Münze" or "prägen"). The textdomain is what separates the translations of two different programs or libraries, and matches the name of the .mo file to be used. – Enno Dec 11 '20 at 13:23
  • @guylabbe.ca have you tried changing the language to fr_CA.utf8? – bpanatta Dec 11 '20 at 14:00
  • @bpanatta Yes, as I have fr_CA, fr_CA.utf8, fr_CA.ISO8859-1 in my language list (locale -a), I tried to set $language to every of them, same for the folder name. – guylabbe.ca Dec 12 '20 at 17:56
  • @bpanatta I also tried to upload the code to another server just in case it was a config issue, but it doesn't work on the online server either. Are you sure my folder/file naming is valid? I have no way to debug that – guylabbe.ca Dec 12 '20 at 17:59