Bug 25067: Move PO file manipulation code into gulp tasks
[koha-ffzg.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::Syck qw(LoadFile);
53
54 $YAML::Syck::ImplicitTyping = 1;
55
56 my $output = 'messages.pot';
57 my $files_from;
58 my $help;
59
60 GetOptions(
61     'output=s' => \$output,
62     'files-from=s' => \$files_from,
63     'help' => \$help,
64 ) or pod2usage(-verbose => 1, -exitval => 2);
65
66 if ($help) {
67     pod2usage(-verbose => 1, -exitval => 0);
68 }
69
70 my @files = @ARGV;
71 if ($files_from) {
72     open(my $fh, '<', $files_from) or die "Cannot open $files_from: $!";
73     push @files, <$fh>;
74     chomp @files;
75     close $fh;
76 }
77
78 my $pot = {
79     '' => Locale::PO->new(
80         -msgid  => '',
81         -msgstr => "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 $pref = LoadFile($file);
93     while ( my ($tab, $tab_content) = each %$pref ) {
94         add_po(undef, basename($file));
95
96         if ( ref($tab_content) eq 'ARRAY' ) {
97             add_prefs( $file, $tab, $tab_content );
98         } else {
99             while ( my ($section, $sysprefs) = each %$tab_content ) {
100                 my $context = "$tab > $section";
101                 my $msgid = sprintf('%s %s', basename($file), $section);
102                 add_po($tab, $msgid);
103                 add_prefs( $file, $context, $sysprefs );
104             }
105         }
106     }
107 }
108
109 Locale::PO->save_file_fromhash($output, $pot);
110
111 sub add_prefs {
112     my ($file, $context, $prefs) = @_;
113
114     for my $pref (@$prefs) {
115         my $pref_name = '';
116         for my $element (@$pref) {
117             if ( ref($element) eq 'HASH' ) {
118                 $pref_name = $element->{pref};
119                 last;
120             }
121         }
122         for my $element (@$pref) {
123             if ( ref($element) eq 'HASH' ) {
124                 while ( my ( $key, $value ) = each(%$element) ) {
125                     next unless $key eq 'choices' or $key eq 'multiple';
126                     next unless ref($value) eq 'HASH';
127                     for my $ckey ( keys %$value ) {
128                         my $msgid = sprintf('%s#%s# %s', basename($file), $pref_name, $value->{$ckey});
129                         add_po( "$context > $pref_name", $msgid );
130                     }
131                 }
132             }
133             elsif ($element) {
134                 my $msgid = sprintf('%s#%s# %s', basename($file), $pref_name, $element);
135                 add_po( "$context > $pref_name", $msgid );
136             }
137         }
138     }
139 }
140
141 sub add_po {
142     my ($comment, $msgid ) = @_;
143
144     return unless $msgid;
145
146     $pot->{$msgid} = Locale::PO->new(
147         -comment   => $comment,
148         -msgid     => $msgid,
149         -msgstr    => '',
150     );
151 }