• Resolved bkjproductions

    (@bkjproductions)


    What is the javascript function to call to switch to a language? I had thought it was doGTranslate.

    I do not want to use the default switcher produced by the plugin’s shortcode. I need a button that toggles from English to Chinese.

    The button is labeled “Chinese” in Chinese, so when you click it, it switches to the Chinese page.   On that page is a button that is labeled “English” in English, and when you click that, it switches to the English page. 

    I was thinking I could do something like this (where I have the Chinese button with a class of .change-language-to-chinese):

    jQuery(document).ready(function( $ ){
    $('.change-language-to-chinese').on('click',function() {
    doGTranslate('en|zh-CN');
    return false;
    });
    });

    But that does not appear to work. I think with the latest version (3.0.1) something fundamentally changed.

    (Also, I am assuming that if I load the switcher-via-shortcode somewhere on the page, and it is off-screen, then the necessary javascript should be loaded. With the previous version, I had simply sent a click to the switcher widget and it worked.)

    I have tried with support at Gtranslate.io but they do not seem to understand my request for the function.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author edo888

    (@edo888)

    Hi,

    You want to have just 2 languages, so you can use [gt-link …] shortcode to place the language links anywhere and you can also hide the current language with Custom CSS.

    [gt-link lang="en" label="English" widget_look="lang_names"]
    [gt-link lang="zh-CN" label="Chinese" widget_look="lang_names"]

    Custom CSS:

    a.gt-current-lang{display:none}

    You can also add additional CSS code to make the links look like buttons.

    Thanks! 🙂

    Thread Starter bkjproductions

    (@bkjproductions)

    @edo888 We would prefer to use our own button rather than the link. Later when we want to we can change to a different method altogether. All I need is the proper javascript function to call. Why would doGTranslate(‘en|zh-CN’); not work?

    Plugin Author edo888

    (@edo888)

    To be able to use it you need to make sure that doGTranslate function is defined. It is defined for example in gtranslate/js/base.js file, so you need to load it manually.

    Also it will not work unless you make sure Google Translate library is loaded.

    To load the library you can call:

    if(!window.gt_translate_script) {
        window.gt_translate_script=document.createElement('script');
        gt_translate_script.src='https://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit2';
        document.body.appendChild(gt_translate_script);
    }

    I recommend you to stick to [gt-link ...] shortcode to avoid issues.

    It is a big customization task and I cannot support you with it. If you know what you are doing, you probably can find a way around.

    Thanks! 🙂

    I used this code to capture the URL parameter and get links that translate the site.

    <script type="text/javascript">
    document.addEventListener("DOMContentLoaded", function() {
        if(!window.gt_translate_script) {
            window.gt_translate_script = document.createElement('script');
            gt_translate_script.src = 'https://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit2';
            document.body.appendChild(gt_translate_script);
        }
    
        const params = new URLSearchParams(window.location.search);
        const lang = params.get('lang');
        if (lang && localStorage.getItem("ChangeLanguageFirstTime") === null) {
            doGTranslate(pt|${lang});
            localStorage.setItem("ChangeLanguageFirstTime", true);
        }
    });
    </script>
    

    Then I used CSS to hide the widget

    .gtranslate_wrapper{
    	display:none!important;
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Who you gonna call? DogTranslate();’ is closed to new replies.