2 # BEGIN BPS TAGGED BLOCK {{{
6 # This software is Copyright (c) 1996-2009 Best Practical Solutions, LLC
7 # <jesse@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>
58 use vars qw($DEBUG $FILECAT);
62 # po dir is for extensions
63 @ARGV = (<lib/RT/I18N/*.po>, <lib/RT/I18N/*.pot>, <po/*.po>, <po/*.pot>) unless @ARGV;
67 # extract all strings and stuff them into $FILECAT
68 # scan html dir for extensions
69 File::Find::find( { wanted => \&extract_strings_from_code, follow => 1 }, qw(bin sbin lib share html etc) );
71 # remove msgid with $ in it. XXX: perhaps give some warnings here
72 $FILECAT = { map { $_ => $FILECAT->{$_} } grep { !m/\$/ } keys %$FILECAT };
74 # ensure proper escaping and [_1] => %1 transformation
75 foreach my $str ( sort keys %{$FILECAT} ) {
76 my $entry = $FILECAT->{$str};
81 $str =~ s/((?<!~)(?:~~)*)\[_(\d+)\]/$1%$2/g;
82 $str =~ s/((?<!~)(?:~~)*)\[([A-Za-z#*]\w*),([^\]]+)\]/"$1%$2(".escape($3).")"/eg;
83 $str =~ s/~([\[\]])/$1/g;
85 delete $FILECAT->{$oldstr};
86 $FILECAT->{$str} = $entry;
89 # update all language dictionaries
90 foreach my $dict (@ARGV) {
91 $dict = "lib/RT/I18N/$dict.pot" if ( $dict eq 'rt' );
92 $dict = "lib/RT/I18N/$dict.po" unless -f $dict or $dict =~ m!/!;
103 # {{{ pull strings out of the code.
105 sub extract_strings_from_code {
110 return if ( $File::Find::dir =~ 'lib/blib|lib/t/autogen|var|m4|local' );
111 return if ( /\.(?:pot|po|bak|gif|png|psd|jpe?g|svg|css|js)$/ );
112 return if ( /~|,D|,B$|extract-message-catalog$|tweak-template-locstring$/ );
113 return if ( /^[\.#]/ );
114 return if ( -f "$_.in" );
116 print "Looking at $File::Find::name\n";
117 my $filename = $File::Find::name;
118 $filename =~ s'^\./'';
119 $filename =~ s'\.in$'';
121 unless (open _, $file) {
122 print "Cannot open $file for reading ($!), skipping.\n";
126 my $re_space_wo_nl = qr{(?!\n)\s};
127 my $re_loc_suffix = qr{$re_space_wo_nl* \# $re_space_wo_nl* loc $re_space_wo_nl* $}x;
128 my $re_loc_qw_suffix = qr{$re_space_wo_nl* \# $re_space_wo_nl* loc_qw $re_space_wo_nl* $}x;
129 my $re_loc_pair_suffix = qr{$re_space_wo_nl* \# $re_space_wo_nl* loc_pair $re_space_wo_nl* $}x;
130 my $re_loc_left_pair_suffix = qr{$re_space_wo_nl* \# $re_space_wo_nl* loc_left_pair $re_space_wo_nl* $}x;
131 my $re_delim = $RE{delimited}{-delim=>q{'"}}{-keep};
135 # Mason filter: <&|/l>...</&>
137 while (m!\G.*?<&\|/l(.*?)&>(.*?)</&>!sg) {
138 my ( $vars, $str ) = ( $1, $2 );
139 $vars =~ s/[\n\r]//g;
140 $line += ( () = ( $& =~ /\n/g ) ); # cryptocontext!
142 #print "STR IS $str\n";
143 push @{ $FILECAT->{$str} }, [ $filename, $line, $vars ];
146 # Localization function: loc(...)
149 while (m/\G.*?\bloc$RE{balanced}{-parens=>'()'}{-keep}/sg) {
151 $line += ( () = ( $& =~ /\n/g ) ); # cryptocontext!
155 /\(\s*($re_delim)(.*?)\s*\)$/so ) {
157 $str = substr( $1, 1, -1 ); # $str comes before $vars now
164 $vars =~ s/[\n\r]//g;
167 push @{ $FILECAT->{$str} }, [ $filename, $line, $vars ];
170 # Comment-based mark: "..." # loc
173 while (m/\G.*?($re_delim)[\}\)\],;]*$re_loc_suffix/smgo) {
175 $line += ( () = ( $& =~ /\n/g ) ); # cryptocontext!
176 unless ( defined $str ) {
177 warn "Couldn't process loc at $filename:$line";
180 $str = substr($str, 1, -1);
182 push @{ $FILECAT->{$str} }, [ $filename, $line, '' ];
185 # Comment-based qw mark: "qw(...)" # loc_qw
188 while (m/\G.*?(?:(qw\([^)]+\))[\}\)\],;]*)?$re_loc_qw_suffix/smgo) {
190 $line += ( () = ( $& =~ /\n/g ) ); # cryptocontext!
191 unless ( defined $str ) {
192 warn "Couldn't process loc_qw at $filename:$line";
195 foreach my $value (eval($str)) {
196 push @{ $FILECAT->{$value} }, [ $filename, $line, '' ];
200 # Comment-based left pair mark: "..." => ... # loc_left_pair
203 while (m/\G.*?(?:(\w+)\s*=>[^#\n]+?)?$re_loc_left_pair_suffix/smgo) {
205 $line += ( () = ( $& =~ /\n/g ) ); # cryptocontext!
206 unless ( defined $key ) {
207 warn "Couldn't process loc_left_pair at $filename:$line";
211 push @{ $FILECAT->{$key} }, [ $filename, $line, '' ];
214 # Comment-based pair mark: "..." => "..." # loc_pair
217 while (m/\G.*?(?:(\w+)\s*=>\s*($re_delim)[\}\)\],;]*)?$re_loc_pair_suffix/smgo) {
220 $line += ( () = ( $& =~ /\n/g ) ); # cryptocontext!
221 unless ( defined $key && defined $val ) {
222 warn "Couldn't process loc_pair at $filename:$line";
225 $val = substr($val, 1, -1);
228 push @{ $FILECAT->{$key} }, [ $filename, $line, '' ];
229 push @{ $FILECAT->{$val} }, [ $filename, $line, '' ];
234 # }}} extract from strings
239 my ( %Lexicon, %Header);
242 unless (!-e $file or -w $file) {
243 warn "Can't write to $lang, skipping...\n";
247 print "Updating $lang...\n";
250 @lines = (<LEXICON>) if open (LEXICON, $file);
251 @lines = grep { !/^(#(:|\.)\s*|$)/ } @lines;
254 $msghdr .= shift @lines while ( $lines[0] && $lines[0] !~ /^(#~ )?msgid/ );
258 # '#~ ' is the prefix of launchpad for msg that's not found the the source
259 # we'll remove the prefix later so we can still show them with our own mark
261 $msgid .= shift @lines while ( $lines[0] && $lines[0] =~ /^(#~ )?(msgid|")/ );
263 $msgstr .= shift @lines while ( $lines[0] && $lines[0] =~ /^(#~ )?(msgstr|")/ );
270 $msgid =~ s/^#~ //mg;
271 $msgstr =~ s/^#~ //mg;
273 $msgid =~ s/^msgid "(.*)"\s*?$/$1/m or warn "$msgid in $file";
275 if ( $msgid eq '' ) {
276 # null msgid, msgstr will have head info
277 $msgstr =~ s/^msgstr "(.*)"\s*?$/$1/ms or warn "$msgstr in $file";
280 $msgstr =~ s/^msgstr "(.*)"\s*?$/$1/m or warn "$msgstr in $file";
283 if ( $msgid ne '' ) {
284 for my $msg ( \$msgid, \$msgstr ) {
285 if ( $$msg =~ /\n/ ) {
286 my @lines = split /\n/, $$msg;
288 shift @lines; # first line don't need to handle any more
296 # convert \\n back to \n
297 $$msg =~ s/(?!\\)\\n/\n/g;
301 $Lexicon{$msgid} = $msgstr;
302 $Header{$msgid} = $msghdr;
305 my $is_english = ( $lang =~ /^en(?:[^A-Za-z]|$)/ );
307 foreach my $str ( sort keys %{$FILECAT} ) {
308 $Lexicon{$str} ||= '';
310 foreach ( sort keys %Lexicon ) {
311 my $f = join ( ' ', sort map $_->[0].":".$_->[1], @{ $FILECAT->{$_} } );
315 if ( !$Lexicon{$_} and $Lexicon{$nospace} ) {
317 $Lexicon{$nospace} . ( ' ' x ( length($_) - length($nospace) ) );
320 next if !length( $Lexicon{$_} ) and $is_english;
323 $out .= $Header{$_} if exists $Header{$_};
327 next if (!$f && $_ && !$Lexicon{$_});
328 if ( $f && $f !~ /^\s+$/ ) {
333 $out .= "#: NOT FOUND IN SOURCE\n";
335 foreach my $entry ( grep { $_->[2] } @{ $FILECAT->{$_} } ) {
336 my ( $file, $line, $var ) = @{$entry};
337 $var =~ s/^\s*,\s*//;
339 $out .= "#. ($var)\n" unless $seen{$var}++;
341 $out .= 'msgid ' . fmt($_) . "msgstr \"$Lexicon{$_}\"\n\n";
344 open PO, ">$file" or die $!;
353 $text =~ s/\b_(\d+)/%$1/;
359 return "\"$str\"\n" unless $str =~ /\n/;
361 my $multi_line = ($str =~ /\n(?!\z)/);
362 $str =~ s/\n/\\n"\n"/g;
364 if ($str =~ /\n"$/) {
370 return $multi_line ? qq(""\n"$str) : qq("$str);
376 # c-indentation-style: bsd
378 # indent-tabs-mode: nil
380 # vim: expandtab shiftwidth=4: