2 # BEGIN BPS TAGGED BLOCK {{{
6 # This software is Copyright (c) 1996-2018 Best Practical Solutions, LLC
7 # <sales@bestpractical.com>
9 # (Except where explicitly superseded by other copyright notices)
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
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.
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.
31 # CONTRIBUTION SUBMISSION POLICY:
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.)
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.
48 # END BPS TAGGED BLOCK }}}
49 # Portions Copyright 2002 Autrijus Tang <autrijus@autrijus.org>
54 use open qw/ :std :encoding(UTF-8) /;
59 use RT::I18N::Extract;
63 # po dir is for extensions
64 @ARGV = (<share/po/*.po>, <share/po/*.pot>, <po/*.po>, <po/*.pot>) unless @ARGV;
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;
71 print "$_\n" for $extract->errors;
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!/!;
89 return grep { !$seen{$_}++ } @_;
96 unless (!-e $file or -w $file) {
97 warn "Can't write to $lang, skipping...\n";
101 my $is_english = ( $lang =~ /^en(?:[^A-Za-z]|$)/ );
103 print "Updating $lang";
104 my $lexicon = Locale::PO->load_file_ashash( $file, "utf-8" );
106 # Default to the empty string for new ones
107 $lexicon->{$_->msgid} ||= $_
111 for my $msgid ( keys %{$lexicon} ) {
112 my $entry = $lexicon->{$msgid};
114 # Don't output empty translations for english
115 if (not length $entry->dequote($entry->msgstr) and $is_english) {
116 delete $lexicon->{$msgid};
120 # The PO properties at the top are always fine to leave as-is
121 next if not length $entry->dequote($msgid);
123 # Not found in source? Drop it
124 my $source = $POT{$msgid};
126 delete $lexicon->{$msgid};
130 # Pull in the properties from the source
131 $entry->reference( $source->reference );
132 $entry->automatic( $source->automatic );
134 my $fail = validate_msgstr($lang,
135 map {$entry->dequote($_)}
136 $entry->msgid, $entry->msgstr);
138 print "\n" unless $errors++;
142 my @order = map {$_->[0]}
143 sort {$a->[1] cmp $b->[1]}
144 map {[$_, $_->dequote($_->msgid)]}
147 Locale::PO->save_file_fromarray($file, \@order, "utf-8")
148 or die "Couldn't update '$file': $!";
153 print "\r", " "x100, "\r";
158 sub validate_msgstr {
163 return if not defined $msgstr or $msgstr eq ''; # no translation for this string
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);
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;
176 return " expected (" . $expected . ") in msgid: $msgid\n" .
177 " got (" . $got . ") in msgstr: $msgstr\n";