How to add Google Translate link that triggers translation?
Asked Answered
A

3

6

I have a web page in Bulgarian and I want my users be able to translate it one-click to English. Also there should not be any translation banner at the top of the page when a user enters to the page (it can after the user clicks the translation link). I have tried to use #googtrans(bg|en) (doc) but it didn't work, also it shows a banner at the top of the page due to this code:

<script>
function googleTranslateElementInit() {
  new google.translate.TranslateElement({
    pageLanguage: 'bg',
    autoDisplay: false,
    layout: google.translate.TranslateElement.InlineLayout.SIMPLE
  }, 'google_translate_element');
}
</script><script src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>

(doc)

the code is here krumovgrad.eu click the flags at the right top.

Atomism answered 30/11, 2011 at 18:3 Comment(4)
Translation banner with nonworking link. I don't want banner, I want link that works/translates.Atomism
Update: In Firefox; banner with a link that works. In IE 8 nothing.Atomism
Can you post the code or a jsfiddle so we can attempt to assist?Mclean
Update: I've removed the cookies in Fx and I've nothing. Behaves according to the old cookies.Atomism
N
10

I had the same issue a few days ago and came up with a solution that works. I have a site in Spanish, and until we translate it into English, we offer our users the possibility of using Google Website Translator. When users click en English flag, it opens a Twitter Bootstrap modal telling the user the website hasn't been translated yet, and there's a button they can click to trigger the translation. I capture the event with JavaScript, set the cookie 'googtrans' with the value '/es/en' and reload the page. The Google's script does the rest. After the reload, I check if the cookie exists and change the English flag for the Spanish flag. When the user clicks on it, I set the cookie to '' (empty string), and reload the page.

For simplicity's sake, I won't include the Bootstrap modal part.

Html

<!DOCTYPE html>
<html>
    <head>
    (...)
    <meta name="google-translate-customization" content="(YOUR_TRANSLATE_CUSTOMIZATION_ID)" />
    (...)
    </head>
    <body>
        (...)
        <a id="lang-change-en" class="lang-change" href="javascript:void(0);">
            <img src="images/en-flag.png" alt="English Flag">
        </a>
        (...)
        <script src="js/script.js"></script>
        <script src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
    </body>
</html>

Javascript (script.js)

function setCookie(cname, cvalue, exdays) {
    var expires;
    if (exdays === 0) {
        expires = '';
    } else {
        var d = new Date();
        d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
        expires = "expires=" + d.toGMTString();
    }
    var domain = (typeof domain === "undefined") ? '' : "; domain="+domain;
    document.cookie = cname + "=" + cvalue + "; " + expires + "path=" + path + domain;
}

function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i].trim();
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}

//Google provides this function
function googleTranslateElementInit() {
    new google.translate.TranslateElement({
        pageLanguage: 'es',
        includedLanguages: 'en',
        layout: google.translate.TranslateElement.InlineLayout.SIMPLE,
       autoDisplay: false
    },'google_translate_element');
}

//Using jQuery
$(document).ready(function() {
    $(document).on('click','#lang-change-en', function() {
        setCookie('googtrans', '/es/en', 0, '.viajoasalta.com');
        setCookie('googtrans', '', 0, '/');
        location.reload();
    });

    $(document).on('click', '#lang-change-es', function() {
        setCookie('googtrans', '', 0, '/', '.viajoasalta.com');
        setCookie('googtrans', '', 0, '/');
        location.reload();
    });

    var googTrans = getCookie('googtrans');

    if (googTrans === '/es/en') {
        var src = $('#lang-change-en > img').attr('src').replace('en-flag', 'es-flag');
        $('#lang-change-en > img').attr('src', src);
        $('#lang-change-en').attr('id', 'lang-change-es');
    }
});

In the Website Translator setup wizard, you can select the languages you want to appear in the list. You then can have your own <select> where each <option> has as the value the value of the cookie it should have, or an ordinary list with flags with something like data-cookie="value". Then with JavaScript you capture the 'change' event (for the select) or the 'click' event for the list, and set the cookie appropriately.

Note: I intentionally removed the div where the Website Translator gets rendered:

<div id="google_translate_element"></div>

To see it working, you can visit www.viajoasalta.com; at least until we finally translate it.

Notional answered 23/3, 2014 at 15:34 Comment(2)
The problem in your code is the setCookie() function as it writes a second cookie with the same name because you didn't specify a path. Look here, point 1: sitepoint.com/3-things-about-cookies-you-may-not-know On your viajoasalta.com website you can also see this. When you click "view original" in the Translate bar, the googtrans cookie written by the Google script is deleted but the duplicate still forces the page to be translated and also messes up your flags. It's a simple fix: function setCookie(cname, cvalue) { document.cookie = cname + "=" + cvalue + "; path=/"; }Aaron
@FFish thanks for your comment. I modified the setCookie() function so it accepts both path and domain parameters. I needed the domain parameter because Google's Website Translator set cookies for "www.viajoasalta.com" and ".viajoasalta.com". I use 'exdays' equals to 0 so the cookie's lifetime is that of the session. I still don't know how to update the flags when you click "view original" in the Translate bar. If I find out how, I'll post a new comment.Notional
Q
1

Google has thought ahead my friend. Please look at this page: http://translate.google.com/translate_tools

EDIT: I'm sorry, I just realized you're using what the page provides! You can, with simple javascript, hide the elements that are displayed and create a link for English where it's onClick changes the value of the hidden select element...and your entire page is translated.

It's a bit messy but it gets the job done and the user doesn't know it exists!

You can also consider capturing the request that is sent to the google translate servers and capture the link that is called when you select English and just use that link.

Chrome has a nice utility to capture requests (see ctrl+shift+j for a developer console)

Quadruple answered 30/11, 2011 at 18:20 Comment(0)
V
-1

For example, your web URI is http://localhost/cars/index.php

by changing it to

http://localhost/cars/index.php#googtrans(fr)

will translate the page to French with a google-translate-iframe on top

To disable the iframe banner, add the following code in page CSS

.goog-te-banner-frame {
    display: none;
}

Please refer to Sibin John Mattappallil's answer to this question

Vogue answered 7/1, 2021 at 3:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.