| 1 | #!@INTLTOOL_PERL@ -w |
|---|
| 2 | # -*- Mode: perl; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
|---|
| 3 | |
|---|
| 4 | # |
|---|
| 5 | # The Intltool Message Extractor |
|---|
| 6 | # |
|---|
| 7 | # Copyright (C) 2000-2001, 2003 Free Software Foundation. |
|---|
| 8 | # |
|---|
| 9 | # Intltool is free software; you can redistribute it and/or |
|---|
| 10 | # modify it under the terms of the GNU General Public License as |
|---|
| 11 | # published by the Free Software Foundation; either version 2 of the |
|---|
| 12 | # License, or (at your option) any later version. |
|---|
| 13 | # |
|---|
| 14 | # Intltool is distributed in the hope that it will be useful, |
|---|
| 15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 17 | # General Public License for more details. |
|---|
| 18 | # |
|---|
| 19 | # You should have received a copy of the GNU General Public License |
|---|
| 20 | # along with this program; if not, write to the Free Software |
|---|
| 21 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|---|
| 22 | # |
|---|
| 23 | # As a special exception to the GNU General Public License, if you |
|---|
| 24 | # distribute this file as part of a program that contains a |
|---|
| 25 | # configuration script generated by Autoconf, you may include it under |
|---|
| 26 | # the same distribution terms that you use for the rest of that program. |
|---|
| 27 | # |
|---|
| 28 | # Authors: Kenneth Christiansen <kenneth@gnu.org> |
|---|
| 29 | # Darin Adler <darin@bentspoon.com> |
|---|
| 30 | # |
|---|
| 31 | |
|---|
| 32 | ## Release information |
|---|
| 33 | my $PROGRAM = "intltool-extract"; |
|---|
| 34 | my $PACKAGE = "intltool"; |
|---|
| 35 | my $VERSION = "0.35.1"; |
|---|
| 36 | |
|---|
| 37 | ## Loaded modules |
|---|
| 38 | use strict; |
|---|
| 39 | use File::Basename; |
|---|
| 40 | use Getopt::Long; |
|---|
| 41 | |
|---|
| 42 | ## Scalars used by the option stuff |
|---|
| 43 | my $TYPE_ARG = "0"; |
|---|
| 44 | my $LOCAL_ARG = "0"; |
|---|
| 45 | my $HELP_ARG = "0"; |
|---|
| 46 | my $VERSION_ARG = "0"; |
|---|
| 47 | my $UPDATE_ARG = "0"; |
|---|
| 48 | my $QUIET_ARG = "0"; |
|---|
| 49 | my $SRCDIR_ARG = "."; |
|---|
| 50 | |
|---|
| 51 | my $FILE; |
|---|
| 52 | my $OUTFILE; |
|---|
| 53 | |
|---|
| 54 | my $gettext_type = ""; |
|---|
| 55 | my $input; |
|---|
| 56 | my %messages = (); |
|---|
| 57 | my %loc = (); |
|---|
| 58 | my %count = (); |
|---|
| 59 | my %comments = (); |
|---|
| 60 | my $strcount = 0; |
|---|
| 61 | |
|---|
| 62 | my $XMLCOMMENT = ""; |
|---|
| 63 | |
|---|
| 64 | ## Use this instead of \w for XML files to handle more possible characters. |
|---|
| 65 | my $w = "[-A-Za-z0-9._:]"; |
|---|
| 66 | |
|---|
| 67 | ## Always print first |
|---|
| 68 | $| = 1; |
|---|
| 69 | |
|---|
| 70 | ## Handle options |
|---|
| 71 | GetOptions ( |
|---|
| 72 | "type=s" => \$TYPE_ARG, |
|---|
| 73 | "local|l" => \$LOCAL_ARG, |
|---|
| 74 | "help|h" => \$HELP_ARG, |
|---|
| 75 | "version|v" => \$VERSION_ARG, |
|---|
| 76 | "update" => \$UPDATE_ARG, |
|---|
| 77 | "quiet|q" => \$QUIET_ARG, |
|---|
| 78 | "srcdir=s" => \$SRCDIR_ARG, |
|---|
| 79 | ) or &error; |
|---|
| 80 | |
|---|
| 81 | &split_on_argument; |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | ## Check for options. |
|---|
| 85 | ## This section will check for the different options. |
|---|
| 86 | |
|---|
| 87 | sub split_on_argument { |
|---|
| 88 | |
|---|
| 89 | if ($VERSION_ARG) { |
|---|
| 90 | &version; |
|---|
| 91 | |
|---|
| 92 | } elsif ($HELP_ARG) { |
|---|
| 93 | &help; |
|---|
| 94 | |
|---|
| 95 | } elsif ($LOCAL_ARG) { |
|---|
| 96 | &place_local; |
|---|
| 97 | &extract; |
|---|
| 98 | |
|---|
| 99 | } elsif ($UPDATE_ARG) { |
|---|
| 100 | &place_normal; |
|---|
| 101 | &extract; |
|---|
| 102 | |
|---|
| 103 | } elsif (@ARGV > 0) { |
|---|
| 104 | &place_normal; |
|---|
| 105 | &message; |
|---|
| 106 | &extract; |
|---|
| 107 | |
|---|
| 108 | } else { |
|---|
| 109 | &help; |
|---|
| 110 | |
|---|
| 111 | } |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | sub place_normal { |
|---|
| 115 | $FILE = $ARGV[0]; |
|---|
| 116 | $OUTFILE = "$FILE.h"; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | sub place_local { |
|---|
| 120 | $FILE = $ARGV[0]; |
|---|
| 121 | $OUTFILE = fileparse($FILE, ()); |
|---|
| 122 | if (!-e "tmp/") { |
|---|
| 123 | system("mkdir tmp/"); |
|---|
| 124 | } |
|---|
| 125 | $OUTFILE = "./tmp/$OUTFILE.h" |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | sub determine_type { |
|---|
| 129 | if ($TYPE_ARG =~ /^gettext\/(.*)/) { |
|---|
| 130 | $gettext_type=$1 |
|---|
| 131 | } |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | ## Sub for printing release information |
|---|
| 135 | sub version{ |
|---|
| 136 | print <<_EOF_; |
|---|
| 137 | ${PROGRAM} (${PACKAGE}) $VERSION |
|---|
| 138 | Copyright (C) 2000, 2003 Free Software Foundation, Inc. |
|---|
| 139 | Written by Kenneth Christiansen, 2000. |
|---|
| 140 | |
|---|
| 141 | This is free software; see the source for copying conditions. There is NO |
|---|
| 142 | warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|---|
| 143 | _EOF_ |
|---|
| 144 | exit; |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | ## Sub for printing usage information |
|---|
| 148 | sub help { |
|---|
| 149 | print <<_EOF_; |
|---|
| 150 | Usage: ${PROGRAM} [OPTION]... [FILENAME] |
|---|
| 151 | Generates a header file from an XML source file. |
|---|
| 152 | |
|---|
| 153 | It grabs all strings between <_translatable_node> and its end tag in |
|---|
| 154 | XML files. Read manpage (man ${PROGRAM}) for more info. |
|---|
| 155 | |
|---|
| 156 | --type=TYPE Specify the file type of FILENAME. Currently supports: |
|---|
| 157 | "gettext/glade", "gettext/ini", "gettext/keys" |
|---|
| 158 | "gettext/rfc822deb", "gettext/schemas", |
|---|
| 159 | "gettext/scheme", "gettext/xml", "gettext/quoted" |
|---|
| 160 | -l, --local Writes output into current working directory |
|---|
| 161 | (conflicts with --update) |
|---|
| 162 | --update Writes output into the same directory the source file |
|---|
| 163 | reside (conflicts with --local) |
|---|
| 164 | --srcdir Root of the source tree |
|---|
| 165 | -v, --version Output version information and exit |
|---|
| 166 | -h, --help Display this help and exit |
|---|
| 167 | -q, --quiet Quiet mode |
|---|
| 168 | |
|---|
| 169 | Report bugs to http://bugzilla.gnome.org/ (product name "$PACKAGE") |
|---|
| 170 | or send email to <xml-i18n-tools\@gnome.org>. |
|---|
| 171 | _EOF_ |
|---|
| 172 | exit; |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | ## Sub for printing error messages |
|---|
| 176 | sub error{ |
|---|
| 177 | print STDERR "Try `${PROGRAM} --help' for more information.\n"; |
|---|
| 178 | exit; |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | sub message { |
|---|
| 182 | print "Generating C format header file for translation.\n" unless $QUIET_ARG; |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | sub extract { |
|---|
| 186 | &determine_type; |
|---|
| 187 | |
|---|
| 188 | &convert; |
|---|
| 189 | |
|---|
| 190 | open OUT, ">$OUTFILE"; |
|---|
| 191 | binmode (OUT) if $^O eq 'MSWin32'; |
|---|
| 192 | &msg_write; |
|---|
| 193 | close OUT; |
|---|
| 194 | |
|---|
| 195 | print "Wrote $OUTFILE\n" unless $QUIET_ARG; |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | sub convert { |
|---|
| 199 | |
|---|
| 200 | ## Reading the file |
|---|
| 201 | { |
|---|
| 202 | local (*IN); |
|---|
| 203 | local $/; #slurp mode |
|---|
| 204 | open (IN, "<$SRCDIR_ARG/$FILE") || die "can't open $SRCDIR_ARG/$FILE: $!"; |
|---|
| 205 | $input = <IN>; |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | &type_ini if $gettext_type eq "ini"; |
|---|
| 209 | &type_keys if $gettext_type eq "keys"; |
|---|
| 210 | &type_xml if $gettext_type eq "xml"; |
|---|
| 211 | &type_glade if $gettext_type eq "glade"; |
|---|
| 212 | &type_scheme if $gettext_type eq "scheme"; |
|---|
| 213 | &type_schemas if $gettext_type eq "schemas"; |
|---|
| 214 | &type_rfc822deb if $gettext_type eq "rfc822deb"; |
|---|
| 215 | &type_quoted if $gettext_type eq "quoted"; |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | sub entity_decode_minimal |
|---|
| 219 | { |
|---|
| 220 | local ($_) = @_; |
|---|
| 221 | |
|---|
| 222 | s/'/'/g; # ' |
|---|
| 223 | s/"/"/g; # " |
|---|
| 224 | s/&/&/g; |
|---|
| 225 | |
|---|
| 226 | return $_; |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | sub entity_decode |
|---|
| 230 | { |
|---|
| 231 | local ($_) = @_; |
|---|
| 232 | |
|---|
| 233 | s/'/'/g; # ' |
|---|
| 234 | s/"/"/g; # " |
|---|
| 235 | s/&/&/g; |
|---|
| 236 | s/</</g; |
|---|
| 237 | s/>/>/g; |
|---|
| 238 | |
|---|
| 239 | return $_; |
|---|
| 240 | } |
|---|
| 241 | |
|---|
| 242 | sub escape_char |
|---|
| 243 | { |
|---|
| 244 | return '\"' if $_ eq '"'; |
|---|
| 245 | return '\n' if $_ eq "\n"; |
|---|
| 246 | return '\\\\' if $_ eq '\\'; |
|---|
| 247 | |
|---|
| 248 | return $_; |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | sub escape |
|---|
| 252 | { |
|---|
| 253 | my ($string) = @_; |
|---|
| 254 | return join "", map &escape_char, split //, $string; |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | sub type_ini { |
|---|
| 258 | ### For generic translatable desktop files ### |
|---|
| 259 | while ($input =~ /^_.*=(.*)$/mg) { |
|---|
| 260 | $messages{$1} = []; |
|---|
| 261 | } |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | sub type_keys { |
|---|
| 265 | ### For generic translatable mime/keys files ### |
|---|
| 266 | while ($input =~ /^\s*_\w+=(.*)$/mg) { |
|---|
| 267 | $messages{$1} = []; |
|---|
| 268 | } |
|---|
| 269 | } |
|---|
| 270 | |
|---|
| 271 | sub type_xml { |
|---|
| 272 | ### For generic translatable XML files ### |
|---|
| 273 | my $tree = readXml($input); |
|---|
| 274 | parseTree(0, $tree); |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | sub print_var { |
|---|
| 278 | my $var = shift; |
|---|
| 279 | my $vartype = ref $var; |
|---|
| 280 | |
|---|
| 281 | if ($vartype =~ /ARRAY/) { |
|---|
| 282 | my @arr = @{$var}; |
|---|
| 283 | print "[ "; |
|---|
| 284 | foreach my $el (@arr) { |
|---|
| 285 | print_var($el); |
|---|
| 286 | print ", "; |
|---|
| 287 | } |
|---|
| 288 | print "] "; |
|---|
| 289 | } elsif ($vartype =~ /HASH/) { |
|---|
| 290 | my %hash = %{$var}; |
|---|
| 291 | print "{ "; |
|---|
| 292 | foreach my $key (keys %hash) { |
|---|
| 293 | print "$key => "; |
|---|
| 294 | print_var($hash{$key}); |
|---|
| 295 | print ", "; |
|---|
| 296 | } |
|---|
| 297 | print "} "; |
|---|
| 298 | } else { |
|---|
| 299 | print $var; |
|---|
| 300 | } |
|---|
| 301 | } |
|---|
| 302 | |
|---|
| 303 | # Same syntax as getAttributeString in intltool-merge.in.in, similar logic (look for ## differences comment) |
|---|
| 304 | sub getAttributeString |
|---|
| 305 | { |
|---|
| 306 | my $sub = shift; |
|---|
| 307 | my $do_translate = shift || 1; |
|---|
| 308 | my $language = shift || ""; |
|---|
| 309 | my $translate = shift; |
|---|
| 310 | my $result = ""; |
|---|
| 311 | foreach my $e (reverse(sort(keys %{ $sub }))) { |
|---|
| 312 | my $key = $e; |
|---|
| 313 | my $string = $sub->{$e}; |
|---|
| 314 | my $quote = '"'; |
|---|
| 315 | |
|---|
| 316 | $string =~ s/^[\s]+//; |
|---|
| 317 | $string =~ s/[\s]+$//; |
|---|
| 318 | |
|---|
| 319 | if ($string =~ /^'.*'$/) |
|---|
| 320 | { |
|---|
| 321 | $quote = "'"; |
|---|
| 322 | } |
|---|
| 323 | $string =~ s/^['"]//g; |
|---|
| 324 | $string =~ s/['"]$//g; |
|---|
| 325 | |
|---|
| 326 | ## differences from intltool-merge.in.in |
|---|
| 327 | if ($key =~ /^_/) { |
|---|
| 328 | $comments{entity_decode($string)} = $XMLCOMMENT if $XMLCOMMENT; |
|---|
| 329 | $messages{entity_decode($string)} = []; |
|---|
| 330 | $$translate = 2; |
|---|
| 331 | } |
|---|
| 332 | ## differences end here from intltool-merge.in.in |
|---|
| 333 | $result .= " $key=$quote$string$quote"; |
|---|
| 334 | } |
|---|
| 335 | return $result; |
|---|
| 336 | } |
|---|
| 337 | |
|---|
| 338 | # Verbatim copy from intltool-merge.in.in |
|---|
| 339 | sub getXMLstring |
|---|
| 340 | { |
|---|
| 341 | my $ref = shift; |
|---|
| 342 | my $spacepreserve = shift || 0; |
|---|
| 343 | my @list = @{ $ref }; |
|---|
| 344 | my $result = ""; |
|---|
| 345 | |
|---|
| 346 | my $count = scalar(@list); |
|---|
| 347 | my $attrs = $list[0]; |
|---|
| 348 | my $index = 1; |
|---|
| 349 | |
|---|
| 350 | $spacepreserve = 1 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?preserve["']?$/)); |
|---|
| 351 | $spacepreserve = 0 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?default["']?$/)); |
|---|
| 352 | |
|---|
| 353 | while ($index < $count) { |
|---|
| 354 | my $type = $list[$index]; |
|---|
| 355 | my $content = $list[$index+1]; |
|---|
| 356 | if (! $type ) { |
|---|
| 357 | # We've got CDATA |
|---|
| 358 | if ($content) { |
|---|
| 359 | # lets strip the whitespace here, and *ONLY* here |
|---|
| 360 | $content =~ s/\s+/ /gs if (!$spacepreserve); |
|---|
| 361 | $result .= $content; |
|---|
| 362 | } |
|---|
| 363 | } elsif ( "$type" ne "1" ) { |
|---|
| 364 | # We've got another element |
|---|
| 365 | $result .= "<$type"; |
|---|
| 366 | $result .= getAttributeString(@{$content}[0], 0); # no nested translatable elements |
|---|
| 367 | if ($content) { |
|---|
| 368 | my $subresult = getXMLstring($content, $spacepreserve); |
|---|
| 369 | if ($subresult) { |
|---|
| 370 | $result .= ">".$subresult . "</$type>"; |
|---|
| 371 | } else { |
|---|
| 372 | $result .= "/>"; |
|---|
| 373 | } |
|---|
| 374 | } else { |
|---|
| 375 | $result .= "/>"; |
|---|
| 376 | } |
|---|
| 377 | } |
|---|
| 378 | $index += 2; |
|---|
| 379 | } |
|---|
| 380 | return $result; |
|---|
| 381 | } |
|---|
| 382 | |
|---|
| 383 | # Verbatim copy from intltool-merge.in.in, except for MULTIPLE_OUTPUT handling removed |
|---|
| 384 | # Translate list of nodes if necessary |
|---|
| 385 | sub translate_subnodes |
|---|
| 386 | { |
|---|
| 387 | my $fh = shift; |
|---|
| 388 | my $content = shift; |
|---|
| 389 | my $language = shift || ""; |
|---|
| 390 | my $singlelang = shift || 0; |
|---|
| 391 | my $spacepreserve = shift || 0; |
|---|
| 392 | |
|---|
| 393 | my @nodes = @{ $content }; |
|---|
| 394 | |
|---|
| 395 | my $count = scalar(@nodes); |
|---|
| 396 | my $index = 0; |
|---|
| 397 | while ($index < $count) { |
|---|
| 398 | my $type = $nodes[$index]; |
|---|
| 399 | my $rest = $nodes[$index+1]; |
|---|
| 400 | traverse($fh, $type, $rest, $language, $spacepreserve); |
|---|
| 401 | $index += 2; |
|---|
| 402 | } |
|---|
| 403 | } |
|---|
| 404 | |
|---|
| 405 | # Based on traverse() in intltool-merge.in.in |
|---|
| 406 | sub traverse |
|---|
| 407 | { |
|---|
| 408 | my $fh = shift; # unused, to allow us to sync code between -merge and -extract |
|---|
| 409 | my $nodename = shift; |
|---|
| 410 | my $content = shift; |
|---|
| 411 | my $language = shift || ""; |
|---|
| 412 | my $spacepreserve = shift || 0; |
|---|
| 413 | |
|---|
| 414 | if ($nodename && "$nodename" eq "1") { |
|---|
| 415 | $XMLCOMMENT = $content; |
|---|
| 416 | } elsif ($nodename) { |
|---|
| 417 | # element |
|---|
| 418 | my @all = @{ $content }; |
|---|
| 419 | my $attrs = shift @all; |
|---|
| 420 | my $translate = 0; |
|---|
| 421 | my $outattr = getAttributeString($attrs, 1, $language, \$translate); |
|---|
| 422 | |
|---|
| 423 | if ($nodename =~ /^_/) { |
|---|
| 424 | $translate = 1; |
|---|
| 425 | $nodename =~ s/^_//; |
|---|
| 426 | } |
|---|
| 427 | my $lookup = ''; |
|---|
| 428 | |
|---|
| 429 | $spacepreserve = 0 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?default["']?$/)); |
|---|
| 430 | $spacepreserve = 1 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?preserve["']?$/)); |
|---|
| 431 | |
|---|
| 432 | if ($translate) { |
|---|
| 433 | $lookup = getXMLstring($content, $spacepreserve); |
|---|
| 434 | if (!$spacepreserve) { |
|---|
| 435 | $lookup =~ s/^\s+//s; |
|---|
| 436 | $lookup =~ s/\s+$//s; |
|---|
| 437 | } |
|---|
| 438 | |
|---|
| 439 | if ($lookup && $translate != 2) { |
|---|
| 440 | $comments{$lookup} = $XMLCOMMENT if $XMLCOMMENT; |
|---|
| 441 | $messages{$lookup} = []; |
|---|
| 442 | } elsif ($translate == 2) { |
|---|
| 443 | translate_subnodes($fh, \@all, $language, 1, $spacepreserve); |
|---|
| 444 | } |
|---|
| 445 | } else { |
|---|
| 446 | $XMLCOMMENT = ""; |
|---|
| 447 | my $count = scalar(@all); |
|---|
| 448 | if ($count > 0) { |
|---|
| 449 | my $index = 0; |
|---|
| 450 | while ($index < $count) { |
|---|
| 451 | my $type = $all[$index]; |
|---|
| 452 | my $rest = $all[$index+1]; |
|---|
| 453 | traverse($fh, $type, $rest, $language, $spacepreserve); |
|---|
| 454 | $index += 2; |
|---|
| 455 | } |
|---|
| 456 | } |
|---|
| 457 | } |
|---|
| 458 | $XMLCOMMENT = ""; |
|---|
| 459 | } |
|---|
| 460 | } |
|---|
| 461 | |
|---|
| 462 | |
|---|
| 463 | # Verbatim copy from intltool-merge.in.in, $fh for compatibility |
|---|
| 464 | sub parseTree |
|---|
| 465 | { |
|---|
| 466 | my $fh = shift; |
|---|
| 467 | my $ref = shift; |
|---|
| 468 | my $language = shift || ""; |
|---|
| 469 | |
|---|
| 470 | my $name = shift @{ $ref }; |
|---|
| 471 | my $cont = shift @{ $ref }; |
|---|
| 472 | |
|---|
| 473 | while (!$name || "$name" eq "1") { |
|---|
| 474 | $name = shift @{ $ref }; |
|---|
| 475 | $cont = shift @{ $ref }; |
|---|
| 476 | } |
|---|
| 477 | |
|---|
| 478 | my $spacepreserve = 0; |
|---|
| 479 | my $attrs = @{$cont}[0]; |
|---|
| 480 | $spacepreserve = 1 if ((exists $attrs->{"xml:space"}) && ($attrs->{"xml:space"} =~ /^["']?preserve["']?$/)); |
|---|
| 481 | |
|---|
| 482 | traverse($fh, $name, $cont, $language, $spacepreserve); |
|---|
| 483 | } |
|---|
| 484 | |
|---|
| 485 | # Verbatim copy from intltool-merge.in.in |
|---|
| 486 | sub intltool_tree_comment |
|---|
| 487 | { |
|---|
| 488 | my $expat = shift; |
|---|
| 489 | my $data = $expat->original_string(); |
|---|
| 490 | my $clist = $expat->{Curlist}; |
|---|
| 491 | my $pos = $#$clist; |
|---|
| 492 | |
|---|
| 493 | $data =~ s/^<!--//s; |
|---|
| 494 | $data =~ s/-->$//s; |
|---|
| 495 | push @$clist, 1 => $data; |
|---|
| 496 | } |
|---|
| 497 | |
|---|
| 498 | # Verbatim copy from intltool-merge.in.in |
|---|
| 499 | sub intltool_tree_cdatastart |
|---|
| 500 | { |
|---|
| 501 | my $expat = shift; |
|---|
| 502 | my $clist = $expat->{Curlist}; |
|---|
| 503 | my $pos = $#$clist; |
|---|
| 504 | |
|---|
| 505 | push @$clist, 0 => $expat->original_string(); |
|---|
| 506 | } |
|---|
| 507 | |
|---|
| 508 | # Verbatim copy from intltool-merge.in.in |
|---|
| 509 | sub intltool_tree_cdataend |
|---|
| 510 | { |
|---|
| 511 | my $expat = shift; |
|---|
| 512 | my $clist = $expat->{Curlist}; |
|---|
| 513 | my $pos = $#$clist; |
|---|
| 514 | |
|---|
| 515 | $clist->[$pos] .= $expat->original_string(); |
|---|
| 516 | } |
|---|
| 517 | |
|---|
| 518 | # Verbatim copy from intltool-merge.in.in |
|---|
| 519 | sub intltool_tree_char |
|---|
| 520 | { |
|---|
| 521 | my $expat = shift; |
|---|
| 522 | my $text = shift; |
|---|
| 523 | my $clist = $expat->{Curlist}; |
|---|
| 524 | my $pos = $#$clist; |
|---|
| 525 | |
|---|
| 526 | # Use original_string so that we retain escaped entities |
|---|
| 527 | # in CDATA sections. |
|---|
| 528 | # |
|---|
| 529 | if ($pos > 0 and $clist->[$pos - 1] eq '0') { |
|---|
| 530 | $clist->[$pos] .= $expat->original_string(); |
|---|
| 531 | } else { |
|---|
| 532 | push @$clist, 0 => $expat->original_string(); |
|---|
| 533 | } |
|---|
| 534 | } |
|---|
| 535 | |
|---|
| 536 | # Verbatim copy from intltool-merge.in.in |
|---|
| 537 | sub intltool_tree_start |
|---|
| 538 | { |
|---|
| 539 | my $expat = shift; |
|---|
| 540 | my $tag = shift; |
|---|
| 541 | my @origlist = (); |
|---|
| 542 | |
|---|
| 543 | # Use original_string so that we retain escaped entities |
|---|
| 544 | # in attribute values. We must convert the string to an |
|---|
| 545 | # @origlist array to conform to the structure of the Tree |
|---|
| 546 | # Style. |
|---|
| 547 | # |
|---|
| 548 | my @original_array = split /\x/, $expat->original_string(); |
|---|
| 549 | my $source = $expat->original_string(); |
|---|
| 550 | |
|---|
| 551 | # Remove leading tag. |
|---|
| 552 | # |
|---|
| 553 | $source =~ s|^\s*<\s*(\S+)||s; |
|---|
| 554 | |
|---|
| 555 | # Grab attribute key/value pairs and push onto @origlist array. |
|---|
| 556 | # |
|---|
| 557 | while ($source) |
|---|
| 558 | { |
|---|
| 559 | if ($source =~ /^\s*([\w:-]+)\s*[=]\s*["]/) |
|---|
| 560 | { |
|---|
| 561 | $source =~ s|^\s*([\w:-]+)\s*[=]\s*["]([^"]*)["]||s; |
|---|
| 562 | push @origlist, $1; |
|---|
| 563 | push @origlist, '"' . $2 . '"'; |
|---|
| 564 | } |
|---|
| 565 | elsif ($source =~ /^\s*([\w:-]+)\s*[=]\s*[']/) |
|---|
| 566 | { |
|---|
| 567 | $source =~ s|^\s*([\w:-]+)\s*[=]\s*[']([^']*)[']||s; |
|---|
| 568 | push @origlist, $1; |
|---|
| 569 | push @origlist, "'" . $2 . "'"; |
|---|
| 570 | } |
|---|
| 571 | else |
|---|
| 572 | { |
|---|
| 573 | last; |
|---|
| 574 | } |
|---|
| 575 | } |
|---|
| 576 | |
|---|
| 577 | my $ol = [ { @origlist } ]; |
|---|
| 578 | |
|---|
| 579 | push @{ $expat->{Lists} }, $expat->{Curlist}; |
|---|
| 580 | push @{ $expat->{Curlist} }, $tag => $ol; |
|---|
| 581 | $expat->{Curlist} = $ol; |
|---|
| 582 | } |
|---|
| 583 | |
|---|
| 584 | # Copied from intltool-merge.in.in and added comment handler. |
|---|
| 585 | sub readXml |
|---|
| 586 | { |
|---|
| 587 | my $xmldoc = shift || return; |
|---|
| 588 | my $ret = eval 'require XML::Parser'; |
|---|
| 589 | if(!$ret) { |
|---|
| 590 | die "You must have XML::Parser installed to run $0\n\n"; |
|---|
| 591 | } |
|---|
| 592 | my $xp = new XML::Parser(Style => 'Tree'); |
|---|
| 593 | $xp->setHandlers(Char => \&intltool_tree_char); |
|---|
| 594 | $xp->setHandlers(Start => \&intltool_tree_start); |
|---|
| 595 | $xp->setHandlers(CdataStart => \&intltool_tree_cdatastart); |
|---|
| 596 | $xp->setHandlers(CdataEnd => \&intltool_tree_cdataend); |
|---|
| 597 | |
|---|
| 598 | ## differences from intltool-merge.in.in |
|---|
| 599 | $xp->setHandlers(Comment => \&intltool_tree_comment); |
|---|
| 600 | ## differences end here from intltool-merge.in.in |
|---|
| 601 | |
|---|
| 602 | my $tree = $xp->parse($xmldoc); |
|---|
| 603 | #print_var($tree); |
|---|
| 604 | |
|---|
| 605 | # <foo><!-- comment --><head id="a">Hello <em>there</em></head><bar>Howdy<ref/></bar>do</foo> |
|---|
| 606 | # would be: |
|---|
| 607 | # [foo, [{}, 1, "comment", head, [{id => "a"}, 0, "Hello ", em, [{}, 0, "there"]], bar, |
|---|
| 608 | # [{}, 0, "Howdy", ref, [{}]], 0, "do" ] ] |
|---|
| 609 | |
|---|
| 610 | return $tree; |
|---|
| 611 | } |
|---|
| 612 | |
|---|
| 613 | sub type_schemas { |
|---|
| 614 | ### For schemas XML files ### |
|---|
| 615 | |
|---|
| 616 | # FIXME: We should handle escaped < (less than) |
|---|
| 617 | while ($input =~ / |
|---|
| 618 | <locale\ name="C">\s* |
|---|
| 619 | (<default>\s*(?:<!--([^>]*?)-->\s*)?(.*?)\s*<\/default>\s*)? |
|---|
| 620 | (<short>\s*(?:<!--([^>]*?)-->\s*)?(.*?)\s*<\/short>\s*)? |
|---|
| 621 | (<long>\s*(?:<!--([^>]*?)-->\s*)?(.*?)\s*<\/long>\s*)? |
|---|
| 622 | <\/locale> |
|---|
| 623 | /sgx) { |
|---|
| 624 | my @totranslate = ($3,$6,$9); |
|---|
| 625 | my @eachcomment = ($2,$5,$8); |
|---|
| 626 | foreach (@totranslate) { |
|---|
| 627 | my $currentcomment = shift @eachcomment; |
|---|
| 628 | next if !$_; |
|---|
| 629 | s/\s+/ /g; |
|---|
| 630 | $messages{entity_decode_minimal($_)} = []; |
|---|
| 631 | $comments{entity_decode_minimal($_)} = $currentcomment if (defined($currentcomment)); |
|---|
| 632 | } |
|---|
| 633 | } |
|---|
| 634 | } |
|---|
| 635 | |
|---|
| 636 | sub type_rfc822deb { |
|---|
| 637 | ### For rfc822-style Debian configuration files ### |
|---|
| 638 | |
|---|
| 639 | my $lineno = 1; |
|---|
| 640 | my $type = ''; |
|---|
| 641 | while ($input =~ /\G(.*?)(^|\n)(_+)([^:]+):[ \t]*(.*?)(?=\n\S|$)/sg) |
|---|
| 642 | { |
|---|
| 643 | my ($pre, $newline, $underscore, $tag, $text) = ($1, $2, $3, $4, $5); |
|---|
| 644 | while ($pre =~ m/\n/g) |
|---|
| 645 | { |
|---|
| 646 | $lineno ++; |
|---|
| 647 | } |
|---|
| 648 | $lineno += length($newline); |
|---|
| 649 | my @str_list = rfc822deb_split(length($underscore), $text); |
|---|
| 650 | for my $str (@str_list) |
|---|
| 651 | { |
|---|
| 652 | $strcount++; |
|---|
| 653 | $messages{$str} = []; |
|---|
| 654 | $loc{$str} = $lineno; |
|---|
| 655 | $count{$str} = $strcount; |
|---|
| 656 | my $usercomment = ''; |
|---|
| 657 | while($pre =~ s/(^|\n)#([^\n]*)$//s) |
|---|
| 658 | { |
|---|
| 659 | $usercomment = "\n" . $2 . $usercomment; |
|---|
| 660 | } |
|---|
| 661 | $comments{$str} = $tag . $usercomment; |
|---|
| 662 | } |
|---|
| 663 | $lineno += ($text =~ s/\n//g); |
|---|
| 664 | } |
|---|
| 665 | } |
|---|
| 666 | |
|---|
| 667 | sub rfc822deb_split { |
|---|
| 668 | # Debian defines a special way to deal with rfc822-style files: |
|---|
| 669 | # when a value contain newlines, it consists of |
|---|
| 670 | # 1. a short form (first line) |
|---|
| 671 | # 2. a long description, all lines begin with a space, |
|---|
| 672 | # and paragraphs are separated by a single dot on a line |
|---|
| 673 | # This routine returns an array of all paragraphs, and reformat |
|---|
| 674 | # them. |
|---|
| 675 | # When first argument is 2, the string is a comma separated list of |
|---|
| 676 | # values. |
|---|
| 677 | my $type = shift; |
|---|
| 678 | my $text = shift; |
|---|
| 679 | $text =~ s/^[ \t]//mg; |
|---|
| 680 | return (split(/, */, $text, 0)) if $type ne 1; |
|---|
| 681 | return ($text) if $text !~ /\n/; |
|---|
| 682 | |
|---|
| 683 | $text =~ s/([^\n]*)\n//; |
|---|
| 684 | my @list = ($1); |
|---|
| 685 | my $str = ''; |
|---|
| 686 | for my $line (split (/\n/, $text)) |
|---|
| 687 | { |
|---|
| 688 | chomp $line; |
|---|
| 689 | if ($line =~ /^\.\s*$/) |
|---|
| 690 | { |
|---|
| 691 | # New paragraph |
|---|
| 692 | $str =~ s/\s*$//; |
|---|
| 693 | push(@list, $str); |
|---|
| 694 | $str = ''; |
|---|
| 695 | } |
|---|
| 696 | elsif ($line =~ /^\s/) |
|---|
| 697 | { |
|---|
| 698 | # Line which must not be reformatted |
|---|
| 699 | $str .= "\n" if length ($str) && $str !~ /\n$/; |
|---|
| 700 | $line =~ s/\s+$//; |
|---|
| 701 | $str .= $line."\n"; |
|---|
| 702 | } |
|---|
| 703 | else |
|---|
| 704 | { |
|---|
| 705 | # Continuation line, remove newline |
|---|
| 706 | $str .= " " if length ($str) && $str !~ /\n$/; |
|---|
| 707 | $str .= $line; |
|---|
| 708 | } |
|---|
| 709 | } |
|---|
| 710 | $str =~ s/\s*$//; |
|---|
| 711 | push(@list, $str) if length ($str); |
|---|
| 712 | return @list; |
|---|
| 713 | } |
|---|
| 714 | |
|---|
| 715 | sub type_quoted { |
|---|
| 716 | while ($input =~ /\"(([^\"]|\\\")*[^\\\"])\"/g) { |
|---|
| 717 | my $message = $1; |
|---|
| 718 | my $before = $`; |
|---|
| 719 | $message =~ s/\\\"/\"/g; |
|---|
| 720 | $before =~ s/[^\n]//g; |
|---|
| 721 | $messages{$message} = []; |
|---|
| 722 | $loc{$message} = length ($before) + 2; |
|---|
| 723 | } |
|---|
| 724 | } |
|---|
| 725 | |
|---|
| 726 | sub type_glade { |
|---|
| 727 | ### For translatable Glade XML files ### |
|---|
| 728 | |
|---|
| 729 | my $tags = "label|title|text|format|copyright|comments|preview_text|tooltip|message"; |
|---|
| 730 | |
|---|
| 731 | while ($input =~ /<($tags)>([^<]+)<\/($tags)>/sg) { |
|---|
| 732 | # Glade sometimes uses tags that normally mark translatable things for |
|---|
| 733 | # little bits of non-translatable content. We work around this by not |
|---|
| 734 | # translating strings that only includes something like label4 or window1. |
|---|
| 735 | $messages{entity_decode($2)} = [] unless $2 =~ /^(window|label|dialog)[0-9]+$/; |
|---|
| 736 | } |
|---|
| 737 | |
|---|
| 738 | while ($input =~ /<items>(..[^<]*)<\/items>/sg) { |
|---|
| 739 | for my $item (split (/\n/, $1)) { |
|---|
| 740 | $messages{entity_decode($item)} = []; |
|---|
| 741 | } |
|---|
| 742 | } |
|---|
| 743 | |
|---|
| 744 | ## handle new glade files |
|---|
| 745 | while ($input =~ /<(property|atkproperty)\s+[^>]*translatable\s*=\s*"yes"(?:\s+[^>]*comments\s*=\s*"([^"]*)")?[^>]*>([^<]+)<\/\1>/sg) { |
|---|
| 746 | $messages{entity_decode($3)} = [] unless $3 =~ /^(window|label)[0-9]+$/; |
|---|
| 747 | if (defined($2) and !($3 =~ /^(window|label)[0-9]+$/)) { |
|---|
| 748 | $comments{entity_decode($3)} = entity_decode($2) ; |
|---|
| 749 | } |
|---|
| 750 | } |
|---|
| 751 | while ($input =~ /<atkaction\s+action_name="([^>]*)"\s+description="([^>]+)"\/>/sg) { |
|---|
| 752 | $messages{entity_decode_minimal($2)} = []; |
|---|
| 753 | } |
|---|
| 754 | } |
|---|
| 755 | |
|---|
| 756 | sub type_scheme { |
|---|
| 757 | my ($line, $i, $state, $str, $trcomment, $char); |
|---|
| 758 | for $line (split(/\n/, $input)) { |
|---|
| 759 | $i = 0; |
|---|
| 760 | $state = 0; # 0 - nothing, 1 - string, 2 - translatable string |
|---|
| 761 | while ($i < length($line)) { |
|---|
| 762 | if (substr($line,$i,1) eq "\"") { |
|---|
| 763 | if ($state == 2) { |
|---|
| 764 | $comments{$str} = $trcomment if ($trcomment); |
|---|
| 765 | $messages{$str} = []; |
|---|
| 766 | $str = ''; |
|---|
| 767 | $state = 0; $trcomment = ""; |
|---|
| 768 | } elsif ($state == 1) { |
|---|
| 769 | $str = ''; |
|---|
| 770 | $state = 0; $trcomment = ""; |
|---|
| 771 | } else { |
|---|
| 772 | $state = 1; |
|---|
| 773 | $str = ''; |
|---|
| 774 | if ($i>0 && substr($line,$i-1,1) eq '_') { |
|---|
| 775 | $state = 2; |
|---|
| 776 | } |
|---|
| 777 | } |
|---|
| 778 | } elsif (!$state) { |
|---|
| 779 | if (substr($line,$i,1) eq ";") { |
|---|
| 780 | $trcomment = substr($line,$i+1); |
|---|
| 781 | $trcomment =~ s/^;*\s*//; |
|---|
| 782 | $i = length($line); |
|---|
| 783 | } elsif ($trcomment && substr($line,$i,1) !~ /\s|\(|\)|_/) { |
|---|
| 784 | $trcomment = ""; |
|---|
| 785 | } |
|---|
| 786 | } else { |
|---|
| 787 | if (substr($line,$i,1) eq "\\") { |
|---|
| 788 | $char = substr($line,$i+1,1); |
|---|
| 789 | if ($char ne "\"" && $char ne "\\") { |
|---|
| 790 | $str = $str . "\\"; |
|---|
| 791 | } |
|---|
| 792 | $i++; |
|---|
| 793 | } |
|---|
| 794 | $str = $str . substr($line,$i,1); |
|---|
| 795 | } |
|---|
| 796 | $i++; |
|---|
| 797 | } |
|---|
| 798 | } |
|---|
| 799 | } |
|---|
| 800 | |
|---|
| 801 | sub msg_write { |
|---|
| 802 | my @msgids; |
|---|
| 803 | if (%count) |
|---|
| 804 | { |
|---|
| 805 | @msgids = sort { $count{$a} <=> $count{$b} } keys %count; |
|---|
| 806 | } |
|---|
| 807 | else |
|---|
| 808 | { |
|---|
| 809 | @msgids = sort keys %messages; |
|---|
| 810 | } |
|---|
| 811 | for my $message (@msgids) |
|---|
| 812 | { |
|---|
| 813 | my $offsetlines = 1; |
|---|
| 814 | $offsetlines++ if $message =~ /%/; |
|---|
| 815 | if (defined ($comments{$message})) |
|---|
| 816 | { |
|---|
| 817 | while ($comments{$message} =~ m/\n/g) |
|---|
| 818 | { |
|---|
| 819 | $offsetlines++; |
|---|
| 820 | } |
|---|
| 821 | } |
|---|
| 822 | print OUT "# ".($loc{$message} - $offsetlines). " \"$FILE\"\n" |
|---|
| 823 | if defined $loc{$message}; |
|---|
| 824 | print OUT "/* ".$comments{$message}." */\n" |
|---|
| 825 | if defined $comments{$message}; |
|---|
| 826 | print OUT "/* xgettext:no-c-format */\n" if $message =~ /%/; |
|---|
| 827 | |
|---|
| 828 | my @lines = split (/\n/, $message, -1); |
|---|
| 829 | for (my $n = 0; $n < @lines; $n++) |
|---|
| 830 | { |
|---|
| 831 | if ($n == 0) |
|---|
| 832 | { |
|---|
| 833 | print OUT "char *s = N_(\""; |
|---|
| 834 | } |
|---|
| 835 | else |
|---|
| 836 | { |
|---|
| 837 | print OUT " \""; |
|---|
| 838 | } |
|---|
| 839 | |
|---|
| 840 | print OUT escape($lines[$n]); |
|---|
| 841 | |
|---|
| 842 | if ($n < @lines - 1) |
|---|
| 843 | { |
|---|
| 844 | print OUT "\\n\"\n"; |
|---|
| 845 | } |
|---|
| 846 | else |
|---|
| 847 | { |
|---|
| 848 | print OUT "\");\n"; |
|---|
| 849 | } |
|---|
| 850 | } |
|---|
| 851 | } |
|---|
| 852 | } |
|---|
| 853 | |
|---|