Reputation: 391
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.
Upvotes: 8
Views: 6723
Reputation: 3351
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.
Upvotes: 2
Reputation: 28742
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().
Upvotes: 6