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,602
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

If you find solution then please post the solution. I am also experiencing same issue.

Thanks in advance

same here, please share if you found a solution,

I am also facing the same issue. If anyone fixed please post the solution.

I am also facing the same kind of issue. Kindly let me know if anyone has found a solution for this.

No its not working i have tried your solution

Hi @schnyadav161 we have implemented his solution yesterday, the js does not break anymore and customers can still use translations

Please share the file path and file name which need to be overriden. His post was not clear.

@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 */​

 

Code is working. Thanks for suggestion

HI @manuel-ego 

The error still exist please find the screenshot.It is not fixed.

schnyadav161_2-1703742506251.pngschnyadav161_3-1703742521512.png

 

@schnyadav161 this error does not cause the website to break. The above fix prevents an injection of the pako library into the Magento js modules that results in malfunctioning of the website entirely.

 

The error that you shared the screentshot of does not affect any functionality and it occurs on any website that is translated regardless of it is magento or any other. Because the code is inside the google translate chrome extension, we cannot change this.

@schnyadav161 did you confirm if the js file was updated after the deployment? If possible, please share the site url.

Hi @athar_ego 
I have tried your step but still getting the issue in console
Cannot read properties of undefined (reading 'gzip')

schnyadav161_1-1703742446117.png

 

schnyadav161_0-1703742419892.png