简体   繁体   English

PHP:检测用户语言并能够更改语言

[英]PHP: Detect user language and able to change language

I have a website with ability to choose language.我有一个可以选择语言的网站。 And I wanted to make that when user enters first time to the website, php gets his system language and writes to cookie (So user by default every time when he enters time will have same language).我想让当用户第一次进入网站时,php 获取他的系统语言并写入 cookie(因此用户默认每次进入时间时都会使用相同的语言)。 But when user want to change website language, he will press a button with chosen language (For example Russian), then website language will be set for russian, and when he will enter website again, he will have russian language.但是当用户想要改变网站语言时,他会在选择的语言(例如俄语)中按下一个按钮,然后网站语言将设置为俄语,当他再次进入网站时,他将是俄语。

So far I have this code, but it's really confusing and it doesnt work properly.到目前为止,我有这段代码,但它真的很混乱,而且不能正常工作。

HTML: HTML:

<a href="index.php?language=en">
<a href="index.php?language=ru">

PHP: 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;
?>

The first place you should look for the users preferred language is the Accept-Language header.您应该查找用户首选语言的第一个位置是 Accept-Language 标头。 Geo-IP lookups are a dangerous and expensive waste of time (at least for determining language). Geo-IP 查找是一种危险且昂贵的时间浪费(至少对于确定语言而言)。 Beyond that, you can set a cookie to override the choices presented by the browser, but there are legal implications around this for websites in Europe.除此之外,您可以设置一个 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);
}

a simple logic can be adopted here -这里可以采用一个简单的逻辑——

when a user lands at your website you should track his/her IP address, we can easily get their country using that IP.当用户登陆您的网站时,您应该跟踪他/她的 IP 地址,我们可以使用该 IP 轻松获取他们的国家/地区。 Then you can easily serve language to them.然后,您可以轻松地为他们提供语言服务。

Found the way how to do this:找到了如何做到这一点的方法:

$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';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM