TobiasH TobiasH - 2 months ago 29
PHP Question

Why is GET variable ignored by gettext?

setlocale(LC_ALL, 'de_DE');

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

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

// Choosing domain

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


<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.

setlocale(LC_ALL, '$setLng');

Can anybody tell me what I am doing wrong?


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

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"


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