Actually (with bug 8044), to translate a string in a Perl script or module you have to write: use CGI; use Koha::I18N; my $cgi = new CGI; my $lh = Koha::I18N->get_handle_from_context($cgi, 'intranet'); print $lh->maketext('my translatable text'); With the patches I'll submit, this will become: use Koha::I18N; print gettext('my translatable text');
Created attachment 25664 [details] [review] Bug 11848: Move language detection function in C4::Languages and store interface (intranet, opac) in context to not have to pass it as parameter.
Created attachment 25665 [details] [review] Bug 11848: Make Koha::I18N easier to use Instead of writing use CGI; use Koha::I18N; my $cgi = new CGI; my $lh = Koha::I18N->get_handle_from_context($cgi, 'intranet'); print $lh->maketext('my translatable text'); you can now write use Koha::I18N; print gettext('my translatable text');
Test plan (for developpers): 1/ Edit a Perl script, for example mainpage.pl 2/ add "use Koha::I18N;" to the top of file 3/ add a translatable message somewhere in the script (this have to be after the call to get_template_and_user). For example: warn gettext("This is a translated warning"); 4/ Create or update the PO files with misc/translator/translate create LANGCODE or misc/translator/translate update LANGCODE (LANGCODE should be enable in syspref 'languages') 5/ In misc/translator/po/LANGCODE-messages.po you should have your string, translate it (using a text editor or a PO file editor, make sure you don't have the "fuzzy" flag for this string). 6/ Go to mainpage.pl with active language being English with your browser and check your logs. You should see your string "This is a translated warning". 7/ Now change language to LANGCODE. Check your logs, you should have the string translated. Note: I chose to name the sub 'gettext' because it's the default keyword for xgettext for Perl. We can change it to whatever we want.
Note 2: The purpose of Koha::I18N is *not* to translate warning messages (warnings should not be in the language of the person actually using Koha), but it's the quickest way to test this (I think)
Comment on attachment 25664 [details] [review] Bug 11848: Move language detection function in C4::Languages Review of attachment 25664 [details] [review]: ----------------------------------------------------------------- ::: C4/Context.pm @@ +1252,5 @@ > > +sub interface { > + my ($class, $interface) = @_; > + > + if (defined $interface) { You are using interface as both a get and set, correct? @@ +1253,5 @@ > +sub interface { > + my ($class, $interface) = @_; > + > + if (defined $interface) { > + $interface ||= 'opac'; Why not // instead? Also, interface is defined. This line is pointless. @@ +1254,5 @@ > + my ($class, $interface) = @_; > + > + if (defined $interface) { > + $interface ||= 'opac'; > + $context->{interface} = $interface; perhaps a lc or uc may be useful? maybe some validation of the value? @@ +1257,5 @@ > + $interface ||= 'opac'; > + $context->{interface} = $interface; > + } > + > + return $context->{interface}; What if you haven't set the interface and you get undef? It would seem the default assumption is 'opac'. Perhaps add a "// 'opac'"?
Comment on attachment 25664 [details] [review] Bug 11848: Move language detection function in C4::Languages Review of attachment 25664 [details] [review]: ----------------------------------------------------------------- ::: t/Templates.t @@ +42,4 @@ > }, > ); > > +delete $ENV{HTTP_ACCEPT_LANGUAGE}; Good catch on this typo that has been there since 2013-07-12 14:57:11.
> You are using interface as both a get and set, correct? Correct! > Why not // instead? > Also, interface is defined. This line is pointless. C4::Context->interface # getter only C4::Context->interface('') # getter and setter, set to opac because '' is a defined but false value > perhaps a lc or uc may be useful? maybe some validation of the value? > What if you haven't set the interface and you get undef? It would seem the > default assumption is 'opac'. Perhaps add a "// 'opac'"? Sure. I will provide a patch for these issues. But not immediately, maybe next week. Thanks for the review! :)
Created attachment 26426 [details] [review] Bug 11848: Fix C4::Context::interface, add POD and UT
Finally, earlier than expected ;)
Created attachment 26494 [details] [review] [SIGNED-OFF] Bug 11848: Move language detection function in C4::Languages and store interface (intranet, opac) in context to not have to pass it as parameter. Signed-off-by: Bernardo Gonzalez Kriegel <bgkriegel@gmail.com> No koha-qa errors
Created attachment 26495 [details] [review] [SIGNED-OFF] Bug 11848: Make Koha::I18N easier to use Instead of writing use CGI; use Koha::I18N; my $cgi = new CGI; my $lh = Koha::I18N->get_handle_from_context($cgi, 'intranet'); print $lh->maketext('my translatable text'); you can now write use Koha::I18N; print gettext('my translatable text'); Signed-off-by: Bernardo Gonzalez Kriegel <bgkriegel@gmail.com> No errors
Created attachment 26496 [details] [review] [SIGNED-OFF] Bug 11848: Fix C4::Context::interface, add POD and UT Signed-off-by: Bernardo Gonzalez Kriegel <bgkriegel@gmail.com> Follow test plan, work as described. No koha-qa errors. Tests pass Fixed small merge conflict on t/Context.t
Created attachment 27747 [details] [review] [PASSED QA] Bug 11848: Move language detection function in C4::Languages and store interface (intranet, opac) in context to not have to pass it as parameter. Signed-off-by: Bernardo Gonzalez Kriegel <bgkriegel@gmail.com> No koha-qa errors Signed-off-by: Katrin Fischer <Katrin.Fischer.83@web.de> Comments on last patch.
Created attachment 27748 [details] [review] [PASSED QA] Bug 11848: Make Koha::I18N easier to use Instead of writing use CGI; use Koha::I18N; my $cgi = new CGI; my $lh = Koha::I18N->get_handle_from_context($cgi, 'intranet'); print $lh->maketext('my translatable text'); you can now write use Koha::I18N; print gettext('my translatable text'); Signed-off-by: Bernardo Gonzalez Kriegel <bgkriegel@gmail.com> No errors Signed-off-by: Katrin Fischer <Katrin.Fischer.83@web.de>
Created attachment 27749 [details] [review] [PASSED QA] Bug 11848: Fix C4::Context::interface, add POD and UT 1/ Edit a Perl script, for example mainpage.pl 2/ add "use Koha::I18N;" to the top of file 3/ add a translatable message somewhere in the script (this have to be after the call to get_template_and_user). For example: warn gettext("This is a translated warning"); 4/ Create or update the PO files with misc/translator/translate create LANGCODE or misc/translator/translate update LANGCODE (LANGCODE should be enable in syspref 'languages') 5/ In misc/translator/po/LANGCODE-messages.po you should have your string, translate it (using a text editor or a PO file editor, make sure you don't have the "fuzzy" flag for this string). 6/ Go to mainpage.pl with active language being English with your browser and check your logs. You should see your string "This is a translated warning". 7/ Now change language to LANGCODE. Check your logs, you should have the string translated. Note: I chose to name the sub 'gettext' because it's the default keyword for xgettext for Perl. We can change it to whatever we want. Signed-off-by: Bernardo Gonzalez Kriegel <bgkriegel@gmail.com> Follow test plan, work as described. No koha-qa errors. Tests pass Fixed small merge conflict on t/Context.t Signed-off-by: Katrin Fischer <Katrin.Fischer.83@web.de> Passes all tests and QA script. Copied test plan from bug.
Pushed to master. Thanks, Julian!