我有一个代码,允许选择网站语言并保存您选择的cookie。此外,它还获取本地浏览器语言并默认设置为webiste。但我只有两种语言俄语和英语。 我想再添加两种语言。但他们没有工作。为什么?任何解决方案?
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
if ( !empty($_GET['language']) )
{
$_COOKIE['language'] = $_GET['language'] === 'en' ? 'en' : 'ru';
}
elseif (empty($_COOKIE['language']))
{
$_COOKIE['language'] = $lang;
}
setcookie('language', $_COOKIE['language']);
if ( $_COOKIE['language'] == "en")
{
$language = 'en';
}
elseif ( $_COOKIE['language'] == "es")
{
$language = 'es';
}
elseif ( $_COOKIE['language'] == "zh")
{
$language = 'zh';
}
else
{
$language = 'ru';
}
$xml = simplexml_load_file("language.xml") or die("1YOU!");
$restore = $xml->restore->$language;
$set_up = $xml->set_up->$language;
$intro = $xml->intro->$language;
$intro_all = $xml->intro_all->$language;
$s_title = $xml->s_title->$language;
$advcash_login = $xml->advcash_login->$language;
$advcash_fee = $xml->advcash_fee->$language;
$advcash_amount = $xml->advcash_amount->$language;
$payeer_amount = $xml->payeer_amount->$language;
$payeer_fee = $xml->payeer_fee->$language;
$payment_send = $xml->payment_send->$language;
$deposit = $xml->deposit->$language;
?>
答案 0 :(得分:2)
这段代码应该像这样重构:
$allow_langs = array('en', 'es', 'zh', 'ru');
$browser_lang = strtolower(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2));
$language = (isset($_COOKIE['language']) ? $_COOKIE['language'] : $browser_lang);
if (isset($_GET['language'])) {
$language = $_GET['language'];
}
if (!in_array($language, $allow_langs, true))
$language = 'en'; //fallback for invalid lang
setcookie('language', $language);
//do your xml loading on $language var