javascript 谷歌翻译获取当前语言

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13480879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 18:55:36  来源:igfitidea点击:

Google translate get current language

javascriptgoogle-translate

提问by Naterade

After finding zero of anything to help me online....

在找到零帮助我在线后....

I am using the current function for a multi language site:

我正在为多语言站点使用当前功能:

function googleTranslateElementInit() {
  new google.translate.TranslateElement({pageLanguage: 'en', includedLanguages: 'en,es',     layout: google.translate.TranslateElement.InlineLayout.SIMPLE}, 'google_translate_element');
}

However I have no idea how to get the current language once a user changes it. I'm not sure if this is even possible. Basically I want to update to Spanish images if Spanish is chosen over English. Any help would be appreciated!

但是,一旦用户更改当前语言,我不知道如何获取当前语言。我不确定这是否可能。基本上,如果选择西班牙语而不是英语,我想更新为西班牙语图像。任何帮助,将不胜感激!

采纳答案by Naterade

Your not gonna believe it:

你不会相信:

window.setInterval(function(){
     var lang = $(".goog-te-menu-value span:first").text();
     alert(lang);
},5000);

I just had to dig to find the container in Firebug and grab the value from the first span element. Hope this helps someone.

我只需要挖掘以在 Firebug 中找到容器并从第一个 span 元素中获取值。希望这可以帮助某人。

回答by jchip

The currently selected language is stored in a cookie named googtrans.

当前选择的语言存储在名为 的 cookie 中googtrans

Here's a simple example of grabbing the value from the cookie (based on cookie code from here: What is the shortest function for reading a cookie by name in JavaScript?):

这是一个从 cookie 中获取值的简单示例(基于此处的 cookie 代码:What is the shortest function for reading a cookie by name in JavaScript?):

function readCookie(name) {
    var c = document.cookie.split('; '),
    cookies = {}, i, C;

    for (i = c.length - 1; i >= 0; i--) {
        C = c[i].split('=');
        cookies[C[0]] = C[1];
     }

     return cookies[name];
}
console.log(readCookie('googtrans')); //eg. 'en/no' for Norwegian, '/en/hr' for Croatian, etc.

回答by tim peterson

Calling google.translate.TranslateElement().cgives the code for the current language. For example, "fr" for french, "en" for english, "de" for german.

调用google.translate.TranslateElement().c给出了当前语言的代码。例如,“fr”代表法语,“en”代表英语,“de”代表德语。

Inspecting the google.translateglobal object is generally informative. It's a little hard b/c Google's obviously compiled the code such that things like TranslateElement.cdon't have human readable names, but with a little effort you can make sense of some of the parameters.

检查google.translate全局对象通常可以提供信息。b/c 谷歌显然编译代码有点困难,例如TranslateElement.c没有人类可读的名称,但只需稍加努力,您就可以理解一些参数。

回答by Crimbo

Calling google.translate.TranslateElement().a.sdgives the identifier of the current language.

调用google.translate.TranslateElement().a.sd给出了当前语言的标识符。

Examples: "fr"for French, "en"for English, "de"for German, "it"for Italian

例如: “FR”代表法语,“en”表示英语,“德”为德国,“它”意大利



Calling google.translate.TranslateElement().ais an object of other current parameters, like the Google Analytics tracking code.

调用google.translate.TranslateElement().a是其他当前参数的对象,例如 Google Analytics 跟踪代码。

Check it out in the console to see what is there. How do I access the console?

在控制台中检查它以查看那里有什么。如何访问控制台?

Inspecting the google.translateglobal object is generally informative. It is a little hard because Google is constantly updating the code, making changes to the parameter allocations, and has obviously compiled the code in such a way that things like google.translate.TranslateElement()does not have human readable names; with a little effort though, you can make sense of some of the parameters.

检查google.translate全局对象通常可以提供信息。这有点困难,因为 Google 不断更新代码,更改参数分配,并且显然已将代码编译为google.translate.TranslateElement()没有人类可读名称之类的东西;只需稍加努力,您就可以了解一些参数。

回答by Rahul Singh

I am using google translate module in my joomla site. My requirement was that we have one language button at the top. Default the site will be in English. So the button to display on top will be Spanish. When user clicks on the button the page translates to Spanish and the button changes to English and vice versa. This works fine as long you don't refresh the page. Now say the translated page is in Spanish and the button at the top is English. Now when you refresh the page the button changes back to Spanish (javascript hide show function, default is Spanish). As I was enable to trace the current language, the language button at the top were not according to the language selected. After to much digging I found this script which helped me resolve my problem. I hope it helps someone else too.

我在我的 joomla 网站中使用谷歌翻译模块。我的要求是顶部有一个语言按钮。默认站点将是英文的。所以显示在顶部的按钮将是西班牙语。当用户单击按钮时,页面会转换为西班牙语,按钮会更改为英语,反之亦然。只要您不刷新页面,这就可以正常工作。现在说翻译的页面是西班牙语,顶部的按钮是英语。现在,当您刷新页面时,按钮会变回西班牙语(javascript 隐藏显示功能,默认为西班牙语)。由于我可以跟踪当前语言,因此顶部的语言按钮与所选语言不符。经过大量挖掘,我找到了这个脚本,它帮助我解决了我的问题。我希望它也能帮助别人。

<script>
 //to get currently selected language
window.setInterval(function(){
     lang = $("select.goog-te-combo option:selected").text();
    alert(lang);
},5000);
 </script>

回答by Thomas Schultz

You might be able to get the language from the URL.

您或许可以从 URL 获取语言。

    function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
    });
    return vars;
}

Then

然后

var selected_lang = getUrlVars()['lang'];