Bug 28028: Remove misc/migration_tools/fix_onloan.pl
[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::XS;
52
53 my $output = 'messages.pot';
54 my $files_from;
55 my $help;
56
57 GetOptions(
58     'output=s' => \$output,
59     'files-from=s' => \$files_from,
60     'help' => \$help,
61 ) or pod2usage(-verbose => 1, -exitval => 2);
62
63 if ($help) {
64     pod2usage(-verbose => 1, -exitval => 0);
65 }
66
67 my @files = @ARGV;
68 if ($files_from) {
69     open(my $fh, '<', $files_from) or die "Cannot open $files_from: $!";
70     push @files, <$fh>;
71     chomp @files;
72     close $fh;
73 }
74
75 my $pot = {
76     '' => Locale::PO->new(
77         -msgid  => '',
78         -msgstr =>
79             "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 $yaml = YAML::XS::LoadFile($file);
91     my @tables = @{ $yaml->{'tables'} };
92
93     my $tablec = 0;
94     for my $table (@tables) {
95         $tablec++;
96
97         my $table_name = ( keys %$table )[0];
98         my @translatable = @{ $table->{$table_name}->{translatable} };
99         my @rows = @{ $table->{$table_name}->{rows} };
100         my @multiline = @{ $table->{$table_name}->{'multiline'} };
101
102         my $rowc = 0;
103         for my $row (@rows) {
104             $rowc++;
105
106             for my $field (@translatable) {
107                 if ( @multiline and grep { $_ eq $field } @multiline ) {
108                     # multiline fields, only notices ATM
109                     my $mulc;
110                     foreach my $line ( @{ $row->{$field} } ) {
111                         $mulc++;
112
113                         # discard pure html, TT, empty
114                         next if ( $line =~ /^(\s*<.*?>\s*$|^\s*\[.*?\]\s*|\s*)$/ );
115
116                         # put placeholders
117                         $line =~ s/(<<.*?>>|\[\%.*?\%\]|<.*?>)/\%s/g;
118
119                         # discard non strings
120                         next if ( $line =~ /^(\s|%s|-|[[:punct:]]|\(|\))*$/ or length($line) < 2 );
121                         if ( not $pot->{$line} ) {
122                             my $msg = Locale::PO->new(
123                                 -msgid  => $line,
124                                 -msgstr => '',
125                                 -reference => "$file:$table_name:$tablec:row:$rowc:mul:$mulc"
126                             );
127                             $pot->{$line} = $msg;
128                         }
129                     }
130                 } elsif (defined $row->{$field} && length($row->{$field}) > 1 && !$pot->{ $row->{$field} }) {
131                     my $msg = Locale::PO->new(
132                         -msgid     => $row->{$field},
133                         -msgstr    => '',
134                         -reference => "$file:$table_name:$tablec:row:$rowc"
135                     );
136                     $pot->{ $row->{$field} } = $msg;
137                 }
138             }
139         }
140     }
141
142     my $desccount = 0;
143     for my $description ( @{ $yaml->{'description'} } ) {
144         $desccount++;
145         if ( length($description) > 1 and not $pot->{$description} ) {
146             my $msg = Locale::PO->new(
147                 -msgid     => $description,
148                 -msgstr    => '',
149                 -reference => "$file:description:$desccount"
150             );
151             $pot->{$description} = $msg;
152         }
153     }
154 }
155
156 Locale::PO->save_file_fromhash($output, $pot, 'utf8');