自动检测用户的首选语言和谷歌翻译

我在我的网站中使用此脚本进行翻译

I use this script in my site for translation

<div id="google_translate_element" align="center"></div>  
<script type="text/javascript">
    function googleTranslateElementInit() {
        new google.translate.TranslateElement({
        pageLanguage: 'auto',
        autoDisplay: false,
        layout: google.translate.TranslateElement.InlineLayout.SIMPLE
        }, 'google_translate_element');
    }
</script>
<script src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>

它工作得很好:)但是有没有办法在用户进入我的网站时检测用户 ip 并自动翻译?

It is working just fine :) But is there a way to detect the user ip and auto translate when a user go in to my site?

推荐答案

虽然你可以使用基于 IP 的位置检测(参见 这个答案),但它既不可靠,也不能让您更了解用户的首选语言(例如,出国旅行的用户等).

Although you can use IP-based location detection (see this answer), but it's neither reliable nor makes you wiser about user's preferred languages (e.g. users travelling abroad, etc.).

具有大量国际流量的网站使用各种参数来决定应以哪种语言呈现内容.其中一些参数:

Websites with heavy international traffic use various parameters to decide in which language the content should be presented. Some of these parameters:

  • 接受语言 详细讨论的 HTTP 标头这里.
  • window.navigator.languagewindow.navigator.userLanguage 属性的值(对于 IE)
  • 根据 CLDR 检查基于 IP 的位置检测数据,以便为您提供该地区的通用语言.
  • Accept-Language HTTP header which is discussed in detail here.
  • Values of properties window.navigator.language or window.navigator.userLanguage (for IE)
  • IP-based location detection data checked against CLDR to provide you with common languages in that territory.

MediaWiki 扩展程序 UniversalLanguageSelector 使用这些因素以及存储的用户偏好来为每个用户提供常用语言列表.请参阅 getFrequentLanguageList().

MediaWiki extension, UniversalLanguageSelector, uses these factors as well as stored user preferences to provide a list of common languages for each user. See getFrequentLanguageList().

W3C 也有一些建议.

相关文章