Can web page translation by Google be detected?
Asked Answered
I

2

8

I have a web page, in Dutch, with a poll with a radio button. I'd like to know which language the users speak. Is there a way I can detect if the page has been translated by Google when they submit? I do not use a translation bar, I am talking about the spontaneous google translation.

Individualize answered 10/4, 2018 at 8:12 Comment(6)
This is done client side, so you cannot detect it. Inspect the accept-language header, but note that it is unreliable.Pteropod
You can check the server logs to see if Google has accessed your site...Draghound
I don't know if there are many people that use Google in-browser translation a lot, and hence whether that is any useful indicator for what language the user speaks. Seems like a minor roundabout scenario to worry about…Learned
It seem browser add class to html tag class="translated-ltr"Altair
A theoretical idea: Use the onSubmit event to call a javascript function which look for a particular word. Put the result into a hidden field and then sumbit the form. On serverside create a list wich contains a range of translations for this word and compare it with submited value.Leandro
See Also: Detecting Google Chrome TranslationForewarn
F
6

Just check a known element if the text matches your text.

function isDutch() {
    return $('#readmore').text() === "Meer lezen";
}

or a non jQuery solution:

function isDutch() {
    document.querySelector('#readmore').innerText  === "Meer lezen";
}

Just make sure the element you have is an easy translatable sentence like read more.

Then you update a hidden field in your form with the result.

You can do this the moment a click is registered on your radio button.

I just tested it on a russian site, lenta.ru and ran $('a[href="/parts/news"]').text(); after having translated it by right clicking the page and selecting translate this page(chrome). The content returned was in my language(dutch) in the jquery text().

proof of translation detection

Footpath answered 10/4, 2018 at 8:19 Comment(0)
Y
2

When translated through Google Translate, the target language is injected into the lang attribute of the main html tag, you can retrieve it with:

document.getElementsByTagName('html')[0].getAttribute('lang')

which results in something like

en-x-mtfrom-nl... and this in turn you can log to your server or set as a cookie.

Yesseniayester answered 10/4, 2018 at 9:11 Comment(2)
Actually, the lang attribute stays the same, but a class : translated-ltr is added. It shows : <html lang="nl" class="translated-ltr">. If I remove the lang attribute, it shows just : <html class="translated-ltr">. This indicates a translation, but not in which language it is translated.Individualize
@Individualize This is no longer true, as Chrome, Firefox and Safari all update the lang attribute after applying machine translation (so long as there was a lang set before the translation). You can use this attribute to detect changes. I wrote more about this here: martijnhols.nl/gists/…Thaxter

© 2022 - 2024 — McMap. All rights reserved.