Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Google language translate script not working in magento2

<script type="text/javascript">
    function googleTranslateElementInit() {
        new google.translate.TranslateElement({
            pageLanguage: 'en',
        }, 'google_translate_element');
    }
</script>
<script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>

The script was functional on our Magento 2 website until last week. However, for the past four days, we've been encountering JavaScript and Knockout.js errors in the console when this script is active. Upon its removal, everything functions properly. We've also tested this on a fresh Magento 2 installation and experienced conflicts. Surprisingly, the script operates without issues on a standard static website. Could anyone assist in resolving this conflict?
Solved Solved
1 14 3,834
1 ACCEPTED SOLUTION

@Kathir-1995 Here are the steps:

  1. Copy the default require.js file from the following path: 

 

lib/web/requirejs/require.js​

 

  • Place it in your theme in the following path: 

 

app/design/frontend/<Your theme vendor>/<your theme name>/web/requirejs/require.js​

 

  • Locate the define function, it would start like this: 

 

define = function (name, deps, callback) {
        var node, context;​

 

  • After the first line add the following code in the define function: 

 

// Restrict pako library in google translate module from loading
    if(typeof name !== 'string' && isFunction(deps) && deps.toString().includes('pako')){
        return;
    }​

 

  • Now the define function should look something like this: 

 

define = function (name, deps, callback) {
        var node, context;

        // Restrict pako library in google translate module from loading
        if(typeof name !== 'string' && isFunction(deps) && deps.toString().includes('pako')){
            return;
        }
/* Rest of the define function code below */​

 

View solution in original post

14 REPLIES 14