rt 4.2.16
[freeside.git] / rt / devel / tools / extract-message-catalog
1 #!/usr/bin/env perl
2 # BEGIN BPS TAGGED BLOCK {{{
3 #
4 # COPYRIGHT:
5 #
6 # This software is Copyright (c) 1996-2019 Best Practical Solutions, LLC
7 #                                          <sales@bestpractical.com>
8 #
9 # (Except where explicitly superseded by other copyright notices)
10 #
11 #
12 # LICENSE:
13 #
14 # This work is made available to you under the terms of Version 2 of
15 # the GNU General Public License. A copy of that license should have
16 # been provided with this software, but in any event can be snarfed
17 # from www.gnu.org.
18 #
19 # This work is distributed in the hope that it will be useful, but
20 # WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22 # General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 # 02110-1301 or visit their web page on the internet at
28 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
29 #
30 #
31 # CONTRIBUTION SUBMISSION POLICY:
32 #
33 # (The following paragraph is not intended to limit the rights granted
34 # to you to modify and distribute this software under the terms of
35 # the GNU General Public License and is only of importance to you if
36 # you choose to contribute your changes and enhancements to the
37 # community by submitting them to Best Practical Solutions, LLC.)
38 #
39 # By intentionally submitting any modifications, corrections or
40 # derivatives to this work, or any other work intended for use with
41 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
42 # you are the copyright holder for those contributions and you grant
43 # Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
44 # royalty-free, perpetual, license to use, copy, create derivative
45 # works based on those contributions, and sublicense and distribute
46 # those contributions and any derivatives thereof.
47 #
48 # END BPS TAGGED BLOCK }}}
49 # Portions Copyright 2002 Autrijus Tang <autrijus@autrijus.org>
50
51 use strict;
52 use warnings;
53
54 use open qw/ :std :encoding(UTF-8) /;
55
56 use Locale::PO;
57
58 use lib 'lib';
59 use RT::I18N::Extract;
60
61 $| = 1;
62
63 # po dir is for extensions
64 @ARGV = (<share/po/*.po>, <share/po/*.pot>, <po/*.po>, <po/*.pot>) unless @ARGV;
65
66 # extract all strings and stuff them into %POT
67 # scan html dir for extensions
68 my $extract = RT::I18N::Extract->new;
69 our %POT = $extract->all;
70
71 print "$_\n" for $extract->errors;
72
73 # update all language dictionaries
74 foreach my $dict (@ARGV) {
75     $dict = "share/po/$dict.pot" if ( $dict eq 'rt' );
76     $dict = "share/po/$dict.po" unless -f $dict or $dict =~ m!/!;
77
78     my $lang = $dict;
79     $lang =~ s|.*/||;
80     $lang =~ s|\.po$||;
81     $lang =~ s|\.pot$||;
82
83     update($lang, $dict);
84 }
85
86
87 sub uniq {
88     my %seen;
89     return grep { !$seen{$_}++ } @_;
90 }
91
92 sub update {
93     my $lang = shift;
94     my $file = shift;
95
96     unless (!-e $file or -w $file) {
97         warn "Can't write to $lang, skipping...\n";
98         return;
99     }
100
101     my $is_english = ( $lang =~ /^en(?:[^A-Za-z]|$)/ );
102
103     print "Updating $lang";
104     my $lexicon = Locale::PO->load_file_ashash( $file, "utf-8" );
105
106     # Default to the empty string for new ones
107     $lexicon->{$_->msgid} ||= $_
108         for values %POT;
109
110     my $errors = 0;
111     for my $msgid ( keys %{$lexicon} ) {
112         my $entry = $lexicon->{$msgid};
113
114         # Don't output empty translations for english
115         if (not length $entry->dequote($entry->msgstr) and $is_english) {
116             delete $lexicon->{$msgid};
117             next;
118         }
119
120         # The PO properties at the top are always fine to leave as-is
121         next if not length $entry->dequote($msgid);
122
123         # Not found in source?  Drop it
124         my $source = $POT{$msgid};
125         if (not $source) {
126             delete $lexicon->{$msgid};
127             next;
128         }
129
130         # Pull in the properties from the source
131         $entry->reference( $source->reference );
132         $entry->automatic( $source->automatic );
133
134         my $fail = validate_msgstr($lang,
135                                    map {$entry->dequote($_)}
136                                        $entry->msgid, $entry->msgstr);
137         next unless $fail;
138         print "\n" unless $errors++;
139         print $fail;
140     }
141
142     my @order = map {$_->[0]}
143                 sort {$a->[1] cmp $b->[1]}
144                 map {[$_, $_->dequote($_->msgid)]}
145                 values %{$lexicon};
146
147     Locale::PO->save_file_fromarray($file, \@order, "utf-8")
148           or die "Couldn't update '$file': $!";
149
150     if ($errors) {
151         print "\n";
152     } else {
153         print "\r", " "x100, "\r";
154     }
155     return 1;
156 }
157
158 sub validate_msgstr {
159     my $lang   = shift;
160     my $msgid  = shift;
161     my $msgstr = shift;
162
163     return if not defined $msgstr or $msgstr eq ''; # no translation for this string
164
165     # we uniq because a string can use a placeholder more than once
166     # (eg %1 %quant(%1, ...) like in our czech localization
167     my @expected_variables = uniq($msgid =~ /%\d+/g);
168     my @got_variables = uniq($msgstr =~ /%\d+/g);
169
170     # this catches the case where expected uses %1,%2 and got uses %1,%3
171     # unlike a simple @expected_variables == @got_variables
172     my $expected = join ", ", sort @expected_variables;
173     my $got      = join ", ", sort @got_variables;
174     return if $expected eq $got;
175
176     return "  expected (" . $expected . ") in msgid: $msgid\n" .
177            "       got (" . $got      . ") in msgstr: $msgstr\n";
178 }