Bug 7607: (follow-up) Address OPAC and limits
[koha.git] / misc / translator / xgettext-installer
blob79be95798ca5f9bbb49442004d18cebeac1bc2f3
1 #!/usr/bin/perl
3 # This file is part of Koha.
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.
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.
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>.
18 =head1 NAME
20 xgettext-installer - extract translatable strings from installer YAML files
22 =head1 SYNOPSIS
24 xgettext-installer [OPTION] [INPUTFILE]...
26 =head1 OPTIONS
28 =over
30 =item B<-f, --files-from=FILE>
32 get list of input files from FILE
34 =item B<-o, --output=FILE>
36 write output to the specified file
38 =item B<-h, --help>
40 display this help and exit
42 =back
44 =cut
46 use Modern::Perl;
48 use Getopt::Long;
49 use Locale::PO;
50 use Pod::Usage;
51 use YAML::Syck qw(LoadFile);
53 $YAML::Syck::ImplicitTyping = 1;
55 my $output = 'messages.pot';
56 my $files_from;
57 my $help;
59 GetOptions(
60 'output=s' => \$output,
61 'files-from=s' => \$files_from,
62 'help' => \$help,
63 ) or pod2usage(-verbose => 1, -exitval => 2);
65 if ($help) {
66 pod2usage(-verbose => 1, -exitval => 0);
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;
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"
91 for my $file (@files) {
92 my $yaml = LoadFile($file);
93 my @tables = @{ $yaml->{'tables'} };
95 my $tablec = 0;
96 for my $table (@tables) {
97 $tablec++;
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'} };
104 my $rowc = 0;
105 for my $row (@rows) {
106 $rowc++;
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++;
115 # discard pure html, TT, empty
116 next if ( $line =~ /^(\s*<.*?>\s*$|^\s*\[.*?\]\s*|\s*)$/ );
118 # put placeholders
119 $line =~ s/(<<.*?>>|\[\%.*?\%\]|<.*?>)/\%s/g;
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"
129 $pot->{$line} = $msg;
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"
138 $pot->{ $row->{$field} } = $msg;
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"
153 $pot->{$description} = $msg;
158 Locale::PO->save_file_fromhash($output, $pot);