检测谷歌网站翻译语言的变化 [英] Detect Google Website Translator change of language

查看:181
本文介绍了检测谷歌网站翻译语言的变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的网站上使用Google网站翻译器让用户即时翻译网站。使用此代码:

I'm using the Google Website Translator on my website to let the user translate the site on the fly. Using this code:

    function googleTranslateElementInit() {
        new google.translate.TranslateElement({pageLanguage: 'en', includedLanguages: 'ar,de,el,en,es,fr,it,ja,ko,nl,ru,zh-CN', layout: google.translate.TranslateElement.FloatPosition.BOTTOM_RIGHT}, 'google_translate_element');
    }

这很有效,现在唯一需要知道的是哪种语言用户实际选择了。
我想检测用户手动选择语言的时间以及翻译者进行自动翻译时,因为它可以根据浏览器设置进行自动翻译。

This works great, only thing now is that I need to know which language the user has actually selected. I want to detect both when the user manually selects a language and also when the translator makes an automated translation, as its enabled to do automatic translations based on the browser settings.

我想要做的是在语言改变时添加一个事件监听器。即不仅在用户手动设置语言时,而且每次翻译者实际进行翻译时。
例如翻译开始或结束时或页面刷新以显示新语言。

What I want to do is to add an event listener to when the language is changed. I.e. not only when the user manually sets the language but every time the translator actually does a translation. E.g. when the translation starts or finishes or when the page "refreshes" to show the new language.

我需要收集此信息并将其发送到服务器以了解使用何种语言用于在稍后阶段发送给用户的电子邮件。由于这些信息是从多个地方收集的,我不想在每次需要信息时手动检查所选语言,而是添加一个检测语言变化的事件监听器并触发一个AJAX方法来保存会话中的信息。服务器。

I need to collect this information and send it to the server to know what language to use for e-mails that are sent to the user at a later stage. As this information is gathered from multiple places, I don't want to manually check the selected language every time I required the information, but to add an event listener that detects the language changing and triggers an AJAX method to save the information in session on the server.

谢谢!

推荐答案

当用户手动选择时一种语言(改变选择框的值)你可能会得到用

When the user manually selects a language (changes the value of the selectbox) you might get the language chosen with

$('.goog-te-combo').on('change',function(){
       language = $("select.goog-te-combo option:selected").text();
        alert(language);
    });

小提琴

如果您的页面刷新并且翻译人员翻译您的页面,您可以使用<$ c获取当前使用的语言$ C>的setTimeout 。这不是完美的,但它确实有助于交配.. :)

In case when your page refreshes and the translator translates your page, you could get the current language that is being used by using a setTimeout. This isn't perfect, but it certainly helps mate.. :)

这篇关于检测谷歌网站翻译语言的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆