Bug 25099

Summary: Sending a LANG variable to plug-in template
Product: Koha Reporter: Nicolas Legrand <nicolas.legrand>
Component: Plugin architectureAssignee: Nicolas Legrand <nicolas.legrand>
Status: CLOSED FIXED QA Contact: Testopia <testopia>
Severity: trivial    
Priority: P5 - low CC: 1joynelson, claire.hernandez, didier.gautheron, jonathan.druart, kyle, martin.renvoize, severine.queune, tomascohen, victor
Version: master   
Hardware: All   
OS: All   
Change sponsored?: Sponsored Patch complexity: ---
Documentation contact: Documentation submission:
Text to go in the release notes:
Make it easier to build internationalised plug-in by sending a LANG variable to the plug-in template.
Version(s) released in:
20.05.00, 19.11.06
Attachments: The Mannequin plugin to test this Bug
Bug 25099: Add lang from cookie to plugins template params
Bug 25099: Sending a LANG variable to plug-in template
Bug 25099: Sending a LANG variable to plug-in template
Bug 25099: Sending a LANG variable to plug-in template

Description Nicolas Legrand 2020-04-09 15:11:18 UTC
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.
Comment 1 Nicolas Legrand 2020-04-09 15:13:02 UTC
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!
Comment 2 Didier Gautheron 2020-05-01 17:32:21 UTC
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.
Comment 3 Nicolas Legrand 2020-05-04 08:54:20 UTC
(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!
Comment 4 Nicolas Legrand 2020-05-04 09:03:49 UTC
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?
Comment 5 Tomás Cohen Arazi 2020-05-04 11:02:26 UTC
(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.
Comment 6 Nicolas Legrand 2020-05-04 11:07:38 UTC
(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 :).
Comment 7 Nicolas Legrand 2020-05-04 15:34:19 UTC
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.
Comment 8 Nicolas Legrand 2020-05-04 15:38:01 UTC
I let the QA team decide if C4::Languages::getlanguage called from Koha::Plugin::Base is legit or not :).
Comment 9 Tomás Cohen Arazi 2020-05-04 15:45:43 UTC
(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.
Comment 10 Victor Grousset/tuxayo 2020-05-04 17:22:20 UTC
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>
Comment 11 Victor Grousset/tuxayo 2020-05-04 17:23:00 UTC
ho là là ça marche!
It works!
Comment 12 Martin Renvoize 2020-05-05 14:34:14 UTC
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>
Comment 13 Martin Renvoize 2020-05-05 14:35:13 UTC
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..
Comment 14 Nicolas Legrand 2020-05-07 13:21:47 UTC
(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?
Comment 15 Didier Gautheron 2020-05-07 13:32:45 UTC
(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?
Comment 16 Katrin Fischer 2020-05-07 21:14:55 UTC
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
Comment 17 Martin Renvoize 2020-05-11 09:01:17 UTC
Nice work everyone!

Pushed to master for 20.05
Comment 18 Joy Nelson 2020-05-11 22:09:01 UTC
Backported to 19.11.x for 19.11.06