我有一个网页,我希望在更改查询字符串时以不同的语言显示,即; mypage.php?langue=en_en
我可以使用从bumperbox找到的代码来执行此操作: https://codereview.stackexchange.com/questions/39787/multi-language-website-management
我更改代码以便能够使用查询字符串进行语言选择:
原始代码
class lang {
private $lang = null;
function __construct($lang) {
$this->lang = parse_ini_file("{$lang}.ini");
}
public function xlate($str) {
$arg_count = func_num_args();
if ($arg_count > 1) {
$params = func_get_args();
// strip first arg
array_shift($params);
} else {
$params = array();
}
$out_str = isset($this->lang[$str]) ? $this->lang[$str] : null;
// if you string doesn't exist or is mistyped, then blow up, so we know about it
// or you could even go away to google translate and perform the translation for
// any missing strings
if (!$out_str) {
throw new exception("Lang String Not Found: $str");
}
return vsprintf($out_str, $params);
}
}
$lang = new lang('fr_fr');
我已经改变了最后一行:
$lang = new lang('fr_fr');
for:
$lang = new lang(mysql_real_escape_string($_GET['langue']));
因此,我可以使用fr_fr
en_en
或mypage.php/langue=fr_fr
)
这很有效。
我的问题是我想在我的sqlquery中显示一个不同的列,具体取决于该查询字符串。
while($row = mysql_fetch_array($rs)) {
if($lang == "fr_fr"){
$tmpTarget="code_produit";
}
else {
$tmpTarget="product_code";
}
echo "<tr><td>$row[$tmpTarget]</td></tr>"
这不起作用,即使我的语言选择是法语,它总会把我的结果带回来。
我尝试了几件事但到目前为止没有任何工作。我真的不知道还能做什么。我只需要,如果$ lang =在查询字符串中进行的选择,那么我的tmpTarget将是一个不同的值,因此我可以显示产品代码的法语名称,该名称位于我的表中的不同列中。
提前感谢您的帮助!非常感谢!
答案 0 :(得分:0)
$lang = new lang('fr_fr');
将$lang
设置为对象,而不是字符串。您需要将代码添加到返回语言名称的lang
类中。
class lang {
private $lang = null;
public $name;
function __construct($lang) {
$this->lang = parse_ini_file("{$lang}.ini");
$this->name = $lang;
}
public function xlate($str) {
$arg_count = func_num_args();
if ($arg_count > 1) {
$params = func_get_args();
// strip first arg
array_shift($params);
} else {
$params = array();
}
$out_str = isset($this->lang[$str]) ? $this->lang[$str] : null;
// if you string doesn't exist or is mistyped, then blow up, so we know about it
// or you could even go away to google translate and perform the translation for
// any missing strings
if (!$out_str) {
throw new exception("Lang String Not Found: $str");
}
return vsprintf($out_str, $params);
}
}
然后你可以这样做:
if ($lang->name == "fr_fr") {
$tmpTarget = "code_produit";
} else {
$tmpTarget = "product_code";
}
BTW,我会在if
循环之前添加while
语句,因为每行的条件都不会改变。