3 # Copyright (C) 2010 Tamil s.a.r.l.
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 use YAML::Syck qw( Dump LoadFile );
27 $YAML::Syck::ImplicitTyping = 1;
30 my ($self, $lang) = @_;
32 $self->{lang} = $lang;
33 $self->{po_path_lang} = $self->{context}->config('intrahtdocs') .
34 "/prog/$lang/modules/admin/preferences";
39 my ($class, $lang, $pref_only) = @_;
43 my $context = C4::Context->new();
44 $self->{context} = $context;
45 $self->{path_pref_en} = $context->config('intrahtdocs') .
46 '/prog/en/modules/admin/preferences';
47 set_lang( $self, $lang ) if $lang;
48 $self->{pref_only} = $pref_only;
49 $self->{translator_path} = $context->config('intranetdir') . "/misc/translator";
50 $self->{path_po} = $self->{translator_path} . "/po";
53 # Get all .pref file names
54 opendir my $fh, $self->{path_pref_en};
55 my @pref_files = grep { /.pref/ } readdir($fh);
57 $self->{pref_files} = \@pref_files;
59 # Get all available language codes
60 opendir $fh, $self->{path_po};
61 my @langs = map { ($_) =~ /(.*)-i-opac/ }
62 grep { $_ =~ /.*-opac-/ } readdir($fh);
64 $self->{langs} = \@langs;
66 # Map for both interfaces opac/intranet
67 $self->{interface} = {
69 dir => $context->config('opachtdocs') . '/prog',
70 suffix => '-i-opac-t-prog-v-3002000.po',
73 dir => $context->config('intrahtdocs') . '/prog',
74 suffix => '-i-staff-t-prog-v-3002000.po',
85 my $context = C4::Context->new;
86 my $trans_path = $context->config('intranetdir') . '/misc/translator/po';
87 my $trans_file = "$trans_path/" . $self->{lang} . "-pref.po";
93 my ($self, $id, $comment) = @_;
97 $p->comment( $p->comment . "\n" . $comment );
100 $po->{$id} = Locale::PO->new(
101 -comment => $comment,
110 my ($self, $comment, $prefs) = @_;
112 for my $pref ( @$prefs ) {
114 for my $element ( @$pref ) {
115 if ( ref( $element) eq 'HASH' ) {
116 $pref_name = $element->{pref};
120 for my $element ( @$pref ) {
121 if ( ref( $element) eq 'HASH' ) {
122 while ( my ($key, $value) = each(%$element) ) {
123 next unless $key eq 'choices';
124 next unless ref($value) eq 'HASH';
125 for my $ckey ( keys %$value ) {
126 my $id = $self->{file} . "#$pref_name# " . $value->{$ckey};
127 $self->po_append( $id, $comment );
132 $self->po_append( $self->{file} . "#$pref_name# $element", $comment );
140 my ($self, $id) = @_;
142 my $po = $self->{po}->{$id};
144 return Locale::PO->dequote($po->msgstr);
148 sub update_tab_prefs {
149 my ($self, $pref, $prefs) = @_;
151 for my $p ( @$prefs ) {
154 for my $element ( @$p ) {
155 if ( ref( $element) eq 'HASH' ) {
156 $pref_name = $element->{pref};
160 for my $i ( 0..@$p-1 ) {
161 my $element = $p->[$i];
162 if ( ref( $element) eq 'HASH' ) {
163 while ( my ($key, $value) = each(%$element) ) {
164 next unless $key eq 'choices';
165 next unless ref($value) eq 'HASH';
166 for my $ckey ( keys %$value ) {
167 my $id = $self->{file} . "#$pref_name# " . $value->{$ckey};
168 my $text = $self->get_trans_text( $id );
169 $value->{$ckey} = $text if $text;
174 my $text = $self->get_trans_text( $self->{file} . "#$pref_name# $element" );
175 $p->[$i] = $text if $text;
182 sub get_po_from_prefs {
185 for my $file ( @{$self->{pref_files}} ) {
186 my $pref = LoadFile( $self->{path_pref_en} . "/$file" );
187 $self->{file} = $file;
188 #print Dump($pref), "\n";
189 while ( my ($tab, $tab_content) = each %$pref ) {
190 if ( ref($tab_content) eq 'ARRAY' ) {
191 $self->add_prefs( $tab, $tab_content );
194 while ( my ($section, $sysprefs) = each %$tab_content ) {
195 my $comment = "$tab > $section";
196 $self->po_append( $self->{file} . " " . $section, $comment );
197 $self->add_prefs( $comment, $sysprefs );
206 # Write .po entries into a file put in Koha standard po directory
207 Locale::PO->save_file_fromhash( $self->po_filename, $self->{po} );
208 print "Saved in file: ", $self->po_filename, "\n";
215 print "Update '", $self->{lang}, "' preferences .po file from 'en' .pref files\n";
216 # Get po from current 'en' .pref files
217 $self->get_po_from_prefs();
218 my $po_current = $self->{po};
220 # Get po from previous generation
221 my $po_previous = Locale::PO->load_file_ashash( $self->po_filename );
223 for my $id ( keys %$po_current ) {
224 my $po = $po_previous->{'"'.$id.'"'};
226 my $text = Locale::PO->dequote( $po->msgstr );
227 $po_current->{$id}->msgstr( $text );
237 unless ( -r $self->{po_path_lang} ) {
238 print "Koha directories hierarchy for ", $self->{lang}, " must be created first\n";
242 # Update the language .po file with last modified 'en' preferences
244 $self->update_prefs();
246 for my $file ( @{$self->{pref_files}} ) {
247 my $pref = LoadFile( $self->{path_pref_en} . "/$file" );
248 $self->{file} = $file;
249 while ( my ($tab, $tab_content) = each %$pref ) {
250 if ( ref($tab_content) eq 'ARRAY' ) {
251 $self->update_tab_prefs( $pref, $tab_content );
254 while ( my ($section, $sysprefs) = each %$tab_content ) {
255 $self->update_tab_prefs( $pref, $sysprefs );
258 for my $section ( keys %$tab_content ) {
259 my $text = $self->get_trans_text($self->{file} . " $section");
260 my $nsection = $text ? $text : $section;
261 $ntab->{$nsection} = $tab_content->{$section};
263 $pref->{$tab} = $ntab;
265 my $file_trans = $self->{po_path_lang} . "/$file";
266 print "Write $file\n";
267 open my $fh, ">", $file_trans;
268 print $fh Dump($pref);
277 "Install templates\n";
278 while ( my ($interface, $tmpl) = each %{$self->{interface}} ) {
280 " Install templates '$interface\n",
281 " From: $tmpl->{dir}/en/\n",
282 " To : $tmpl->{dir}/$self->{lang}\n",
283 " With: $self->{path_po}/$self->{lang}$tmpl->{suffix}\n";
284 my $lang_dir = "$tmpl->{dir}/$self->{lang}";
285 mkdir $lang_dir unless -d $lang_dir;
287 "$self->{translator_path}/tmpl_process3.pl install " .
288 "-i $tmpl->{dir}/en/ " .
289 "-o $tmpl->{dir}/$self->{lang} ".
290 "-s $self->{path_po}/$self->{lang}$tmpl->{suffix} -r"
299 "Update templates\n";
300 while ( my ($interface, $tmpl) = each %{$self->{interface}} ) {
302 " Update templates '$interface'\n",
303 " From: $tmpl->{dir}/en/\n",
304 " To : $self->{path_po}/$self->{lang}$tmpl->{suffix}\n";
305 my $lang_dir = "$tmpl->{dir}/$self->{lang}";
306 mkdir $lang_dir unless -d $lang_dir;
308 "$self->{translator_path}/tmpl_process3.pl update " .
309 "-i $tmpl->{dir}/en/ " .
310 "-s $self->{path_po}/$self->{lang}$tmpl->{suffix} -r"
318 $self->get_po_from_prefs();
327 "Create templates\n";
328 while ( my ($interface, $tmpl) = each %{$self->{interface}} ) {
330 " Create templates .po files for '$interface'\n",
331 " From: $tmpl->{dir}/en/\n",
332 " To : $self->{path_po}/$self->{lang}$tmpl->{suffix}\n";
334 "$self->{translator_path}/tmpl_process3.pl create " .
335 "-i $tmpl->{dir}/en/ " .
336 "-s $self->{path_po}/$self->{lang}$tmpl->{suffix} -r"
343 return unless $self->{lang};
344 $self->install_tmpl() unless $self->{pref_only};
345 $self->install_prefs();
351 return unless $self->{lang};
352 $self->update_tmpl() unless $self->{pref_only};
353 $self->update_prefs();
359 return unless $self->{lang};
360 $self->create_tmpl() unless $self->{pref_only};
361 $self->create_prefs();
371 LangInstaller.pm - Handle templates and preferences translation
375 my $installer = LangInstaller->new( 'fr-FR' );
376 $installer->create();
377 $installer->update();
378 $installer->install();
379 for my $lang ( @{$installer->{langs} ) {
380 $installer->set_lang( $lan );
381 $installer->install();
388 Create a new instance of the installer object.
392 For the current language, create .po files for templates and preferences based
393 of the english ('en') version.
397 For the current language, update .po files.
401 For the current langage C<$self->{lang}, use .po files to translate the english
402 version of templates and preferences files and copy those files in the
403 appropriate directory.
407 =item translate create F<lang>
409 Create 3 .po files in F<po> subdirectory: (1) from opac pages templates, (2)
410 intranet templates, and (3) from preferences.
414 =item F<lang>-opac.po
416 Contains extracted text from english (en) OPAC templates found in
417 <KOHA_ROOT>/koha-tmpl/opac-tmpl/prog/en/ directory.
419 =item F<lang>-intranet.po
421 Contains extracted text from english (en) intranet templates found in
422 <KOHA_ROOT>/koha-tmpl/intranet-tmpl/prog/en/ directory.
424 =item F<lang>-pref.po
426 Contains extracted text from english (en) preferences. They are found in files
427 located in <KOHA_ROOT>/koha-tmpl/intranet-tmpl/prog/en/admin/preferences
432 =item pref-trans update F<lang>
434 Update .po files in F<po> directory, named F<lang>-*.po.
436 =item pref-trans install F<lang>