Google Translate set default language
Asked Answered
C

10

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.

Carrageen answered 20/11, 2009 at 22:44 Comment(2)
could you please share jquery working solution for this?Weeds
just got this working $($('span:contains("Select Language")')[1]).html('English')Weeds
C
28

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');
Cablegram answered 13/12, 2011 at 17:50 Comment(0)
B
25

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

Bomar answered 21/8, 2012 at 22:1 Comment(2)
This should be the accepted answer. I made another answer showing how this can be done and tested.Stylish
This is somewhat helpful but should not be the accepted answer. It should instead be a complete answer. How to AnswerVulgarian
C
7

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');
}
Cannady answered 26/7, 2020 at 7:28 Comment(1)
Thank you for this solution. Really helped. This could also be used to customize the google translate drop down.Heliotherapy
B
4

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.

Babbittry answered 8/3, 2017 at 9:27 Comment(1)
This may break your WordPress website, don't do this in function.phpFalciform
F
2

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”);
Finned answered 29/3, 2013 at 10:29 Comment(0)
S
2

Based on Josh's answer: https://mcmap.net/q/487768/-google-translate-set-default-language

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://mcmap.net/q/127392/-how-to-change-the-locale-in-chrome-browser

Stylish answered 10/12, 2020 at 0:8 Comment(0)
B
1

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.

Brocade answered 29/6, 2011 at 17:21 Comment(0)
F
1

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');
Fourdrinier answered 14/4, 2017 at 18:25 Comment(1)
or for cross browser use jquery instead. <code>function set_default_language () { session_start(); if (!isset($_SESSION['views'])) { $_SESSION['views'] = 0; } $_SESSION['views'] = $_SESSION['views']+ if ( $_SESSION['views'] == 1 ) { ?> <script type="text/javascript"> jQuery(document).ready(function($) { $('select.notranslate').val('en|id').trigger('change'); }); </script> <?php } } add_action( 'wp_footer', 'set_default_language'); ?> </code>Fourdrinier
A
1

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.

Attach answered 17/7, 2017 at 23:11 Comment(0)
D
1

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

Dobby answered 31/7, 2017 at 21:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.