最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I automatically translate my website into the user's language? - Stack Overflow

programmeradmin0浏览0评论

I have a website in English, and recently some users reported that they do not know English, so I thought, maybe the site is not accessible to everyone, because some do not know English.

So my question is: is there any way for the website to automatically translate into the user's language?

I've been searching, it seems that Google has a translation API, is this what I need?

EDIT

I want to detect the user's browser language and translate the page into the user's browser language. We can obtain the language of the browser using navigator.language || navigator.userLanguage in JavaScript. Is there any way to integrate this with the Google API and then automatically translate without requiring user interaction?

I think it may be possible through control structures and methods, or by passing a variable as a parameter on the Googgle Translate website, am I right?
Please, I need help, I don't want the user to choose the language, I want to translate automatically, I want to recognize the language of the user's browser and automatically translate

Note: I use <meta charset="UTF-8">, does this affect anything?

I have a website in English, and recently some users reported that they do not know English, so I thought, maybe the site is not accessible to everyone, because some do not know English.

So my question is: is there any way for the website to automatically translate into the user's language?

I've been searching, it seems that Google has a translation API, is this what I need?

EDIT

I want to detect the user's browser language and translate the page into the user's browser language. We can obtain the language of the browser using navigator.language || navigator.userLanguage in JavaScript. Is there any way to integrate this with the Google API and then automatically translate without requiring user interaction?

I think it may be possible through control structures and methods, or by passing a variable as a parameter on the Googgle Translate website, am I right?
Please, I need help, I don't want the user to choose the language, I want to translate automatically, I want to recognize the language of the user's browser and automatically translate

Note: I use <meta charset="UTF-8">, does this affect anything?

Share Improve this question edited Jan 8, 2024 at 19:25 Rainy sidewalks 1,1681 gold badge6 silver badges18 bronze badges asked Jan 13, 2021 at 15:50 user14929188user14929188 1
  • 1 Have you looked at w3schools./howto/howto_google_translate.asp or stackoverflow./questions/46966839/… ? – RedYetiDev Commented Jan 13, 2021 at 15:54
Add a ment  | 

3 Answers 3

Reset to default 2

Remember how you asked me how I can detect the user's language? Well I finally came up with a code for that. Code is below.

let lang = window.navigator.languages ? window.navigator.languages[0] : null;
    lang = lang || window.navigator.language || window.navigator.browserLanguage || window.navigator.userLanguage;

let shortLang = lang;
if (shortLang.indexOf('-') !== -1)
    shortLang = shortLang.split('-')[0];

if (shortLang.indexOf('_') !== -1)
    shortLang = shortLang.split('_')[0];

console.log(lang, shortLang);
I hope this is what you are looking for!

This answer might answer the question and the solution is from w3schools. What it basically does is that a dropdown will be created with different types of languages. Here is the code, tell me if it works.

    <!DOCTYPE html>
<html lang="en-US">
<body>

<h1>My Web Page</h1>

<p>Hello everybody!</p>

<p>Translate this page:</p>

<div id="google_translate_element"></div>

<script type="text/javascript">
function googleTranslateElementInit() {
  new google.translate.TranslateElement({pageLanguage: 'en'}, 'google_translate_element');
}
</script>

<script type="text/javascript" src="//translate.google./translate_a/element.js?cb=googleTranslateElementInit"></script>

<p>You can translate the content of this page by selecting a language in the select box.</p>

</body>
</html>

I hope this is what you are looking for.

Is it ok, to make a request to google services on behalf of the user? Google will gleefully want to know what page the user is surfing, and will know by the automatic translation request. This will have to be encapsulated by an opt-in on site load.

发布评论

评论列表(0)

  1. 暂无评论