V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
kokerkov
V2EX  ›  问与答

GoogleTranslate 能根据用户浏览器语言,自动切换为该用户的当前语言,而无需手动选择吗?

  •  
  •   kokerkov · 55 天前 · 521 次点击
    这是一个创建于 55 天前的主题,其中的信息可能已经有所发展或是发生改变。

    网站是英文的,为了方便其他语种,这样似乎方便点。我记得好像有网站可以的。

    请指教

    第 1 条附言  ·  55 天前

    我让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);
    };
    
    1 条回复    2024-11-01 15:48:55 +08:00
    kokerkov
        1
    kokerkov  
    OP
       55 天前
    我让 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);
    };
    ```
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3450 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 00:06 · PVG 08:06 · LAX 16:06 · JFK 19:06
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.