开发者

Google Translate set default language

Maybe this has an obvio开发者_如何转开发us 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.


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');


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).


You can set cookie in JS like this way

function setCookie(key, value, expiry) {
  var expires = new Date();
  expires.setTime(expires.getTime() + (expiry * 24 * 60 * 60 * 1000));
  document.cookie = key + '=' + value + ';expires=' + expires.toUTCString();
}

and call in the following way.

function googleTranslateElementInit() {
    setCookie('googtrans', '/en/pt',1);
    new google.translate.TranslateElement({
       pageLanguage: 'en'
    }, 'google_translate_element');
}


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.


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”);


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.


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');


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.


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


Based on Josh's answer: https://stackoverflow.com/a/12063697/4298115

A simple function to achieve this in vanilla JS:

var language = window.navigator.userLanguage || window.navigator.language;
window.location.replace(`/#googtrans(${language})`);

Then you can test using by changing your browser's locale: https://stackoverflow.com/a/64948204/4298115

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜