From d24b24adee9d686c100b8b4e298abbb3ad89a950 Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Wed, 26 Feb 2014 16:45:59 +0100 Subject: [PATCH] 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 Signed-off-by: Katrin Fischer Signed-off-by: Galen Charlton --- Koha/I18N.pm | 36 +++++++++++++++++++------------- misc/translator/LangInstaller.pm | 2 +- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/Koha/I18N.pm b/Koha/I18N.pm index c1dffed2a4..1d6188b526 100644 --- a/Koha/I18N.pm +++ b/Koha/I18N.pm @@ -18,10 +18,10 @@ package Koha::I18N; # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. use Modern::Perl; -use base qw(Locale::Maketext); +use base qw(Locale::Maketext Exporter); -use C4::Templates; -use C4::Context; +use CGI; +use C4::Languages; use Locale::Maketext::Lexicon { 'en' => ['Auto'], @@ -31,22 +31,28 @@ use Locale::Maketext::Lexicon { . '/misc/translator/po/*-messages.po' ], '_AUTO' => 1, + '_style' => 'gettext', }; -sub get_handle_from_context { - my ($class, $cgi, $interface) = @_; - - my $lh; - my $lang = C4::Templates::getlanguage($cgi, $interface); - if ($lang) { - $lh = $class->get_handle($lang) - or die "No language handle for '$lang'"; - } else { - $lh = $class->get_handle() - or die "Can't get a language handle"; +our @EXPORT = qw( gettext ); + +my %language_handles; + +sub get_language_handle { + my $cgi = new CGI; + my $language = C4::Languages::getlanguage; + + if (not exists $language_handles{$language}) { + $language_handles{$language} = __PACKAGE__->get_handle($language) + or die "No language handle for '$language'"; } - return $lh; + return $language_handles{$language}; +} + +sub gettext { + my $lh = get_language_handle; + $lh->maketext(@_); } 1; diff --git a/misc/translator/LangInstaller.pm b/misc/translator/LangInstaller.pm index 95fd406af7..b89aaa87af 100644 --- a/misc/translator/LangInstaller.pm +++ b/misc/translator/LangInstaller.pm @@ -476,7 +476,7 @@ sub extract_messages { } my $xgettext_cmd = "$self->{xgettext} -L Perl --from-code=UTF-8 " . - "-kmaketext -o $Bin/$self->{domain}.pot -D $intranetdir"; + "-o $Bin/$self->{domain}.pot -D $intranetdir"; $xgettext_cmd .= " $_" foreach (@files_to_scan); if (system($xgettext_cmd) != 0) { -- 2.39.5