Created attachment 102643 [details] The Mannequin plugin to test this Bug To make internationalised plugin, it may be useful to get the KohaOpacLanguage from Koha cookie. We could then branch an include template containing all strings in the page in a specific language à la Theke solutions (see https://gitlab.com/thekesolutions/plugins/koha-plugin-pay-via-paypal/-/blob/master/Koha/Plugin/Com/Theke/PayViaPayPal/opac_online_payment_error.tt) The idea is to have Koha/Plugin/Com/Something/Superplugin/i18n with lang_dialect.inc files. It contains a TOKENS hash with all the string you need for your plugin.
Created attachment 102644 [details] [review] Bug 25099: Add lang from cookie to plugins template params To make internationalised plugins, it may be useful to get the KohaOpacLanguage from Koha cookie. This patch adds the value of $cgi->cookie('KohaOpacLanguage') to a LANG variable which is sent to the template plugin. In your plugin, you can add a i18n directory containing lang[-DIALECT].inc files with a TOKENS hash like this: [% TOKENS = { PLUGIN_NAME = "Mannequin" PRESENTATION = "Voici un example d'outil." CLICK_ME = "Cliquez moi !" } %] The plugin template can then include such a lang[-DIALECT].inc with a hash containing all the plugin strings: [% TRY %] [% PROCESS "$PLUGIN_DIR/i18n/${LANG}.inc" %] [% CATCH %] [% PROCESS "$PLUGIN_DIR/i18n/default.inc" %] [% END %] The strings are then printed in the template with something like: [% TOKENS.PRESENTATION %] Test plan: 1. apply patch 2. install, enable and run BULAC Mannequin plugin 3. it should print stuff in english by default 4. install french French (fr-FR) 5. change you Koha lang to French 6. run again the plugin 7. ho là là! French!
Hi Using directly a raw cookie is generally unsafe. cf. https://bugs.koha-community.org/bugzilla3//show_bug.cgi?id=6629 C4::Languages::getlanguage(); is available and I believe its uses is fair game for plugins.
(In reply to didier from comment #2) > Hi > > Using directly a raw cookie is generally unsafe. > cf. > > https://bugs.koha-community.org/bugzilla3//show_bug.cgi?id=6629 > > C4::Languages::getlanguage(); > > is available and I believe its uses is fair game for plugins. !!! You're totally right! Thanks!
So should we send a LANG variable to template with C4::Languages::getlanguage and create an “official way to do translations in plug-ins” or should anyone get along with what fits him most and use C4::Languages::getlanguage directly from its own plug-in?
(In reply to Nicolas Legrand from comment #4) > So should we send a LANG variable to template with > C4::Languages::getlanguage and create an “official way to do translations in > plug-ins” or should anyone get along with what fits him most and use > C4::Languages::getlanguage directly from its own plug-in? I agree with stashing the LANG variable like this, and also agree it should be done using getlanguages() as the PayPal plugin does, but in Base.pm.
(In reply to Tomás Cohen Arazi from comment #5) > (In reply to Nicolas Legrand from comment #4) > > So should we send a LANG variable to template with > > C4::Languages::getlanguage and create an “official way to do translations in > > plug-ins” or should anyone get along with what fits him most and use > > C4::Languages::getlanguage directly from its own plug-in? > > I agree with stashing the LANG variable like this, and also agree it should > be done using getlanguages() as the PayPal plugin does, but in Base.pm. The only problem I see is that one should not reference C4 namespace in Koha namespace :).
Created attachment 104309 [details] [review] Bug 25099: Sending a LANG variable to plug-in template To make internationalised plug-ins, it may be useful to get the lang from C4::Languages::getlanguage. This patch adds C4::Languages::getlanguage result to a LANG variable which is sent to the template plugin. In your plugin, you can add a i18n directory containing lang[-DIALECT].inc files with a TOKENS hash like this: [% TOKENS = { PLUGIN_NAME = "Mannequin" PRESENTATION = "Voici un example d'outil." CLICK_ME = "Cliquez moi !" } %] The plugin template can then include such a lang[-DIALECT].inc with a hash containing all the plugin strings: [% TRY %] [% PROCESS "$PLUGIN_DIR/i18n/${LANG}.inc" %] [% CATCH %] [% PROCESS "$PLUGIN_DIR/i18n/default.inc" %] [% END %] The strings are then printed in the template with something like: [% TOKENS.PRESENTATION %] Test plan: 1. apply patch 2. install, enable and run BULAC Mannequin plugin 3. it should print stuff in english by default 4. install french French (fr-FR) 5. change you Koha lang to French 6. run again the plugin 7. ho là là! French! Thanks to Tomàs Cohen and his pay via paypal plug-in from which most ideas in this patch are taken.
I let the QA team decide if C4::Languages::getlanguage called from Koha::Plugin::Base is legit or not :).
(In reply to Nicolas Legrand from comment #8) > I let the QA team decide if C4::Languages::getlanguage called from > Koha::Plugin::Base is legit or not :). That's not a problem, Nicolas.
Created attachment 104317 [details] [review] Bug 25099: Sending a LANG variable to plug-in template To make internationalised plug-ins, it may be useful to get the lang from C4::Languages::getlanguage. This patch adds C4::Languages::getlanguage result to a LANG variable which is sent to the template plugin. In your plugin, you can add a i18n directory containing lang[-DIALECT].inc files with a TOKENS hash like this: [% TOKENS = { PLUGIN_NAME = "Mannequin" PRESENTATION = "Voici un example d'outil." CLICK_ME = "Cliquez moi !" } %] The plugin template can then include such a lang[-DIALECT].inc with a hash containing all the plugin strings: [% TRY %] [% PROCESS "$PLUGIN_DIR/i18n/${LANG}.inc" %] [% CATCH %] [% PROCESS "$PLUGIN_DIR/i18n/default.inc" %] [% END %] The strings are then printed in the template with something like: [% TOKENS.PRESENTATION %] Test plan: 1. apply patch 2. install, enable and run BULAC Mannequin plugin 3. it should print stuff in english by default 4. install french French (fr-FR) 5. change you Koha lang to French 6. run again the plugin 7. ho là là! French! Thanks to Tomàs Cohen and his pay via paypal plug-in from which most ideas in this patch are taken. Signed-off-by: Victor Grousset/tuxayo <victor@tuxayo.net>
ho là là ça marche! It works!
Created attachment 104374 [details] [review] Bug 25099: Sending a LANG variable to plug-in template To make internationalised plug-ins, it may be useful to get the lang from C4::Languages::getlanguage. This patch adds C4::Languages::getlanguage result to a LANG variable which is sent to the template plugin. In your plugin, you can add a i18n directory containing lang[-DIALECT].inc files with a TOKENS hash like this: [% TOKENS = { PLUGIN_NAME = "Mannequin" PRESENTATION = "Voici un example d'outil." CLICK_ME = "Cliquez moi !" } %] The plugin template can then include such a lang[-DIALECT].inc with a hash containing all the plugin strings: [% TRY %] [% PROCESS "$PLUGIN_DIR/i18n/${LANG}.inc" %] [% CATCH %] [% PROCESS "$PLUGIN_DIR/i18n/default.inc" %] [% END %] The strings are then printed in the template with something like: [% TOKENS.PRESENTATION %] Test plan: 1. apply patch 2. install, enable and run BULAC Mannequin plugin 3. it should print stuff in english by default 4. install french French (fr-FR) 5. change you Koha lang to French 6. run again the plugin 7. ho là là! French! Thanks to Tomàs Cohen and his pay via paypal plug-in from which most ideas in this patch are taken. Signed-off-by: Victor Grousset/tuxayo <victor@tuxayo.net> Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Passing QA, this works well and is a useful addition to the plugin architecture. We do however, need something for the release notes and even better some plugin developer documentation somewhere..
(In reply to Martin Renvoize from comment #13) > Passing QA, this works well and is a useful addition to the plugin > architecture. > > We do however, need something for the release notes What about: “[Bug 25099] Sending a LANG variable to plug-in template”? > and even better some > plugin developer documentation somewhere.. There isn't much documentation apart from “take a look at kitchen sink” right?. I can make some pull requests with internationalized kitchen sink?
(In reply to Nicolas Legrand from comment #14) > (In reply to Martin Renvoize from comment #13) > > Passing QA, this works well and is a useful addition to the plugin > > architecture. > > > > We do however, need something for the release notes > > What about: “[Bug 25099] Sending a LANG variable to plug-in template”? > > > and even better some > > plugin developer documentation somewhere.. > > There isn't much documentation apart from “take a look at kitchen sink” > right?. I can make some pull requests with internationalized kitchen sink? Add the first Koha version with LANG available?
I think updating kitchen sink would be a good start. I'd love more general documentaiton than 'read the code' still, but at least there it's all in one place for now. See also: https://wiki.koha-community.org/wiki/Koha_Plugin_Hooks
Nice work everyone! Pushed to master for 20.05
Backported to 19.11.x for 19.11.06