r4nd99y
Forum Replies Created
-
Forum: Plugins
In reply to: [Loco Translate] wp_set_script_translationsCurrently working on a plugin that I need in my native language but I still want to make it in English to maybe reuse later.
I set the i18n up following this guide.
Using
wp i18n make-pot
to createplugin/languages/plugin.pot
which includes php and JS strings (running it while the JS is compiled but not minifed, so the file references are good but__()
calls can still be identified).Then
cp plugin.pot plugin-en_US.po
to get the.po
andwp i18n make-json
to get the.json
files with their respective hashes based on the references.
Nowwp_set_script_translations()
loads these.json
s when applicable.Now to translate to another language with Loco:
the
.pot
is good, contains both PHP and JS strings. I create the new language with Loco towp-content/langauges/loco/plugins
, translate some strings and save. Since Loco doesn’t create the.json
‘s currently I runwp i18n make-json ./
in that directory to get them.If I saved the language to
wp-content/languages/plugins
it would already work (since it seems to be looking there by default), but since they’re in/loco
I also added this filter:add_filter('load_script_translation_file', function($file, $handle, $domain) { if($domain === 'my-domain') { if(strpos($file, 'wp-content/languages/plugins') !== false) { $loco_file = str_replace('wp-content/languages/plugins', 'wp-content/languages/loco/plugins', $file); if(file_exists($loco_file)) { return $loco_file; } } } return $file; }, 10, 3);
and with that I was able to translate JS strings with Loco.
A little caveat being that
wp i18n make-json
strips the JS strings from the.po
so next time I go to translate with Loco the JS strings are no longer available. So currently I back up the.po
and restore it after runningmake-json
.So that’s pretty much what I’ve got right now… is that a reasonable way to handle it?
Between the
pre_load_script_translations
,load_script_translation_file
andload_script_translations
filters, is there still no workable permanent solution for this plugin?Not sure how it’s supposed to work in the end, whether the
.po
should retain the JS strings after creating.json
s, or maybe Loco should allow to translate.json
s directly (probably cumbersome to find strings from different files unless you can pool them in the editor)?Is there still a development version that handles
.json
generation?Forum: Plugins
In reply to: [Loco Translate] wp_set_script_translationsAny update on this situation? I see that WC Gutenberg Products Blocks plugin includes some
json
files now, but no way to modify any fork/modify/generate anything with Loco?Thanks!
Forum: Plugins
In reply to: [Loco Translate] wp_set_script_translationsLooks like they also got the same thing going on, references in
languages/woo-gutenberg-products-block.php
point toassets/js/..
while the actual assets are served frombuild/
.Would it help to rewrite these references to point to the minified files after generating the
pot
? (on my end I mean, I don’t expect Loco to do that)- This reply was modified 6 years, 2 months ago by r4nd99y.
Forum: Plugins
In reply to: [Loco Translate] wp_set_script_translationsDo you have good reason to use an additional tool to generate POT files?
I generate the pot from development files and ship minified files, where
__()
is renamed to something else and I don’t expect strings to be extracted from there. So while the reference in the pot may be#: src/blocks/grid/row.js:32
the file loaded on Gutenberg page isdist/row.js
. I suppose this is why the development version of Loco Translate is giving me these warnings:Warning: src/blocks/grid/row.js not found in bundle
.I tested with the
woo-gutenberg-products-block
plugin. Did “Create template”, edited a string, it shows up in thewp-content/languages/loco/plugins/woo-gutenberg-products-block-en_US.po
:#: assets/js/product-category-block.js:158 msgid "Showing product block preview." msgstr "Test123"
It also generated 6
.json
files, none of which include the stringTest123
.On the Gutenberg page the string is also not translated, so (not) working as expected?
Forum: Plugins
In reply to: [Loco Translate] wp_set_script_translationsThanks for the updates. Hope this gets resolved soon.
From a plugin developing point of view, is it correct to provide a
pot
generated with babel-plugin-makepot and just callingwp_set_script_translations( 'my-handle', 'my-domain' );
should make it translatable with Loco in the future?There is also pot-to-php tool, any idea if that is just an outdated hack meant for plugins in www.ads-software.com repo or should all plugins include the strings in PHP form as well?