我让 AI 帮我写了一个,懂行的能看一下有什么问题吗?
```
// Get the user's preferred language
const userLanguage = navigator.language || navigator.userLanguage;
// Create a Google Translate widget with the user's language as the target language
const script = document.createElement('script');
script.src = `
https://translate.google.com/translate_element?cb=googleTranslateElementInit`;
document.body.appendChild(script);
window.googleTranslateElementInit = () => {
new google.translate.TranslateElement({
pageLanguage: 'en', // Source language (English)
includedLanguages: 'en,fr,de,es,it,pt,ru,ja,ko,zh-CN,zh-TW', // Supported languages
layout: google.translate.TranslateElement.InlineLayout.SIMPLE,
autoDisplay: false
}, 'google_translate_element');
const googleTranslateElement = document.getElementById('google_translate_element');
googleTranslateElement.style.display = 'none'; // Initially hide the widget
// Function to show the widget with the user's language
function showTranslateWidget() {
googleTranslateElement.style.display = 'block';
googleTranslateElement.setAttribute('data-lang', userLanguage);
}
// Trigger the widget on a specific event (e.g., user clicks a button)
// Replace this with your desired trigger
document.getElementById('showTranslateButton').addEventListener('click', showTranslateWidget);
};
```