PHP gettext doesn't return translated string

71 Views Asked by At

I am trying to setup getText() with PHP

$locale = "en_GB";

//if (isset($_GET["locale"])) $locale = $_GET["locale"];

$domain = 'messages';
bindtextdomain($domain, "./locale");
textdomain($domain);
bind_textdomain_codeset($domain, 'UTF-8');
var_dump( file_exists("locale/".$locale."/LC_MESSAGES/".$domain.".mo" ) );
$results = gettext("Velkommen");
if ($results === "Velkommen") {
    echo "Original English was returned. Something wrong\n";
}
echo $results . "\n";
?>
<?= _("Velkommen"); ?>`

var_dump returns true, but I get the errormessage saying something went wrong.

Checking locale -a I know that en_GB is present on the server.

I have tried multiple suggestions found on Google. Tried to change the path in bindtextdomain

1

There are 1 best solutions below

2
Jeppe Donslund On

This example works for me.

print date("d-m-Y H:i:s", date("U"));
$locale = "en_GB";
if (!function_exists("gettext")){ echo "gettext is not installed\n"; } else{ echo "gettext is supported\n"; }
//if (isset($_GET["locale"])) $locale = $_GET["locale"];
putenv('LC_ALL='.$locale);
if (false === setlocale(LC_ALL, $locale)) {
    throw new LocaleNotSupportedException(sprintf('Locale "%s" is not installed in the system.', $locale));
}
$domain = 'messages';
bindtextdomain($domain, "locale");
textdomain($domain);
bind_textdomain_codeset($domain, 'UTF-8');
var_dump( file_exists("locale/".$locale."/LC_MESSAGES/".$domain.".mo" ) );
$results = gettext("Velkommen");
if ($results === "Velkommen") {
    echo "Original Danish was returned. Something wrong\n";
}
echo $results . "\n";
?>
<?= _("Velkommen"); ?>