duanluan8390 2015-07-13 17:41
浏览 25
已采纳

PHP检测用户语言[关闭]

I need a script like

if ($userLanguage === 'english') {
  echo "we have detected you are English. would you like to visit our site in English?";
} else {
  header('location: /index.php?lang=default');
}

The script above is used for an example. I have looked all over google and all it gave me was geolocation scripts and such. I don't want a third party URL in my script. You never know if their service goes down or not.

Where do I find something like this?

  • 写回答

4条回答 默认 最新

  • doulan6245 2015-07-13 18:09
    关注

    I'm not going to repeat all the valid answers here, but this link shows another good approach (for multi-lingo websites) taking advantage of http_negotiate_language() method mirror

    So combining that mapping and your exciting code you will have:

    $map = array("en" => "english", "es" => "spanish");
    $userLanguage = $map[ http_negotiate_language(array_keys($map)) ];
    
    if ($userLanguage === 'english') {
      echo "we have detected you are English. would you like to visit our site in English?";
    } else {
      header('location: /index.php?lang=default');
    }
    

    But if you are only interested to detect the english (en) language, you might want to shorten the script to:

    if ('en' == substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2)) {
      echo "we have detected you are English. would you like to visit our site in English?";
    } else {
      header('location: /index.php?lang=default');
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址