Bug 3070: Fixes RSS feed for single result search.
[koha.git] / misc / translator / pref-trans
1 #!/usr/bin/perl
2
3
4 package Tokenizer;
5
6 use strict;
7 use warnings;
8
9 use C4::Context;
10 use YAML::Syck qw( Dump LoadFile );
11 use Data::Dumper;
12 use Locale::PO;
13
14
15 sub new {
16     my ($class, $lang) = @_;
17
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}        = {};
25
26     bless $self, $class;
27 }
28
29
30 sub po_filename {
31     my $self = shift;
32
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;
37 }
38
39
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 );
46     }
47     else {
48         $po->{$id} = Locale::PO->new(
49             -comment => $comment,
50             -msgid   => $id,
51             -msgstr  => ''
52         );
53     }
54 }
55
56
57 sub add_prefs {
58     my ($self, $comment, $prefs) = @_;
59
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;
66             }
67         }
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 );
76                     }
77                 }
78             }
79             elsif ( $element ) {
80                 $self->po_append( $self->{file} . "#$pref_name# $element", $comment );
81             }
82         }
83     }
84 }
85
86
87 sub get_trans_text {
88     my ($self, $id) = @_;
89
90     my $po = $self->{po}->{$id};
91     return unless $po;
92     return Locale::PO->dequote($po->msgstr);
93 }
94
95
96 sub update_prefs {
97     my ($self, $pref, $prefs) = @_;
98
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;
107             }
108         }
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;
119                     }
120                 }
121             }
122             elsif ( $element ) {
123                 my $text = $self->get_trans_text( $self->{file} . "#$pref_name# $element" );
124                 $p->[$i] = $text if $text;
125             }
126         }
127     }
128 }
129
130
131 sub get_po_from_prefs {
132     my $self = shift;
133
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;
143             }
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 );
148             }
149         }
150     }
151 }
152
153
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";
159 }
160
161
162 sub init {
163     my $self = shift;
164
165     $self->get_po_from_prefs();
166     $self->save_po();
167 }
168
169
170 sub update {
171     my $self = shift;
172
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};
177
178     # Get po from previous generation
179     my $po_previous = Locale::PO->load_file_ashash( $self->po_filename );
180
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 );
186     }
187
188     $self->save_po();
189 }
190
191
192 sub install {
193     my $self = shift;
194
195     unless ( -r $self->{path_lang} ) {
196         print "Koha directories hierarchy for ", $self->{lang}, " must be created first\n";
197         exit;
198     }
199
200     # Update the language .po file with last modified 'en' preferences
201     # and load it.
202     $self->update();
203
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;
212             }
213             while ( my ($section, $sysprefs) = each %$tab_content ) {
214                 $self->update_prefs( $pref, $sysprefs );
215             }
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};
221             }
222             $pref->{$tab} = $ntab;
223         }
224         my $file_trans = $self->{path_lang} . "/$file";
225         print "Write $file\n";
226         open my $fh, ">", $file_trans;
227         print $fh Dump($pref);
228     }
229 }
230
231
232 package Main;
233
234 use strict;
235 use warnings;
236
237 use Pod::Usage;
238
239 sub usage {
240     pod2usage( -verbose => 2 );
241     exit;
242 }
243
244
245 usage() if $#ARGV != 1;
246
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;
253 }
254 else {
255     usage();
256 }
257
258
259
260 =head1 NAME
261
262 pref-trans - Handle preferences translation
263
264 =head1 SYNOPSYS
265
266   pref-trans init fr-FR
267   pref-trans update fr-FR
268   pref-trans install fr-FR
269
270 =head1 USAGE
271
272 =over
273
274 =item pref-trans init F<lang>
275
276 Create a .po file in po directory, named F<lang>-pref.po. This file contains
277 text to translate extracted from .pref files.
278
279 =item pref-trans update F<lang>
280
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.
285
286 =item pref-trans install F<lang>
287
288 Use F<lang>-pref.po file to translate the english version of preferences files
289 and copy those files in the appropriate directory.
290
291 =back
292
293 =head1 DESCRIPTION
294
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.
301
302 =head1 COPYRIGHT AND LICENSE
303
304 Copyright 2010 by Tamil, s.a.r.l.
305
306 L<http://www.tamil.fr>
307
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.
310
311 =cut
312