PHP:检测用户语言并能够更改语言
PHP: Detect user language and able to change language
我有一个可以选择语言的网站。我想在用户第一次进入网站时,php 获取他的系统语言并写入 cookie(因此默认情况下用户每次进入时都会使用相同的语言)。但是当用户想要更改网站语言时,他将按选择语言(例如俄语)的按钮,然后网站语言将设置为俄语,当他再次进入网站时,他将有俄语。
到目前为止我有这段代码,但它真的很混乱而且不能正常工作。
HTML:
<a href="index.php?language=en">
<a href="index.php?language=ru">
PHP:
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
$language = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
if (empty($_COOKIE['language'])){
setcookie('language', $language);
}
if ( !empty($_GET['language']) ) {
$_COOKIE['language'] = $_GET['language'] === 'en' ? 'en' : 'ru';
} else {
switch ($language){
case "ru":
$language = 'ru';
break;
case "en":
$language = 'en';
break;
default:
$language = 'en';
break;
}
}
if ( $_COOKIE['language'] == "en") {
$language = 'en';
} else {
$language = 'ru';
}
$xml = simplexml_load_file("language.xml") or die("Equestria forgot languages");
$s_nav_main = $xml->s_nav_main->$language;
$s_nav_more = $xml->s_nav_more->$language;
$s_nav_bot = $xml->s_nav_bot->$language;
$s_nav_partners = $xml->s_nav_partners->$language;
$s_nav_developer = $xml->s_nav_developer->$language;
$s_aboutus = $xml->s_aboutus->$language;
$s_title = $xml->s_title->$language;
$s_head_title = $xml->s_head_title->$language;
$s_head_info = $xml->s_head_info->$language;
$s_statistics_people = $xml->s_statistics_people->$language;
$s_statistics_online = $xml->s_statistics_online->$language;
$s_statistics_messages = $xml->s_statistics_messages->$language;
$s_why_we_best = $xml->s_why_we_best->$language;
$s_why_we_best_content_title = $xml->s_why_we_best_content_title->$language;
$s_why_we_best_content_info = $xml->s_why_we_best_content_info->$language;
$s_why_we_best_adm_title = $xml->s_why_we_best_adm_title->$language;
$s_why_we_best_adm_info = $xml->s_why_we_best_adm_info->$language;
$s_why_we_best_comfort_title = $xml->s_why_we_best_comfort_title->$language;
$s_why_we_best_comfort_info = $xml->s_why_we_best_comfort_info->$language;
$s_why_we_best_wtf_title = $xml->s_why_we_best_wtf_title->$language;
$s_why_we_best_wtf_info = $xml->s_why_we_best_wtf_info->$language;
$s_trusted_title = $xml->s_trusted_title->$language;
$s_trusted_info = $xml->s_trusted_info->$language;
$s_people_celestia = $xml->s_people_celestia->$language;
$s_people_celestia_comment = $xml->s_people_celestia_comment->$language;
$s_people_luna = $xml->s_people_luna->$language;
$s_people_luna_comment = $xml->s_people_luna_comment->$language;
$s_people_twilight = $xml->s_people_twilight->$language;
$s_people_twilight_comment = $xml->s_people_twilight_comment->$language;
$s_botinfo_info = $xml->s_botinfo_info->$language;
$s_botinfo_more = $xml->s_botinfo_more->$language;
?>
这里可以采用一个简单的逻辑-
当用户登陆您的网站时,您应该跟踪 his/her IP 地址,我们可以使用该 IP 轻松获取他们的国家/地区。然后你就可以轻松地为他们提供语言服务了。
找到方法:
$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';
} else {
$language = 'ru';
}
您应该首先寻找用户首选语言的地方是 Accept-Language header。 Geo-IP 查找是一种危险且昂贵的时间浪费(至少对于确定语言而言)。除此之外,您可以设置一个 cookie 来覆盖浏览器提供的选择,但是这对欧洲的网站有法律影响。
$avail_lang=array(
'en'=>1,
'fr'=>1,
'de'=>1,
'ru'=>1
);
define("DEFAULT_LANG", 'en');
...
if ($_COOKIE['language'] && isset($avail_lang[$_COOKIE['language']]) {
$use_lang=$_COOKIE['language'];
}
// override with GET if provided
if ($_GET['language'] && isset($avail_lang[$_GET['language']]) {
$use_lang=$_GET['language'];
}
// no language? check browser
if (!$use_lang) {
$request_lang=explode(",", $_SERVER['HTTP_ACCEPT_LANGUAGE']);
foreach($request_lang as $i) {
list($lang, $pref)=explode("=", trim($i));
$pref=$pref ? 0.0+$pref : 1.0;
list($lang, $country)=explode("-", $lang);
$pref_lang[$lang]=$pref;
}
rsort($pref_lang);
$use_lang=array_shift(array_intersect_key($pref_lang, $avail_lang));
if (!$use_lang) $use_lang=DEFAULT_LANGUAGE;
}
if (user_accepts_cookies() && $use_lang!=$_COOKIE['language']) {
set_lang_cookie($use_lang);
}
我有一个可以选择语言的网站。我想在用户第一次进入网站时,php 获取他的系统语言并写入 cookie(因此默认情况下用户每次进入时都会使用相同的语言)。但是当用户想要更改网站语言时,他将按选择语言(例如俄语)的按钮,然后网站语言将设置为俄语,当他再次进入网站时,他将有俄语。
到目前为止我有这段代码,但它真的很混乱而且不能正常工作。
HTML:
<a href="index.php?language=en">
<a href="index.php?language=ru">
PHP:
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
$language = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
if (empty($_COOKIE['language'])){
setcookie('language', $language);
}
if ( !empty($_GET['language']) ) {
$_COOKIE['language'] = $_GET['language'] === 'en' ? 'en' : 'ru';
} else {
switch ($language){
case "ru":
$language = 'ru';
break;
case "en":
$language = 'en';
break;
default:
$language = 'en';
break;
}
}
if ( $_COOKIE['language'] == "en") {
$language = 'en';
} else {
$language = 'ru';
}
$xml = simplexml_load_file("language.xml") or die("Equestria forgot languages");
$s_nav_main = $xml->s_nav_main->$language;
$s_nav_more = $xml->s_nav_more->$language;
$s_nav_bot = $xml->s_nav_bot->$language;
$s_nav_partners = $xml->s_nav_partners->$language;
$s_nav_developer = $xml->s_nav_developer->$language;
$s_aboutus = $xml->s_aboutus->$language;
$s_title = $xml->s_title->$language;
$s_head_title = $xml->s_head_title->$language;
$s_head_info = $xml->s_head_info->$language;
$s_statistics_people = $xml->s_statistics_people->$language;
$s_statistics_online = $xml->s_statistics_online->$language;
$s_statistics_messages = $xml->s_statistics_messages->$language;
$s_why_we_best = $xml->s_why_we_best->$language;
$s_why_we_best_content_title = $xml->s_why_we_best_content_title->$language;
$s_why_we_best_content_info = $xml->s_why_we_best_content_info->$language;
$s_why_we_best_adm_title = $xml->s_why_we_best_adm_title->$language;
$s_why_we_best_adm_info = $xml->s_why_we_best_adm_info->$language;
$s_why_we_best_comfort_title = $xml->s_why_we_best_comfort_title->$language;
$s_why_we_best_comfort_info = $xml->s_why_we_best_comfort_info->$language;
$s_why_we_best_wtf_title = $xml->s_why_we_best_wtf_title->$language;
$s_why_we_best_wtf_info = $xml->s_why_we_best_wtf_info->$language;
$s_trusted_title = $xml->s_trusted_title->$language;
$s_trusted_info = $xml->s_trusted_info->$language;
$s_people_celestia = $xml->s_people_celestia->$language;
$s_people_celestia_comment = $xml->s_people_celestia_comment->$language;
$s_people_luna = $xml->s_people_luna->$language;
$s_people_luna_comment = $xml->s_people_luna_comment->$language;
$s_people_twilight = $xml->s_people_twilight->$language;
$s_people_twilight_comment = $xml->s_people_twilight_comment->$language;
$s_botinfo_info = $xml->s_botinfo_info->$language;
$s_botinfo_more = $xml->s_botinfo_more->$language;
?>
这里可以采用一个简单的逻辑-
当用户登陆您的网站时,您应该跟踪 his/her IP 地址,我们可以使用该 IP 轻松获取他们的国家/地区。然后你就可以轻松地为他们提供语言服务了。
找到方法:
$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';
} else {
$language = 'ru';
}
您应该首先寻找用户首选语言的地方是 Accept-Language header。 Geo-IP 查找是一种危险且昂贵的时间浪费(至少对于确定语言而言)。除此之外,您可以设置一个 cookie 来覆盖浏览器提供的选择,但是这对欧洲的网站有法律影响。
$avail_lang=array(
'en'=>1,
'fr'=>1,
'de'=>1,
'ru'=>1
);
define("DEFAULT_LANG", 'en');
...
if ($_COOKIE['language'] && isset($avail_lang[$_COOKIE['language']]) {
$use_lang=$_COOKIE['language'];
}
// override with GET if provided
if ($_GET['language'] && isset($avail_lang[$_GET['language']]) {
$use_lang=$_GET['language'];
}
// no language? check browser
if (!$use_lang) {
$request_lang=explode(",", $_SERVER['HTTP_ACCEPT_LANGUAGE']);
foreach($request_lang as $i) {
list($lang, $pref)=explode("=", trim($i));
$pref=$pref ? 0.0+$pref : 1.0;
list($lang, $country)=explode("-", $lang);
$pref_lang[$lang]=$pref;
}
rsort($pref_lang);
$use_lang=array_shift(array_intersect_key($pref_lang, $avail_lang));
if (!$use_lang) $use_lang=DEFAULT_LANGUAGE;
}
if (user_accepts_cookies() && $use_lang!=$_COOKIE['language']) {
set_lang_cookie($use_lang);
}