MT 2116: Addons to the CSV export
[koha.git] / misc / translator / pref-trans
blob9474a9821f7467d45f3b05859240487dc2bb702d
1 #!/usr/bin/perl
4 package Tokenizer;
6 use strict;
7 use warnings;
9 use C4::Context;
10 use YAML::Syck qw( Dump LoadFile );
11 use Data::Dumper;
12 use Locale::PO;
15 sub new {
16 my ($class, $lang) = @_;
18 my $self = { lang => $lang };
19 my $context = C4::Context->new();
20 $self->{path_en} = $context->config('intrahtdocs') .
21 '/prog/en/modules/admin/preferences';
22 $self->{path_lang} = $context->config('intrahtdocs') .
23 "/prog/$lang/modules/admin/preferences";
24 $self->{po} = {};
26 bless $self, $class;
30 sub po_filename {
31 my $self = shift;
33 my $context = C4::Context->new;
34 my $trans_path = $context->config('intranetdir') . '/misc/translator/po';
35 my $trans_file = "$trans_path/" . $self->{lang} . "-pref.po";
36 return $trans_file;
40 sub po_append {
41 my ($self, $id, $comment) = @_;
42 my $po = $self->{po};
43 my $p = $po->{$id};
44 if ( $p ) {
45 $p->comment( $p->comment . "\n" . $comment );
47 else {
48 $po->{$id} = Locale::PO->new(
49 -comment => $comment,
50 -msgid => $id,
51 -msgstr => ''
57 sub add_prefs {
58 my ($self, $comment, $prefs) = @_;
60 for my $pref ( @$prefs ) {
61 my $pref_name = '';
62 for my $element ( @$pref ) {
63 if ( ref( $element) eq 'HASH' ) {
64 $pref_name = $element->{pref};
65 last;
68 for my $element ( @$pref ) {
69 if ( ref( $element) eq 'HASH' ) {
70 while ( my ($key, $value) = each(%$element) ) {
71 next unless $key eq 'choices';
72 next unless ref($value) eq 'HASH';
73 for my $ckey ( keys %$value ) {
74 my $id = $self->{file} . "#$pref_name# " . $value->{$ckey};
75 $self->po_append( $id, $comment );
79 elsif ( $element ) {
80 $self->po_append( $self->{file} . "#$pref_name# $element", $comment );
87 sub get_trans_text {
88 my ($self, $id) = @_;
90 my $po = $self->{po}->{$id};
91 return unless $po;
92 return Locale::PO->dequote($po->msgstr);
96 sub update_prefs {
97 my ($self, $pref, $prefs) = @_;
99 for my $p ( @$prefs ) {
100 my $pref_name = '';
101 next unless $p;
102 my @pref_array = @$p;
103 for my $element ( @pref_array ) {
104 if ( ref( $element) eq 'HASH' ) {
105 $pref_name = $element->{pref};
106 last;
109 for my $i ( 0..$#pref_array ) {
110 my $element = $pref_array[$i];
111 if ( ref( $element) eq 'HASH' ) {
112 while ( my ($key, $value) = each(%$element) ) {
113 next unless $key eq 'choices';
114 next unless ref($value) eq 'HASH';
115 for my $ckey ( keys %$value ) {
116 my $id = $self->{file} . "#$pref_name# " . $value->{$ckey};
117 my $text = $self->get_trans_text( $id );
118 $value->{$ckey} = $text if $text;
122 elsif ( $element ) {
123 my $text = $self->get_trans_text( $self->{file} . "#$pref_name# $element" );
124 $p->[$i] = $text if $text;
131 sub get_po_from_prefs {
132 my $self = shift;
134 chdir( $self->{path_en} );
135 for my $file ( <*.pref> ) {
136 my $pref = LoadFile($file);
137 $self->{file} = $file;
138 #print Dump($pref), "\n";
139 while ( my ($tab, $tab_content) = each %$pref ) {
140 if ( ref($tab_content) eq 'ARRAY' ) {
141 $self->add_prefs( $tab, $tab_content );
142 next;
144 while ( my ($section, $sysprefs) = each %$tab_content ) {
145 my $comment = "$tab > $section";
146 $self->po_append( $self->{file} . " " . $section, $comment );
147 $self->add_prefs( $comment, $sysprefs );
154 sub save_po {
155 my $self = shift;
156 # Write .po entries into a file put in Koha standard po directory
157 Locale::PO->save_file_fromhash( $self->po_filename, $self->{po} );
158 print "Saved in file: ", $self->po_filename, "\n";
162 sub init {
163 my $self = shift;
165 $self->get_po_from_prefs();
166 $self->save_po();
170 sub update {
171 my $self = shift;
173 print "Update '", $self->{lang}, "' preferences .po file from 'en' .pref files\n";
174 # Get po from current 'en' .pref files
175 $self->get_po_from_prefs();
176 my $po_current = $self->{po};
178 # Get po from previous generation
179 my $po_previous = Locale::PO->load_file_ashash( $self->po_filename );
181 for my $id ( keys %$po_current ) {
182 my $po = $po_previous->{'"'.$id.'"'};
183 next unless $po;
184 my $text = Locale::PO->dequote( $po->msgstr );
185 $po_current->{$id}->msgstr( $text );
188 $self->save_po();
192 sub install {
193 my $self = shift;
195 unless ( -r $self->{path_lang} ) {
196 print "Koha directories hierarchy for ", $self->{lang}, " must be created first\n";
197 exit;
200 # Update the language .po file with last modified 'en' preferences
201 # and load it.
202 $self->update();
204 chdir( $self->{path_en} );
205 for my $file ( <*.pref> ) {
206 my $pref = LoadFile($file);
207 $self->{file} = $file;
208 while ( my ($tab, $tab_content) = each %$pref ) {
209 if ( ref($tab_content) eq 'ARRAY' ) {
210 $self->update_prefs( $pref, $tab_content );
211 next;
213 while ( my ($section, $sysprefs) = each %$tab_content ) {
214 $self->update_prefs( $pref, $sysprefs );
216 my $ntab = {};
217 for my $section ( keys %$tab_content ) {
218 my $text = $self->get_trans_text($self->{file} . " $section");
219 my $nsection = $text ? $text : $section;
220 $ntab->{$nsection} = $tab_content->{$section};
222 $pref->{$tab} = $ntab;
224 my $file_trans = $self->{path_lang} . "/$file";
225 print "Write $file\n";
226 open my $fh, ">", $file_trans;
227 print $fh Dump($pref);
232 package Main;
234 use strict;
235 use warnings;
237 use Pod::Usage;
239 sub usage {
240 pod2usage( -verbose => 2 );
241 exit;
245 usage() if $#ARGV != 1;
247 my ($cmd, $lang) = @ARGV;
248 if ( $cmd =~ /init|install|update/i ) {
249 my $tokenizer = Tokenizer->new( $lang );
250 $tokenizer->init( ) if $cmd =~ /init/i;
251 $tokenizer->update( ) if $cmd =~ /update/i;
252 $tokenizer->install( ) if $cmd =~ /install/i;
254 else {
255 usage();
260 =head1 NAME
262 pref-trans - Handle preferences translation
264 =head1 SYNOPSYS
266 pref-trans init fr-FR
267 pref-trans update fr-FR
268 pref-trans install fr-FR
270 =head1 USAGE
272 =over
274 =item pref-trans init F<lang>
276 Create a .po file in po directory, named F<lang>-pref.po. This file contains
277 text to translate extracted from .pref files.
279 =item pref-trans update F<lang>
281 Update a .po file in po directory, named F<lang>-pref.po. This file contains
282 new text to translate extracted from .pref files. Previous translated text are
283 kept. There is a minor bug, which can't be fixed due to preferences data
284 struture: preferences tab subsection labels are lost when updating .po file.
286 =item pref-trans install F<lang>
288 Use F<lang>-pref.po file to translate the english version of preferences files
289 and copy those files in the appropriate directory.
291 =back
293 =head1 DESCRIPTION
295 Koha preferences are stored in a data structure found in
296 koha-tmpl/intranet-tmpl/en/module/admin/preferences/ files. Depending of user
297 language, other files are used. This script extract text from 'en' preference
298 files, and put them in one .po file. This .po file can be updated. When
299 completed, a .po file can be applied to create localized versions of
300 preferences templates.
302 =head1 COPYRIGHT AND LICENSE
304 Copyright 2010 by Tamil, s.a.r.l.
306 L<http://www.tamil.fr>
308 This script is free software; you can redistribute it and/or modify it under
309 the terms of the GNU Lesser General Public License, version 2.1.
311 =cut