为什么gettext会忽略GET变量?

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

所以我通过 GET ($setLng) 获取变量 "lang"。输出是正确的,但是它不起作用。当我在代码中输入变量(不获取)时,它没有问题。

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

谁能告诉我我做错了什么?

您应该在字符串中使用 double-quote (and not single-quote)

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

通过使用单引号 php 不要重新定义字符串中的变量,输出将是带有 $ 符号的字符串(而不是变量中的值)。

来自the manual:

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

检查这个:

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

输出将是

LC_ALL=$setLng
LC_ALL=de_DE

(基于您的 URL 是 http://.../?lang=de_DE 的事实)。