2 # BEGIN BPS TAGGED BLOCK {{{
6 # This software is Copyright (c) 1996-2013 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 }}}
57 shrink => { 'update!' => 1, 'keep=s@' => [] },
58 clean => { 'update!' => 1 },
59 rosetta => { 'boundary=i' => 20 },
64 usage() unless $command;
65 usage("Unknown command '$command'")
66 unless $commands{ $command };
68 my $opt = $commands{ $command };
70 if ( $opt && keys %$opt ) {
71 while ( my ($k, $v) = each %$opt ) {
72 my ($target) = ($k =~ /^(.*?)(?:[:!+=|]|$)/);
75 GetOptions( \%opt, keys %$opt );
78 { no strict 'refs'; &$command( \%opt, @ARGV ); }
83 my %opt = %{ shift() };
84 my $dir = shift || 'lib/RT/I18N';
89 use constant TRANSLATED => 0;
90 use constant DISTINCT => 1;
92 foreach my $po_file (<$dir/*.po>) {
93 my $array = Locale::PO->load_file_asarray( $po_file );
95 $res{$po_file} = [0, 0];
98 foreach my $entry ( splice @$array, 1 ) {
99 next if $entry->reference && $entry->reference =~ /NOT FOUND IN SOURCE/;
101 next unless $entry->dequote( $entry->msgstr );
102 $res{$po_file}[TRANSLATED]++;
103 next if $entry->msgstr eq $entry->msgid;
104 $res{$po_file}[DISTINCT]++;
106 $max = $size if $max < $size;
109 my $legend = "<file>: <translated>[(<distinct>)]/<size> (<%>)";
111 print "\n$legend\n\n";
113 foreach my $po_file ( sort { $res{$b}[TRANSLATED] <=> $res{$a}[TRANSLATED] } keys %res ) {
114 my ($tr, $dist) = @{ $res{$po_file} };
115 my $perc = int($tr*1000/$max)/10;
116 if ( $tr == $dist ) {
117 printf "%s:\t%d/%d\t(%.1f%%)\n", $po_file, $tr, $max, $perc;
119 printf "%s:\t%d(%d)/%d\t(%.1f%%)\n", $po_file, $tr, $dist, $max, $perc;
127 my %opt = %{ shift() };
128 my $dir = shift || 'lib/RT/I18N';
130 my %keep = map { $_ => 1 } @{ $opt{'keep'} };
134 foreach my $po_file (<$dir/*.po>) {
135 my $array = Locale::PO->load_file_asarray( $po_file );
136 $stats{ $po_file } = { };
137 foreach my $entry ( splice @$array, 1 ) {
138 if ( !$keep{'not-referenced'} && $entry->reference && $entry->reference =~ /NOT FOUND IN SOURCE/ ) {
139 $stats{ $po_file }{'not-referenced'}++;
142 elsif ( !$keep{'not-translated'} && !$entry->dequote( $entry->msgstr ) ) {
143 $stats{ $po_file }{'not-translated'}++;
146 elsif ( !$keep{'equal'} && $entry->msgstr eq $entry->msgid ) {
147 $stats{ $po_file }{'equal'}++;
150 push @$array, $entry;
152 $stats{ $po_file }{'total'} += $_ for values %{ $stats{ $po_file } };
153 Locale::PO->save_file_fromarray($po_file, $array) if $opt{'update'};
156 my $legend = "<file>: <total> (<details>)";
157 print "\n$legend\n\n";
159 foreach my $po_file ( sort { $stats{$a}{'total'} <=> $stats{$b}{'total'} } keys %stats ) {
160 my $res = sprintf "%s:\t%d ", $po_file, $stats{ $po_file }{'total'};
162 foreach ( qw(not-referenced not-translated equal) ) {
163 next unless my $v = $stats{ $po_file }{ $_ };
167 $res .= " (". join( ', ', @tmp ) .")";
169 elsif ( @tmp == 1 ) {
170 $res .= " (". (split /:/, $tmp[0])[0] .")";
179 my %opt = %{ shift() };
180 $opt{'keep'} = [qw(not-translated equal)];
181 return shrink( \%opt, @_ );
185 my %opt = %{ shift() };
186 my $url = shift or die 'must provide rosseta download url or directory with new po files';
189 if ( $url =~ m/^[a-z]+:\/\// ) {
191 $dir = File::Temp::tempdir();
192 my ($fname) = $url =~ m{([^/]+)$};
194 print "Downloading $url\n";
196 LWP::Simple::getstore($url => "$dir/$fname");
198 print "Extracting $dir/$fname\n";
199 require Archive::Extract;
200 my $ae = Archive::Extract->new(archive => "$dir/$fname");
201 my $ok = $ae->extract( to => $dir );
203 elsif ( -e $url && -d _ ) {
207 die "Is not URL or directory: '$url'";
210 my @files = <$dir/rt/*.po>, <$dir/*.po>;
212 print STDERR "No files in $dir/rt/*.po and $dir/*.po\n";
216 require Locale::Maketext::Extract;
217 Locale::Maketext::Lexicon::set_option('use_fuzzy', 1);
218 Locale::Maketext::Lexicon::set_option('allow_empty', 1);
223 my ($lang) = m/([\w_]+)\.po/;
224 my $fn_orig = "lib/RT/I18N/$lang.po";
226 print "$_ -> $fn_orig\n";
228 # retain the "NOT FOUND IN SOURCE" entries
230 my $tmp = File::Temp->new;
231 system("sed -e 's/^#~ //' $_ > $tmp");
232 my $ext = Locale::Maketext::Extract->new;
235 my $po_orig = Locale::PO->load_file_ashash( -e $fn_orig? $fn_orig : 'lib/RT/I18N/rt.pot' );
236 # don't want empty vales to override ours.
237 # don't want fuzzy flag as when uploading to rosetta again it's not accepted by rosetta.
238 foreach my $msgid ($ext->msgids) {
239 my $entry = $po_orig->{Locale::PO->quote($msgid)} or next;
240 my $msgstr = $entry->dequote($entry->{msgstr}) or next;
241 $ext->set_msgstr($msgid, $msgstr)
242 if $ext->msgstr($msgid) eq '' && $msgstr;
244 if ( $opt{'boundary'} && $lang !~ /^en(_[A-Z]{2})?$/ ) { # en[_**] are exceptional
245 my @ids = $ext->msgids;
247 foreach my $id ( @ids ) {
248 next unless $ext->msgstr( $id );
249 next if $ext->msgstr( $id ) eq $id;
252 my $perc = int($translated/@ids * 100 + 0.5);
253 if ( $perc < $opt{'boundary'} ) {
254 print "Only $perc% translated for '$lang' when $opt{'boundary'}% required.\n";
255 print "Deleting '$fn_orig'...\n";
260 $ext->write_po($fn_orig);
267 system($^X, 'sbin/extract-message-catalog', @_);