Allowing custom handling of JSON translations
-
Thanks for the nice plugin, I’ve been a fan for quite sometime!
One thing that has been quite painful recently is JSON translations, I’ve been patching the plugin since 2.4 to get it working, broke with 2.4.6, and patched it again, and finally had to patch 2.5 as well to make the plugin output all the JS strings of a bundle in the same JSON file for it to work with our Webpack setup that output a single bundle file to production.
So, here is how I did this so others can benefit from the night I spent tracking down the required changes:
## the issue
WordPress, and subsequently loco-translate, assume that JS files, where strings are extracted from, would exist as is (or minified with the same name but suffixed with.min
) on production sites, and that breaks translations for any site that uses JS bundlers to output single-file bundles different from the original file names.## the solution
– Patch the plugin to add a filter before the JSON generation logic so I can take over, and use that filter to intercept the process and output a single JSON file with all translations from all the JS files within the bundle.
– Filterload_script_translation_file
to let WordPress know where the file will exist, rather than guessing the file path based on its relative URL / md5 hash.I’ve documented the patch and related PHP functionality in this gist: https://gist.github.com/shadyvb/f2c243bd83569d8cf9bf78c8269cc76b , maybe it comes handy for others.
I’ve noticed that you do not prefer pull-requests, hence why I’m posting here, so you can hopefully add some filters/actions so developers can customize the behavior of the plugin where needed instead of having to patch it like what I currently do. Specially given how popular is the use of bundlers like Webpack nowadays.
Cheers!
- The topic ‘Allowing custom handling of JSON translations’ is closed to new replies.