TobiasH TobiasH - 1 month ago 12
PHP Question

Why is GET variable ignored by gettext?

<?php
putenv('LC_ALL=de_DE');
setlocale(LC_ALL, 'de_DE');

$setLng= htmlspecialchars($_GET["lang"]);
//putenv('LC_ALL=$setLng');
//setlocale(LC_ALL, '$setLng');

// translation path
bindtextdomain("messages", "./locale");

// Choosing domain
textdomain("messages");

// translation searched in ./locale/de_DE/LC_MESSAGES/meinePHPApp.mo

?>

<hi><?php echo _("Translate using gettext") ?></h1>
<p> <?php echo _("This video is a simple tutorial.") ?></p>

<?php echo "Set Languange:" .$setLng //shows current setting and hows me that variable is correctly fetched...
?>


so i am fetching the variable "lang" via GET ($setLng). The output is correct, however it just doesnt work. When I enter the variable within the code (without fetching), it works without problems.

putenv('LC_ALL=$setLng');
setlocale(LC_ALL, '$setLng');


Can anybody tell me what I am doing wrong?

Answer

You should use a double-quote (and not single-quote) in your string:

putenv("LC_ALL=$setLng");
setlocale(LC_ALL, "$setLng");

By using single-quote php don't reconginse the variables inside your string, and the output will be the string with the $ sign (and not the value inside your variable).

This is from the manual:

When a string is specified in double quotes or with heredoc, variables are parsed within it.

Check this:

echo 'LC_ALL=$setLng';
echo "<br />\n";
echo "LC_ALL=$setLng";

The output will be"

LC_ALL=$setLng
LC_ALL=de_DE

(based on the fact the your URL is http://.../?lang=de_DE).

Comments