Bug 26854: Close STDERR when forking stage-marc-import.pl
[koha.git] / misc / translator / xgettext-pref
blob1113a27f950c98e4a22fe370ee696d9e07a72da3
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-pref - extract translatable strings from system preferences YAML files
22 =head1 SYNOPSIS
24 xgettext-pref [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 File::Basename;
49 use Getopt::Long;
50 use Locale::PO;
51 use Pod::Usage;
52 use YAML::Syck qw(LoadFile);
54 $YAML::Syck::ImplicitTyping = 1;
56 my $output = 'messages.pot';
57 my $files_from;
58 my $help;
60 GetOptions(
61 'output=s' => \$output,
62 'files-from=s' => \$files_from,
63 'help' => \$help,
64 ) or pod2usage(-verbose => 1, -exitval => 2);
66 if ($help) {
67 pod2usage(-verbose => 1, -exitval => 0);
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;
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"
91 for my $file (@files) {
92 my $pref = LoadFile($file);
93 while ( my ($tab, $tab_content) = each %$pref ) {
94 add_po(undef, basename($file));
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 );
109 Locale::PO->save_file_fromhash($output, $pot);
111 sub add_prefs {
112 my ($file, $context, $prefs) = @_;
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;
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 );
133 elsif ($element) {
134 my $msgid = sprintf('%s#%s# %s', basename($file), $pref_name, $element);
135 add_po( "$context > $pref_name", $msgid );
141 sub add_po {
142 my ($comment, $msgid ) = @_;
144 return unless $msgid;
146 $pot->{$msgid} = Locale::PO->new(
147 -comment => $comment,
148 -msgid => $msgid,
149 -msgstr => '',