2 # BEGIN BPS TAGGED BLOCK {{{
6 # This software is Copyright (c) 1996-2014 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 }}}
54 use File::Temp 'tempdir';
56 use constant PO_DIR => 'share/po';
60 shrink => { 'update!' => 1, 'keep=s@' => [] },
61 clean => { 'update!' => 1 },
62 rosetta => { 'boundary=i' => 20 },
67 usage() unless $command;
68 usage("Unknown command '$command'")
69 unless $commands{ $command };
71 my $opt = $commands{ $command };
73 if ( $opt && keys %$opt ) {
74 while ( my ($k, $v) = each %$opt ) {
75 my ($target) = ($k =~ /^(.*?)(?:[:!+=|]|$)/);
78 GetOptions( \%opt, keys %$opt );
81 { no strict 'refs'; &$command( \%opt, @ARGV ); }
86 my %opt = %{ shift() };
87 my $dir = shift || PO_DIR;
92 use constant TRANSLATED => 0;
93 use constant DISTINCT => 1;
95 foreach my $po_file (<$dir/*.po>) {
96 my $array = Locale::PO->load_file_asarray( $po_file );
98 $res{$po_file} = [0, 0];
101 foreach my $entry ( splice @$array, 1 ) {
102 next if $entry->reference && $entry->reference =~ /NOT FOUND IN SOURCE/;
104 next unless $entry->dequote( $entry->msgstr );
105 $res{$po_file}[TRANSLATED]++;
106 next if $entry->msgstr eq $entry->msgid;
107 $res{$po_file}[DISTINCT]++;
109 $max = $size if $max < $size;
112 my $legend = "<file>: <translated>[(<distinct>)]/<size> (<%>)";
114 print "\n$legend\n\n";
116 foreach my $po_file ( sort { $res{$b}[TRANSLATED] <=> $res{$a}[TRANSLATED] } keys %res ) {
117 my ($tr, $dist) = @{ $res{$po_file} };
118 my $perc = int($tr*1000/$max)/10;
119 if ( $tr == $dist ) {
120 printf "%s:\t%d/%d\t(%.1f%%)\n", $po_file, $tr, $max, $perc;
122 printf "%s:\t%d(%d)/%d\t(%.1f%%)\n", $po_file, $tr, $dist, $max, $perc;
130 my %opt = %{ shift() };
131 my $dir = shift || PO_DIR;
133 my %keep = map { $_ => 1 } @{ $opt{'keep'} };
137 foreach my $po_file (<$dir/*.po>) {
138 my $array = Locale::PO->load_file_asarray( $po_file );
139 $stats{ $po_file } = { };
140 foreach my $entry ( splice @$array, 1 ) {
141 if ( !$keep{'not-referenced'} && $entry->reference && $entry->reference =~ /NOT FOUND IN SOURCE/ ) {
142 $stats{ $po_file }{'not-referenced'}++;
145 elsif ( !$keep{'not-translated'} && !$entry->dequote( $entry->msgstr ) ) {
146 $stats{ $po_file }{'not-translated'}++;
149 elsif ( !$keep{'equal'} && $entry->msgstr eq $entry->msgid ) {
150 $stats{ $po_file }{'equal'}++;
153 push @$array, $entry;
155 $stats{ $po_file }{'total'} += $_ for values %{ $stats{ $po_file } };
156 Locale::PO->save_file_fromarray($po_file, $array) if $opt{'update'};
159 my $legend = "<file>: <total> (<details>)";
160 print "\n$legend\n\n";
162 foreach my $po_file ( sort { $stats{$a}{'total'} <=> $stats{$b}{'total'} } keys %stats ) {
163 my $res = sprintf "%s:\t%d ", $po_file, $stats{ $po_file }{'total'};
165 foreach ( qw(not-referenced not-translated equal) ) {
166 next unless my $v = $stats{ $po_file }{ $_ };
170 $res .= " (". join( ', ', @tmp ) .")";
172 elsif ( @tmp == 1 ) {
173 $res .= " (". (split /:/, $tmp[0])[0] .")";
182 my %opt = %{ shift() };
183 $opt{'keep'} = [qw(not-translated equal)];
184 return shrink( \%opt, @_ );
188 my %opt = %{ shift() };
189 my $url = shift or die 'must provide Rosetta download url or directory with new po files';
192 if ( $url =~ m{^[a-z]+://} ) {
194 my ($fname) = $url =~ m{([^/]+)$};
196 print "Downloading $url\n";
198 LWP::Simple::getstore($url => "$dir/$fname");
200 print "Extracting $dir/$fname\n";
201 require Archive::Extract;
202 my $ae = Archive::Extract->new(archive => "$dir/$fname");
203 my $ok = $ae->extract( to => $dir );
205 elsif ( -e $url && -d _ ) {
209 die "Is not URL or directory: '$url'";
212 my @files = ( <$dir/rt/*.po>, <$dir/*.po> );
214 print STDERR "No files in $dir/rt/*.po and $dir/*.po\n";
218 require Locale::Maketext::Extract;
219 Locale::Maketext::Lexicon::set_option('use_fuzzy', 1);
220 Locale::Maketext::Lexicon::set_option('allow_empty', 1);
225 my ($lang) = m/([\w_]+)\.po/;
226 my $fn_orig = PO_DIR . "/$lang.po";
228 print "$_ -> $fn_orig\n";
230 # retain the "NOT FOUND IN SOURCE" entries
231 my $tmp = File::Temp->new;
232 system("sed -e 's/^#~ //' $_ > $tmp");
233 my $ext = Locale::Maketext::Extract->new;
236 my $po_orig = Locale::PO->load_file_ashash( -e $fn_orig? $fn_orig : PO_DIR . '/rt.pot' );
237 # don't want empty vales to override ours.
238 # don't want fuzzy flag as when uploading to rosetta again it's not accepted by rosetta.
239 foreach my $msgid ($ext->msgids) {
240 my $entry = $po_orig->{Locale::PO->quote($msgid)} or next;
241 my $msgstr = $entry->dequote($entry->{msgstr}) or next;
242 $ext->set_msgstr($msgid, $msgstr)
243 if $ext->msgstr($msgid) eq '' && $msgstr;
245 if ( $opt{'boundary'} && $lang !~ /^en(_[A-Z]{2})?$/ ) { # en[_**] are exceptional
246 my @ids = $ext->msgids;
248 foreach my $id ( @ids ) {
249 next unless $ext->msgstr( $id );
250 next if $ext->msgstr( $id ) eq $id;
253 my $perc = int($translated/@ids * 100 + 0.5);
254 if ( $perc < $opt{'boundary'} ) {
255 print "Only $perc% translated for '$lang' when $opt{'boundary'}% required.\n";
256 print "Deleting '$fn_orig'...\n";
261 $ext->write_po($fn_orig);
268 system($^X, 'devel/tools/extract-message-catalog', @_);