简体   繁体   English

如何检测浏览器是否支持某种语言?

[英]How to detect if the browser supports a language?

I am building a multilingual website. 我正在建立一个多语言网站。 Is it possible to check if a particular language is installed/supported on the user's machine using PHP or Javascript? 是否可以使用PHP或Javascript检查用户计算机上是否安装/支持特定语言? I want to detect this and display a message to the user if the language is not supported/installed. 如果不支持/未安装该语言,我想检测并向用户显示一条消息。

Thanks, Mark. 谢谢马克。

Detecting which languages are installed might not be quite possible... 检测安装哪种语言可能不太可能...

... But you can detect which languages the user is willing to get from websites -- ie, generally, which languages he can understand. ...但是您可以检测到用户愿意从网站中获得哪些语言-即通常来说,他可以理解哪些语言。

Most browser send an HTTP Header called " Accept-Language ", which can have a value such as " fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3 " (this is what my current browser send to websites I am visiting) 大多数浏览器都会发送一个名为“ Accept-Language ”的HTTP标头,其值可以为“ fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3(这是我的当前浏览器发送到我正在访问的网站)

On the PHP side, you can get this in the $_SERVER array : 在PHP方面,您可以在$_SERVER数组中获得它:

var_dump($_SERVER['HTTP_ACCEPT_LANGUAGE']);

Will output : 将输出:

string 'fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3' (length=35)

Here, it indicates I want websites to send me content in french ; 在这里,它表示我希望网站以法语向我发送内容; but that I also accept english (preferably US english, but I'm OK with just plain default english too) 但是我也接受英语(最好是美国英语,但是我也可以使用普通的默认英语)


You can find quite a lot of examples of how to parse this in PHP ; 您可以找到很多有关如何在PHP中进行解析的示例; for instance : Parse Accept-Language to detect a user's language . 例如: 解析Accept-Language以检测用户的语言

Using the code provided in that article, I get this array of languages : 使用该文章中提供的代码,我得到了以下语言数组:

array
  'fr' => int 1
  'fr-fr' => string '0.8' (length=3)
  'en-us' => string '0.5' (length=3)
  'en' => string '0.3' (length=3)

ie : 即:

  • I prefer french 我喜欢法语
  • if "fr" is not possible, I like "french from France" 如果无法使用“ fr”,那么我喜欢“法国的法国”
  • if that is not possible either, I accept "english from the US" 如果这也不可行,我接受“美国英语”
  • and if that is not possible too, I'm qite OK with "english" 如果那也是不可能的话,我对“英语”很满意

And if the website cannot serve any of those... Well, I suppose I don't have much of a choice, and will get whatever it wants to send me... 而且,如果该网站无法为这些网站提供任何服务...那么,我想我没有太多选择,它将得到想要发送给我的一切...

Browsers typically send a Accept-Language header which might provide you a little hint about the language the person expects to see. 浏览器通常会发送一个Accept-Language标头,该标头可能会为您提供一些有关人们期望看到的语言的提示。 In my case, my browser sent this header to stackoverflow.com: 就我而言,我的浏览器将此标头发送到stackoverflow.com:

Accept-Language: en-us,en;q=0.5

Assuming that you're using PHP on server side, you can lookup this information in the $_SERVER variable: 假设您在服务器端使用PHP,则可以在$ _SERVER变量中查找以下信息:

$_SERVER["HTTP_ACCEPT_LANGUAGE"]

It is possible to learn the user's language preferences by examining the Accept header. 通过检查Accept标头可以了解用户的语言首选项。 <selfpromotion type="shameless"> I wrote a paper on the subject quite a few years ago (2000, phew! ) exploring this and a couple of alternate mechanisms.</selfpromotion> <selfpromotion type =“ shameless”>几年前,我写了一篇有关该主题的论文 (2000年, phe! ),探讨了这一主题以及一些其他机制。</ selfpromotion>

Quite a few interesting resources have came up since; 此后,出现了很多有趣的资源。 particularly, I like this article , which takes a pragmatic approach for PHP. 特别是,我喜欢本文 ,它对PHP采用了务实的方法。

Good luck! 祝好运!

I've been doing multilingual stuff for the last ten years and my impression is that you're trying to overdo it: you're looking at it the wrong way. 在过去的十年中,我一直在做多语种工作,而我的印象是您试图超越它:您以错误的方式看待它。 You shouldn't care what language (either for the OS or the browser) is installed on the user's machine: OS language doesn't matter, and modern browsers support practically all languages around (and, since the demise of NN4, all the writing systems/alphabets that found a place in the Unicode standard). 您不必在意用户计算机上安装了哪种语言(针对操作系统还是浏览器):操作系统语言无关紧要,现代浏览器几乎支持所有语言(自NN4淘汰以来,所有文字在Unicode标准中找到位置的系统/字母)。 What you should pursue is what language does the user prefers. 您应该追求的是用户喜欢哪种语言。 The user is the bottleneck regarding the number of 'supported' languages. 用户是有关“受支持”语言数量的瓶颈。

The Accept-Language header could be a good solution - if it worked. 如果可以,Accept-Language标头可能是一个很好的解决方案。 To my experience, it does not - much too often. 根据我的经验,它不是-经常出现。 The average user of today still has to learn that the URL goes into the address bar and not into his homepage search form - I wouldn't expect more than 1-2% of the Net population to know about HTTP headers (yes, I'm in an optimistic mood tonight). 如今,普通用户仍然必须了解URL进入地址栏而不是进入其主页搜索表单-我不希望超过1-2%的网络用户知道HTTP标头(是的,今晚心情乐观)。 Users will happily come to your website with whatever language their browser has been originally set up to claim. 用户将很高兴使用最初设置为要求使用其浏览器的任何语言来访问您的网站。 This includes tourists from cybercafès around the world, expatriates working abroad for companies with strong "don't mess with the config" policies, old folks whase IT-capable grandsons have a bizzarre sense of humour, shared machines and so on. 其中包括来自世界各地网吧的游客,为具有强大的“不要搞砸配置”政策而在国外工作的公司的外籍人士,怀有IT技术能力的孙子们的幽默感,共享机器等等。 You're going to guess wrong more often than not. 您经常会猜错。

The concept of displaying a message for unsupported languages is dubious too. 对于不支持的语言显示消息的概念也是可疑的。 If the user with a, say, Punjabi browser is able to navigate to your site he would probably be able to see you don't supply Punjabi on its own - no need to interrupt his browsing with messages. 如果说使用旁遮普语浏览器的用户能够导航到您的站点,则他可能会看到您自己没有提供旁遮普语-无需中断他的浏览消息。

All that said, sometimes the simplest solution is the best: I'd suggest you let the user choose. 综上所述,有时候最简单的解决方案是最好的:我建议您让用户选择。 Put out the list of available languages and the user will be more than happy to take the guesswork out of your code. 列出可用语言列表,用户将很乐意将猜测从代码中删除。 And he/she will get it right every time. 而且他/她每次都会做对。

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

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