willdurand / BazingaJsTranslationBundle

A pretty nice way to expose your Symfony translation messages to your client applications.
MIT License
572 stars 178 forks source link

Document how to use with AssetMapper #355

Open tacman opened 2 months ago

tacman commented 2 months ago

I'd like to use this bundle with AssetMapper, and install the translations during the compile that than from a script tag.

Like the jsRoutingBundle, this is a bit a tricky, since you can't simply load JSON:

https://github.com/FriendsOfSymfony/FOSJsRoutingBundle/issues/466

It certainly looks possible, to dump the message as js rather than json and then load it, but I was wondering if anyone had already done this and could point to how to do it.

Thanks!

alainsharemat commented 1 month ago

I was looking for a solution for the same problem. I ended up importing translations like that

// This will load the translation for the 'admin' domain (this is what path('bazinga_jstranslation_js', { 'domain': 'admin' }) would return in twig, adjust with your own path)
import '/path/to/translations/admin'

// Then load the Translator object 
import Translator from 'bazinga-translator'

console.log(Translator.trans('whatever'))