BazingaJsTranslationBundle icon indicating copy to clipboard operation
BazingaJsTranslationBundle copied to clipboard

Uncaught ReferenceError: Translator is not defined when using Webpack Encore

Open ptyskju opened this issue 6 years ago • 9 comments

I am using Symfony 4.1 with installed Webpack Encore. Translator is properly imported by
var Translator = require('bazinga-translator');
but I am receiving that error in my console: Uncaught ReferenceError: Translator is not defined Translator is located correctly in app.js file.

ptyskju avatar Aug 05 '18 20:08 ptyskju

Hello, try this

In base.html.twig add before main application javascript file

<script src="{{ asset('bundles/bazingajstranslation/js/translator.min.js') }}"></script>
<script src="{{ url('bazinga_jstranslation_js'  }}"></script>

<script src="{{ asset('build/app.js') }}"></script> <-- builded javascript file

In webpack.config.js add this

module.exports = Encore.getWebpackConfig();
module.exports.externals = [{
        'bazinga-translator': 'Translator'
    }
];

And compile

yarn encore production

dirmax avatar Aug 24 '18 09:08 dirmax

Tip : To use the bundle with VueJs & Encore, add this line before new Vue : Vue.prototype.$translator = Translator; Now we can use {{ this.$translator.trans('string') }} in our components.

Uneo7 avatar Dec 04 '18 18:12 Uneo7

For me, simply adding bazinga-translator to the autoProvideVariables function call of the Encore object in file webpack.config.js, after extending base.html.twig as described by @dirmax, also did the job:

Encore.autoProvideVariables({
    'bazinga-translator': 'Translator'
})

manuelkiessling avatar Jan 30 '19 06:01 manuelkiessling

I have the same error when I try SSR. I've already set autoProvideVariables and tried to conditionally load with if (typeof window === 'undefined') { const Translator = require('bazinga-translator'); }

Nothing worked so far. How should I fix that?

degregar avatar May 20 '19 15:05 degregar

For now I've made a quick workaround:

  1. Define new variable in the file I want to use Translator const TranslatorBackup = require('bazinga-translator');

  2. Check if I have my old variable, if not, use new one: {typeof Translator === "undefined" ? TranslatorBackup.trans(item.value) : Translator.trans(item.value)}

I've tried to define it above the class I'm rendering this in, but it throws same errors.

degregar avatar May 20 '19 16:05 degregar

If you don't mind dumping your languages, and loading ALL languages into your built file, then the following webpack works without any extra script tags in the twig templates.

// Load the Translator from the composer version
export const Translator = require('../../vendor/willdurand/js-translation-bundle/Resources/js/translator');
// Make it global
global.Translator = Translator;
// Load the default config
require('./bazinga/translations/config');
// Load each language here. When we add more languages, we need to add them here.
require('./bazinga/translations/en');

I'm using the dump command bin/console bazinga:js-translation:dump --format=js --merge-domains assets/js/bazinga to put the files in a useful spot for webpack, and to just dump out the JS versions (with merged domains to again shrink the folder structure)

timwhite avatar Dec 28 '19 23:12 timwhite

For those interested, and thanks to other comments, I made it work in a separate component as described here: https://github.com/willdurand/BazingaJsTranslationBundle/issues/254#issuecomment-663666450

It allows to keep all the js managed by Webpack.

Schyzophrenic avatar Jul 24 '20 19:07 Schyzophrenic

Hello, try this

In base.html.twig add before main application javascript file

<script src="{{ asset('bundles/bazingajstranslation/js/translator.min.js') }}"></script>
<script src="{{ url('bazinga_jstranslation_js'  }}"></script>

<script src="{{ asset('build/app.js') }}"></script> <-- builded javascript file

In webpack.config.js add this

module.exports = Encore.getWebpackConfig();
module.exports.externals = [{
        'bazinga-translator': 'Translator'
    }
];

And compile

yarn encore production

Thank you so much it's working ! I had Translator undefined in Symfony 6.2

There is just a parenthesis missing here <script src="{{ url('bazinga_jstranslation_js') }}"></script>

I run npm run watch and Translator is defined now, great!

flora-le avatar Mar 10 '23 09:03 flora-le