[英]Detect User's Preferred Language and Google Translate Automatically
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>
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 (eg users travelling abroad, etc.). 虽然您可以使用基于IP的位置检测(请参阅此答案 ),但它既不可靠也不会让您更加了解用户的首选语言(例如,出国旅行的用户等)。
Websites with heavy international traffic use various parameters to decide in which language the content should be presented. 具有大量国际流量的网站使用各种参数来决定应该以何种语言呈现内容。 Some of these parameters: 其中一些参数:
Accept-Language
HTTP header which is discussed in detail here . Accept-Language
中详细讨论的HTTP标头这里 。 window.navigator.language
or window.navigator.userLanguage
(for IE) 属性值window.navigator.language
或window.navigator.userLanguage
(对于IE) MediaWiki extension, UniversalLanguageSelector
, uses these factors as well as stored user preferences to provide a list of common languages for each user. MediaWiki扩展, UniversalLanguageSelector
,使用这些因素以及存储的用户首选项来为每个用户提供常用语言列表。 See getFrequentLanguageList()
. 请参阅getFrequentLanguageList()
。
W3C also has some recommendations . W3C也有一些建议 。
This script shows the translation drop-down box only for people with English not set as their primary or only language, and hides it when English users view the page - it is coded for English pages using the en
in the google code. 此脚本仅针对英语未设置为主要或唯一语言的人显示翻译下拉框,并在英语用户查看页面时隐藏它 - 使用Google代码中的en
编码英语页面。
It uses the first 2 characters of the language only to avoid checking for the many variants of English like en-US
, en-tt
etc - they all begin with en
. 它仅使用该语言的前2个字符来避免检查英语的许多变体,如en-US
, en-tt
等 - 它们都以en
开头。
This could easily be adapted to detect pageLanguage
and compare it with the user's preferred language(s). 这可以很容易地适用于检测pageLanguage
并将其与用户的首选语言进行比较。 The use of navigator.languages
is important because this is used on newer browser releases, see cross-browser compatibility explained navigator.languages
的使用很重要,因为它用于较新的浏览器版本,请参阅解释的跨浏览器兼容性
<div id="google_translate_element"></div>
<script type="text/javascript">
var userLang = navigator.language || navigator.userLanguage || navigator.languages;
if (userLang.substr(0,2) != "en"){
function googleTranslateElementInit() {
new google.translate.TranslateElement({pageLanguage: 'en', layout:
google.translate.TranslateElement.FloatPosition.TOP_LEFT}, 'google_translate_element');
}
}
else {
document.getElementById("google_translate_element").style.display="none";
}
</script>
<script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.