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