Bug 24000: Some modules do not return 1
[koha.git] / misc / translator / xgettext-pref
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 =head1 NAME
19
20 xgettext-pref - extract translatable strings from system preferences YAML files
21
22 =head1 SYNOPSIS
23
24 xgettext-pref [OPTION] [INPUTFILE]...
25
26 =head1 OPTIONS
27
28 =over
29
30 =item B<-f, --files-from=FILE>
31
32 get list of input files from FILE
33
34 =item B<-o, --output=FILE>
35
36 write output to the specified file
37
38 =item B<-h, --help>
39
40 display this help and exit
41
42 =back
43
44 =cut
45
46 use Modern::Perl;
47
48 use File::Basename;
49 use Getopt::Long;
50 use Locale::PO;
51 use Pod::Usage;
52 use YAML::XS;
53
54 my $output = 'messages.pot';
55 my $files_from;
56 my $help;
57
58 GetOptions(
59     'output=s' => \$output,
60     'files-from=s' => \$files_from,
61     'help' => \$help,
62 ) or pod2usage(-verbose => 1, -exitval => 2);
63
64 if ($help) {
65     pod2usage(-verbose => 1, -exitval => 0);
66 }
67
68 my @files = @ARGV;
69 if ($files_from) {
70     open(my $fh, '<', $files_from) or die "Cannot open $files_from: $!";
71     push @files, <$fh>;
72     chomp @files;
73     close $fh;
74 }
75
76 my $pot = {
77     '' => Locale::PO->new(
78         -msgid  => '',
79         -msgstr => "Project-Id-Version: Koha\n"
80           . "PO-Revision-Date: YEAR-MO-DA HO:MI +ZONE\n"
81           . "Last-Translator: FULL NAME <EMAIL\@ADDRESS>\n"
82           . "Language-Team: Koha Translate List <koha-translate\@lists.koha-community.org>\n"
83           . "MIME-Version: 1.0\n"
84           . "Content-Type: text/plain; charset=UTF-8\n"
85           . "Content-Transfer-Encoding: 8bit\n"
86     ),
87 };
88
89 for my $file (@files) {
90     my $pref = YAML::XS::LoadFile($file);
91     while ( my ($tab, $tab_content) = each %$pref ) {
92         add_po(undef, basename($file));
93
94         if ( ref($tab_content) eq 'ARRAY' ) {
95             add_prefs( $file, $tab, $tab_content );
96         } else {
97             while ( my ($section, $sysprefs) = each %$tab_content ) {
98                 my $context = "$tab > $section";
99                 my $msgid = sprintf('%s %s', basename($file), $section);
100                 add_po($tab, $msgid);
101                 add_prefs( $file, $context, $sysprefs );
102             }
103         }
104     }
105 }
106
107 Locale::PO->save_file_fromhash($output, $pot);
108
109 sub add_prefs {
110     my ($file, $context, $prefs) = @_;
111
112     for my $pref (@$prefs) {
113         my $pref_name = '';
114         for my $element (@$pref) {
115             if ( ref($element) eq 'HASH' ) {
116                 $pref_name = $element->{pref};
117                 last;
118             }
119         }
120         for my $element (@$pref) {
121             if ( ref($element) eq 'HASH' ) {
122                 while ( my ( $key, $value ) = each(%$element) ) {
123                     next unless $key eq 'choices' or $key eq 'multiple';
124                     next unless ref($value) eq 'HASH';
125                     for my $ckey ( keys %$value ) {
126                         my $msgid = sprintf('%s#%s# %s', basename($file), $pref_name, $value->{$ckey});
127                         add_po( "$context > $pref_name", $msgid );
128                     }
129                 }
130             }
131             elsif ($element) {
132                 my $msgid = sprintf('%s#%s# %s', basename($file), $pref_name, $element);
133                 add_po( "$context > $pref_name", $msgid );
134             }
135         }
136     }
137 }
138
139 sub add_po {
140     my ($comment, $msgid ) = @_;
141
142     return unless $msgid;
143
144     $pot->{$msgid} = Locale::PO->new(
145         -comment   => $comment,
146         -msgid     => $msgid,
147         -msgstr    => '',
148     );
149 }