Support » Developing with WordPress » Problem using wp.i18n in frontend scripts

Viewing 2 replies - 1 through 2 (of 2 total)
  • By doing const { __, _x, _n, _nx } = wp.i18n; in the global scope in both scripts you end up declaring __ and co twice. That’s what the error is about.

    You should avoid declaring variables in the global scope.

    An easy way to fix this is wrapping your whole code in an immediately-invoked function expression (IIFE). Like this:

    'use strict';
    
    (function() {
    const { __, _x, _n, _nx } = wp.i18n;
    // ...
    // ...
    })()

    So this is not an issue with wp-i18n or anything, but in how you are re-declaring variables in the global scope. I recommend checking out some documentation how scope works in JS so you can avoid this kind of issues in the future.

    Thread Starter Giuseppe

    (@mociofiletto)

    Thanks @swissspidy I will amend those plugins.
    I misunderstood

    New! JavaScript i18n support in WordPress 5.0

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Problem using wp.i18n in frontend scripts’ is closed to new replies.