10 use YAML
::Syck
qw( Dump LoadFile );
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";
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";
41 my ($self, $id, $comment) = @_;
45 $p->comment( $p->comment . "\n" . $comment );
48 $po->{$id} = Locale
::PO
->new(
58 my ($self, $comment, $prefs) = @_;
60 for my $pref ( @
$prefs ) {
62 for my $element ( @
$pref ) {
63 if ( ref( $element) eq 'HASH' ) {
64 $pref_name = $element->{pref
};
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 );
80 $self->po_append( $self->{file
} . "#$pref_name# $element", $comment );
90 my $po = $self->{po
}->{$id};
92 return Locale
::PO
->dequote($po->msgstr);
97 my ($self, $pref, $prefs) = @_;
99 for my $p ( @
$prefs ) {
102 my @pref_array = @
$p;
103 for my $element ( @pref_array ) {
104 if ( ref( $element) eq 'HASH' ) {
105 $pref_name = $element->{pref
};
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;
123 my $text = $self->get_trans_text( $self->{file
} . "#$pref_name# $element" );
124 $p->[$i] = $text if $text;
131 sub get_po_from_prefs
{
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 );
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 );
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";
165 $self->get_po_from_prefs();
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.'"'};
184 my $text = Locale
::PO
->dequote( $po->msgstr );
185 $po_current->{$id}->msgstr( $text );
195 unless ( -r
$self->{path_lang
} ) {
196 print "Koha directories hierarchy for ", $self->{lang
}, " must be created first\n";
200 # Update the language .po file with last modified 'en' preferences
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 );
213 while ( my ($section, $sysprefs) = each %$tab_content ) {
214 $self->update_prefs( $pref, $sysprefs );
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);
240 pod2usage
( -verbose
=> 2 );
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;
262 pref-trans - Handle preferences translation
266 pref-trans init fr-FR
267 pref-trans update fr-FR
268 pref-trans install fr-FR
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.
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.