Google Translate set default language

纵饮孤独 提交于 2019-11-27 01:38:28

问题


Maybe this has an obvious solution that I'm overlooking, but I can't seem to find the correct parameter to put in to make this happen. Using the Google Translate widget on a site, I need to set the default language that the user sees when entering the site, even though the site is english.

function googleTranslateElementInit() {
    new google.translate.TranslateElement({
       pageLanguage: 'en'
    }, 'google_translate_element');
}

I've tried adding: defaultLanguage: 'fr' and tried: targetLanguage: 'fr'

I did find some nice jQuery solutions, but didn't want to bypass this if it was an easy fix.


回答1:


This isn't a direct answer to how to use jQuery to accomplish the task, but hopefully it's helpful. Google Translate uses a cookie called "googtrans" to track which language is selected. You can set that cookie yourself before the page loads and Google Translate will use it.

// PHP code sample, could be accomplished with any language that can set cookies
// set the default language translation to Portugese
setcookie('googtrans', '/en/pt');



回答2:


Adding #googtrans(xx) to the end of the query string will also automatically translate the page for you, similar to setting the cookie yourself (where xx is the language code, eg. fr for french).




回答3:


We can set google translate default language by working with cookies for this first use google translate to translate your web page then see what cookies he has created (for this right click on your web page then page info then security then view cookies and click on googtrans you see what is the translation he is using and what is the path and what is the domain or host name ) and put this all data in setcookies function

example

 setcookie(“googtrans”, “/en/ja”, time()+3600, “/”, “www.example.com”);

//setcookie(“googtrans”, “en/ja”);
setcookie(“googtrans”, “/en/en”, time()+3600, “/”, “.example.com”);



回答4:


Looks like jQuery / Javascript are the way to go here, unless your user has their browser preferences set to the different language. Quoting from the google groups discussion:

The Translate Element works by translating (by default) the content on your page to whatever language the end-user's browser is set for. They can optionally select a different language, but there's no way to use the element to automatically translate the page into a given language for all of your visitors.




回答5:


Use the following php code to redirect the current page with 'googtrans' tag.

if(!isset($_GET['gt'])) {
  header("Location: ".$_SERVER['REQUEST_URI']."&gt=1#googtrans(en)");
  die(); 
}

Where 'en' stands for English.




回答6:


My Idea is to set session first. and check if session counter to 1. and then add javascript to set and change dropdown languange as desired.

Example :

function set_default_language () {
        session_start();
        if (!isset($_SESSION['views'])) { 
            $_SESSION['views'] = 0;
        }

        $_SESSION['views'] = $_SESSION['views']+1;
        if ( $_SESSION['views'] == 1 ) { ?> 
        <script type="text/javascript">

            var select = document.querySelector('select.notranslate');
            select.value    = "en|id";
            select.dispatchEvent(new Event('change'));
        </script>
        <?php    
        }
    } add_action( 'wp_footer', 'set_default_language');



回答7:


Go to your theme folder, and then to function.php where you add

// set the default language translation to potugese
set cookie('googtrans', '/en/pt');

at the end of the file.




回答8:


In the url you can place two languages.

https://translate.google.com/#no/en/Hello

This would translate the word Norwegian to English

https://translate.google.com/#{first country code}/{second one}/Hello



来源:https://stackoverflow.com/questions/1773687/google-translate-set-default-language

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!