broadband_nas export, #15284
[freeside.git] / rt / sbin / rt-message-catalog
1 #!/usr/bin/env perl 
2 # BEGIN BPS TAGGED BLOCK {{{
3 #
4 # COPYRIGHT:
5 #
6 # This software is Copyright (c) 1996-2011 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 use strict;
50 use warnings;
51
52 use Locale::PO;
53 use Getopt::Long;
54
55 my %commands = (
56     stats   => { },
57     shrink  => { 'update!' => 1, 'keep=s@' => [] },
58     clean   => { 'update!' => 1 },
59     rosetta => { 'boundary=i' => 20 },
60     extract => { },
61 );
62
63 my $command = shift;
64 usage() unless $command;
65 usage("Unknown command '$command'")
66     unless $commands{ $command };
67
68 my $opt = $commands{ $command };
69 my %opt = ();
70 if ( $opt && keys %$opt ) {
71     while ( my ($k, $v) = each %$opt ) {
72         my ($target) = ($k =~ /^(.*?)(?:[:!+=|]|$)/);
73         $opt{$target} = $v;
74     }
75     GetOptions( \%opt, keys %$opt );
76 }
77
78 { no strict 'refs'; &$command( \%opt, @ARGV ); }
79
80 exit;
81
82 sub stats {
83     my %opt = %{ shift() };
84     my $dir = shift || 'lib/RT/I18N';
85
86     my $max = 0;
87     my %res = ();
88
89     use constant TRANSLATED => 0;
90     use constant DISTINCT => 1;
91
92     foreach my $po_file (<$dir/*.po>) {
93         my $array = Locale::PO->load_file_asarray( $po_file );
94
95         $res{$po_file} = [0, 0];
96
97         my $size = 0;
98         foreach my $entry ( splice @$array, 1 ) {
99             next if $entry->reference && $entry->reference =~ /NOT FOUND IN SOURCE/;
100             $size++;
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]++;
105         }
106         $max = $size if $max < $size;
107     }
108
109     my $legend = "<file>: <translated>[(<distinct>)]/<size> (<%>)";
110
111     print "\n$legend\n\n";
112
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;
118         } else {
119             printf "%s:\t%d(%d)/%d\t(%.1f%%)\n", $po_file, $tr, $dist, $max, $perc;
120         }
121     }
122
123     print "\n$legend\n";
124 }
125
126 sub shrink {
127     my %opt = %{ shift() };
128     my $dir = shift || 'lib/RT/I18N';
129
130     my %keep = map { $_ => 1 } @{ $opt{'keep'} };
131
132     my %stats = ();
133
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'}++;
140                 next;
141             }
142             elsif ( !$keep{'not-translated'} && !$entry->dequote( $entry->msgstr ) ) {
143                 $stats{ $po_file }{'not-translated'}++;
144                 next;
145             }
146             elsif ( !$keep{'equal'} && $entry->msgstr eq $entry->msgid ) {
147                 $stats{ $po_file }{'equal'}++;
148                 next;
149             }
150             push @$array, $entry;
151         }
152         $stats{ $po_file }{'total'} += $_ for values %{ $stats{ $po_file } };
153         Locale::PO->save_file_fromarray($po_file, $array) if $opt{'update'};
154     }
155
156     my $legend = "<file>: <total> (<details>)";
157     print "\n$legend\n\n";
158
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'};
161         my @tmp;
162         foreach ( qw(not-referenced not-translated equal) ) {
163             next unless my $v = $stats{ $po_file }{ $_ };
164             push @tmp, "$_: $v";
165         }
166         if ( @tmp > 1 ) {
167             $res .= " (". join( ', ', @tmp ) .")";
168         }
169         elsif ( @tmp == 1 ) {
170             $res .= " (". (split /:/, $tmp[0])[0] .")";
171         }
172         print $res, "\n";
173     }
174
175     print "\n$legend\n";
176 }
177
178 sub clean {
179     my %opt = %{ shift() };
180     $opt{'keep'} = [qw(not-translated equal)];
181     return shrink( \%opt, @_ );
182 }
183
184 sub rosetta {
185     my %opt = %{ shift() };
186     my $url = shift or die 'must provide rosseta download url or directory with new po files';
187
188     my $dir;
189     if ( $url =~ m/^[a-z]+:\/\// ) {
190         require File::Temp;
191         $dir = File::Temp::tempdir();
192         my ($fname) = $url =~ m{([^/]+)$};
193
194         print "Downloading $url\n";
195         require LWP::Simple;
196         LWP::Simple::getstore($url => "$dir/$fname");
197
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 );
202     }
203     elsif ( -e $url && -d _ ) {
204         $dir = $url;
205     }
206     else {
207         die "Is not URL or directory: '$url'";
208     }
209
210     my @files = <$dir/rt/*.po>, <$dir/*.po>;
211     unless ( @files ) {
212         print STDERR "No files in $dir/rt/*.po and $dir/*.po\n";
213         exit;
214     }
215
216     require Locale::Maketext::Extract;
217     Locale::Maketext::Lexicon::set_option('use_fuzzy', 1);
218     Locale::Maketext::Lexicon::set_option('allow_empty', 1);
219
220     require Locale::PO;
221
222     for ( @files ) {
223         my ($lang) = m/([\w_]+)\.po/;
224         my $fn_orig = "lib/RT/I18N/$lang.po";
225
226         print "$_ -> $fn_orig\n";
227
228         # retain the "NOT FOUND IN SOURCE" entries
229         require File::Temp;
230         my $tmp = File::Temp->new;
231         system("sed -e 's/^#~ //' $_ > $tmp");
232         my $ext = Locale::Maketext::Extract->new;
233         $ext->read_po($tmp);
234
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;
243         }
244         if ( $opt{'boundary'} && $lang !~ /^en(_[A-Z]{2})?$/ ) { # en[_**] are exceptional
245             my @ids = $ext->msgids;
246             my $translated = 0;
247             foreach my $id ( @ids ) {
248                 next unless $ext->msgstr( $id );
249                 next if $ext->msgstr( $id ) eq $id;
250                 $translated++;
251             }
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";
256                 unlink $fn_orig;
257                 next;
258             }
259         }
260         $ext->write_po($fn_orig);
261     }
262     extract({});
263 }
264
265 sub extract {
266     shift;
267     system($^X, 'sbin/extract-message-catalog', @_);
268 }
269