Bug 24083: Add support for unseen_renewals
[koha.git] / misc / translator / xgettext-installer
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-installer - extract translatable strings from installer YAML files
21
22 =head1 SYNOPSIS
23
24 xgettext-installer [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 Getopt::Long;
49 use Locale::PO;
50 use Pod::Usage;
51 use YAML::Syck qw(LoadFile);
52
53 $YAML::Syck::ImplicitTyping = 1;
54
55 my $output = 'messages.pot';
56 my $files_from;
57 my $help;
58
59 GetOptions(
60     'output=s' => \$output,
61     'files-from=s' => \$files_from,
62     'help' => \$help,
63 ) or pod2usage(-verbose => 1, -exitval => 2);
64
65 if ($help) {
66     pod2usage(-verbose => 1, -exitval => 0);
67 }
68
69 my @files = @ARGV;
70 if ($files_from) {
71     open(my $fh, '<', $files_from) or die "Cannot open $files_from: $!";
72     push @files, <$fh>;
73     chomp @files;
74     close $fh;
75 }
76
77 my $pot = {
78     '' => Locale::PO->new(
79         -msgid  => '',
80         -msgstr =>
81             "Project-Id-Version: Koha\n"
82           . "PO-Revision-Date: YEAR-MO-DA HO:MI +ZONE\n"
83           . "Last-Translator: FULL NAME <EMAIL\@ADDRESS>\n"
84           . "Language-Team: Koha Translate List <koha-translate\@lists.koha-community.org>\n"
85           . "MIME-Version: 1.0\n"
86           . "Content-Type: text/plain; charset=UTF-8\n"
87           . "Content-Transfer-Encoding: 8bit\n"
88     ),
89 };
90
91 for my $file (@files) {
92     my $yaml = LoadFile($file);
93     my @tables = @{ $yaml->{'tables'} };
94
95     my $tablec = 0;
96     for my $table (@tables) {
97         $tablec++;
98
99         my $table_name = ( keys %$table )[0];
100         my @translatable = @{ $table->{$table_name}->{translatable} };
101         my @rows = @{ $table->{$table_name}->{rows} };
102         my @multiline = @{ $table->{$table_name}->{'multiline'} };
103
104         my $rowc = 0;
105         for my $row (@rows) {
106             $rowc++;
107
108             for my $field (@translatable) {
109                 if ( @multiline and grep { $_ eq $field } @multiline ) {
110                     # multiline fields, only notices ATM
111                     my $mulc;
112                     foreach my $line ( @{ $row->{$field} } ) {
113                         $mulc++;
114
115                         # discard pure html, TT, empty
116                         next if ( $line =~ /^(\s*<.*?>\s*$|^\s*\[.*?\]\s*|\s*)$/ );
117
118                         # put placeholders
119                         $line =~ s/(<<.*?>>|\[\%.*?\%\]|<.*?>)/\%s/g;
120
121                         # discard non strings
122                         next if ( $line =~ /^(\s|%s|-|[[:punct:]]|\(|\))*$/ or length($line) < 2 );
123                         if ( not $pot->{$line} ) {
124                             my $msg = Locale::PO->new(
125                                 -msgid  => $line,
126                                 -msgstr => '',
127                                 -reference => "$file:$table_name:$tablec:row:$rowc:mul:$mulc"
128                             );
129                             $pot->{$line} = $msg;
130                         }
131                     }
132                 } elsif (defined $row->{$field} && length($row->{$field}) > 1 && !$pot->{ $row->{$field} }) {
133                     my $msg = Locale::PO->new(
134                         -msgid     => $row->{$field},
135                         -msgstr    => '',
136                         -reference => "$file:$table_name:$tablec:row:$rowc"
137                     );
138                     $pot->{ $row->{$field} } = $msg;
139                 }
140             }
141         }
142     }
143
144     my $desccount = 0;
145     for my $description ( @{ $yaml->{'description'} } ) {
146         $desccount++;
147         if ( length($description) > 1 and not $pot->{$description} ) {
148             my $msg = Locale::PO->new(
149                 -msgid     => $description,
150                 -msgstr    => '',
151                 -reference => "$file:description:$desccount"
152             );
153             $pot->{$description} = $msg;
154         }
155     }
156 }
157
158 Locale::PO->save_file_fromhash($output, $pot);