From 85e9a58634b1411040c2105221c30cfa71e2ea70 Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Mon, 20 Aug 2012 11:34:52 +0200 Subject: [PATCH] Bug 8044: Translate basket CSV column names using Koha::I18N This is a test case for new module Koha::I18N. It translate CSV columns headers when you export a basket as CSV. To test: - go to a basket page in staff interface with english interface - click on "Export this basket as CSV" and check that column titles are there and in english - change language and click on the same button. - column titles are still in english - now open a terminal, go to misc/translator and run: ./translate create LANG (e.g. fr-FR) - translate the po misc/translator/po/LANG-messages.po with your favorite po editor. - go back to the navigator and click again on the Export button - column names must be translated, according to the PO file. --- C4/Acquisition.pm | 79 ++++++++++++-------- acqui/basket.pl | 4 +- .../prog/en/modules/acqui/csv/basket.tt | 3 - 3 files changed, 52 insertions(+), 34 deletions(-) delete mode 100644 koha-tmpl/intranet-tmpl/prog/en/modules/acqui/csv/basket.tt diff --git a/C4/Acquisition.pm b/C4/Acquisition.pm index 83031be..a462fdf 100644 --- a/C4/Acquisition.pm +++ b/C4/Acquisition.pm @@ -17,7 +17,6 @@ package C4::Acquisition; # with Koha; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - use strict; use warnings; use Carp; @@ -34,6 +33,7 @@ use C4::Templates qw(gettemplate); use Time::localtime; use HTML::Entities; +use Text::CSV::Encoded; use vars qw($VERSION @ISA @EXPORT); @@ -240,51 +240,70 @@ $cgi parameter is needed for column name translation =cut sub GetBasketAsCSV { - my ($basketno, $cgi) = @_; + my ($basketno, $lh) = @_; my $basket = GetBasket($basketno); my @orders = GetOrders($basketno); my $contract = GetContract($basket->{'contractnumber'}); + my $csv = Text::CSV::Encoded->new(); + my $output; + + my @headers = ( + $lh->maketext('Contract name'), + $lh->maketext('Order number'), + $lh->maketext('Entry date'), + $lh->maketext('ISBN'), + $lh->maketext('Author'), + $lh->maketext('Title'), + $lh->maketext('Publication year'), + $lh->maketext('Publisher code'), + $lh->maketext('Collection title'), + $lh->maketext('Notes'), + $lh->maketext('Quantity'), + $lh->maketext('RRP'), + $lh->maketext('Delivery place'), + $lh->maketext('Billing place'), + ); - my $template = C4::Templates::gettemplate("acqui/csv/basket.tmpl", "intranet", $cgi); + $csv->combine(@headers); + $output = $csv->string() . "\n"; my @rows; foreach my $order (@orders) { my $bd = GetBiblioData( $order->{'biblionumber'} ); - my $row = { - contractname => $contract->{'contractname'}, - ordernumber => $order->{'ordernumber'}, - entrydate => $order->{'entrydate'}, - isbn => $order->{'isbn'}, - author => $bd->{'author'}, - title => $bd->{'title'}, - publicationyear => $bd->{'publicationyear'}, - publishercode => $bd->{'publishercode'}, - collectiontitle => $bd->{'collectiontitle'}, - notes => $order->{'notes'}, - quantity => $order->{'quantity'}, - rrp => $order->{'rrp'}, - deliveryplace => C4::Branch::GetBranchName( $basket->{'deliveryplace'} ), - billingplace => C4::Branch::GetBranchName( $basket->{'billingplace'} ), - }; - foreach(qw( - contractname author title publishercode collectiontitle notes - deliveryplace billingplace - ) ) { + my @row = ( + $contract->{'contractname'}, + $order->{'ordernumber'}, + $order->{'entrydate'}, + $order->{'isbn'}, + $bd->{'author'}, + $bd->{'title'}, + $bd->{'publicationyear'}, + $bd->{'publishercode'}, + $bd->{'collectiontitle'}, + $order->{'notes'}, + $order->{'quantity'}, + $order->{'rrp'}, + C4::Branch::GetBranchName($basket->{'deliveryplace'}), + C4::Branch::GetBranchName($basket->{'billingplace'}), + ); + foreach (@row) { # Double the quotes to not be interpreted as a field end - $row->{$_} =~ s/"/""/g if $row->{$_}; + $_ =~ s/"/""/g; } - push @rows, $row; + push @rows, \@row; } @rows = sort { - if(defined $a->{publishercode} and defined $b->{publishercode}) { - $a->{publishercode} cmp $b->{publishercode}; + if(defined $a->[7] and defined $b->[7]) { + $a->[7] cmp $b->[7]; } } @rows; + foreach my $row (@rows) { + $csv->combine(@$row); + $output .= $csv->string . "\n"; + } - $template->param(rows => \@rows); - - return $template->output; + return $output; } diff --git a/acqui/basket.pl b/acqui/basket.pl index ace6b63..d4cc213 100755 --- a/acqui/basket.pl +++ b/acqui/basket.pl @@ -35,6 +35,7 @@ use C4::Members qw/GetMember/; #needed for permissions checking for changing ba use C4::Items; use C4::Suggestions; use Date::Calc qw/Add_Delta_Days/; +use Koha::I18N; =head1 NAME @@ -144,11 +145,12 @@ if ( $op eq 'delete_confirm' ) { print $query->redirect('/cgi-bin/koha/acqui/basketgroup.pl?basketno=' . $basket->{'basketno'} . '&op=attachbasket&booksellerid=' . $booksellerid); # check if we have to "close" a basket before building page } elsif ($op eq 'export') { + my $lh = Koha::I18N->get_handle_from_context($query, 'intranet'); print $query->header( -type => 'text/csv', -attachment => 'basket' . $basket->{'basketno'} . '.csv', ); - print GetBasketAsCSV($query->param('basketno'), $query); + print GetBasketAsCSV($query->param('basketno'), $lh); exit; } elsif ($op eq 'close') { my $confirm = $query->param('confirm') || $confirm_pref eq '2'; diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/csv/basket.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/csv/basket.tt deleted file mode 100644 index 2d5c2df..0000000 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/csv/basket.tt +++ /dev/null @@ -1,3 +0,0 @@ -Contract name,Order number,Entry date,ISBN,Author,Title,Publication year,Publisher code,Collection title,Notes,Quantity,RRP,Delivery place,Billing place -[% FOREACH r IN rows %]"[% r.contractname %]",[% r.ordernumber %],[% r.entrydate %],[% r.isbn %],"[% r.author %]","[% r.title %]",[% r.publicationyear %],"[% r.publishercode %]","[% r.collectiontitle %]","[% r.notes %]",[% r.quantity %],[% r.rrp %],"[% r.deliveryplace %]","[% r.billingplace %]" -[% END %] -- 1.7.10.4