Changeset 8102 for trunk/gsdl/perllib
- Timestamp:
- 2004-09-08T13:34:23+12:00 (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gsdl/perllib/translator.pm
r5276 r8102 55 55 sub read_languages_from_main_cfg_file 56 56 { 57 local % name2abbrhash = ();57 local %code2namehash = (); 58 58 59 59 # Read the main.cfg file … … 68 68 # Parse the lines containing a language definition 69 69 if ($line =~ m/^Language\s+/i) { 70 local @lineparts = split(/\s+/, $line); 71 local ($langabbr) = ($lineparts[1] =~ m/^shortname=(.*)$/); 72 local ($langname) = ($lineparts[2] =~ m/^longname=(.*)$/); 73 74 # Casefold language name and code 75 $langabbr =~ tr/A-Z/a-z/; 76 $langname =~ tr/A-Z/a-z/; 77 78 $name2abbrhash{$langname} = $langabbr; 70 local ($langcode) = ($line =~ /shortname=(\S+)/i); 71 local ($langname) = ($line =~ /longname=(\"[^\"]+\"|\S+)/i); 72 73 # Casefold the language code, and remove any quotes around the language name 74 $langcode =~ tr/A-Z/a-z/; 75 $langname =~ s/^\"(.+)\"$/$1/; 76 77 $code2namehash{$langcode} = $langname; 79 78 } 80 79 } 81 80 close(MAIN_CFG_FILE); 82 81 83 return % name2abbrhash;82 return %code2namehash; 84 83 } 85 84 … … 100 99 # Check if a file is being defined 101 100 if ($line =~ m/^File\s+/i) { 102 local @lineparts = split(/\s+/, $line); 103 local ($fileid) = ($lineparts[1] =~ m/^id=(.*)$/i); 104 local ($filedesc) = ($lineparts[2] =~ m/^description=(.*)$/i); 101 local ($fileid) = ($line =~ /id=(\S+)/i); 102 local ($filedesc) = ($line =~ /description=(\S+)/i); 105 103 $name2deschash{$fileid} = $filedesc; 106 104 } … … 114 112 sub read_file_data_from_config_file 115 113 { 116 my ($source lang, $sourceabbr, $targetlang, $targetabbr, $file) = @_;114 my ($sourceabbr, $targetabbr, $file) = @_; 117 115 118 116 # Read the translator config file … … 137 135 local ($filetype) = ($lineparts[5] =~ m/^type=(.*)$/); 138 136 139 # Resolve instances of {sourcelang}, {sourceabbr}, {targetlang}, {targetabbr} 140 $sourcefile =~ s/\{sourcelang\}/$sourcelang/g; 137 # Determine sourcebase and targetbase, using the filetype 138 local $sourcebase = &get_file_base($sourceabbr, $filetype); 139 local $targetbase = &get_file_base($targetabbr, $filetype); 140 141 # Resolve instances of {sourceabbr}, {sourcebase}, {targetabbr}, {targetbase} 141 142 $sourcefile =~ s/\{sourceabbr\}/$sourceabbr/g; 142 $targetfile =~ s/\{targetlang\}/$targetlang/g;143 $sourcefile =~ s/\{sourcebase\}/$sourcebase/g; 143 144 $targetfile =~ s/\{targetabbr\}/$targetabbr/g; 145 $targetfile =~ s/\{targetbase\}/$targetbase/g; 144 146 145 147 close(CONFIG_FILE); … … 164 166 165 167 168 sub get_file_base 169 { 170 my ($langabbr, $filetype) = @_; 171 172 if ($filetype =~ /^macrofile/) { 173 return &get_macrofile_base($langabbr); 174 } 175 if ($filetype =~ /^resourcebundle/ || $filetype =~ /^myresourcebundle/) { 176 return &get_resource_bundle_base($langabbr); 177 } 178 179 print STDERR "Error: Unknown file type $filetype.\n"; 180 die "\n"; 181 } 182 183 166 184 sub process_file 167 185 { … … 185 203 if ($filetype =~ /^resourcebundle/) { 186 204 return &build_key_to_line_mapping_for_resource_bundle($filepath); 205 } 206 if ($filetype =~ /^myresourcebundle/) { 207 return &build_key_to_line_mapping_for_my_resource_bundle($filepath); 187 208 } 188 209 … … 232 253 if ($filetype =~ /^resourcebundle/) { 233 254 return &import_chunk_from_resource_bundle($chunktext); 255 } 256 if ($filetype =~ /^myresourcebundle/) { 257 return &import_chunk_from_my_resource_bundle($chunktext); 234 258 } 235 259 … … 323 347 # ========================================================================================== 324 348 349 sub get_macrofile_base 350 { 351 local ($langcode) = @_; 352 353 # List special cases here (macrofile names differ from language longnames) 354 local %specialcases = ('id', 'indo', 355 'pt-br', 'port-br', 356 'pt-pt', 'port-pt', 357 'zh', 'chinese' ); 358 359 # Special cases override main.cfg entries 360 if ($specialcases{$langcode}) { 361 return $specialcases{$langcode}; 362 } 363 364 # Use the language name defined in the main.cfg file as the macrofile name 365 my %langhash = &read_languages_from_main_cfg_file(); 366 my $langname = $langhash{$langcode} || die "Error: Language to base mapping undefined!\n"; 367 $langname =~ tr/A-Z/a-z/; 368 return $langname; 369 } 370 371 325 372 sub build_key_to_line_mapping_for_macrofile 326 373 { … … 355 402 # While there is still text of the macro to go... 356 403 local $startline = $i; 357 while ($line !~ / .*\}/) {404 while ($line !~ /\}$/) { 358 405 $i++; 359 406 $line = $lines[$i]; 407 $line =~ s/(\s*)$//; # Remove any nasty whitespace, carriage returns etc. 360 408 } 361 409 … … 373 421 local $startline = $i; 374 422 unless ($line =~ m/^\#\# .*\#\#/) { 375 $line = $lines[++$i]; 423 $i++; 424 $line = $lines[$i]; 376 425 $macroname .= $line; 377 426 } … … 383 432 384 433 # Read the rest of the text associated with the image macro 385 while ( $line !~ /^\s*$/) {434 while (defined($line) && $line !~ /^\s*$/) { 386 435 $i++; 387 436 $line = $lines[$i]; … … 404 453 405 454 # Is this an icon macro?? 406 if ($chunktext =~ m/^\#\# (.*)/) {455 if ($chunktext =~ /^\#\# (.*)/) { 407 456 # Extract image macro text 408 $chunktext =~ s/^\#\# ((.|\n)+) \#\# (.+) \#\# (.|\n)+/$1/; 457 $chunktext =~ /^\#\#\s+([^\#]+)\s+\#\#/; 458 $chunktext = $1; 409 459 410 460 # Remove enclosing quotes 411 $chunktext =~ s/^\s*\"((.|\n)+)\"\s*/$1/; 461 $chunktext =~ s/^\"//; 462 $chunktext =~ s/\"$//; 412 463 } 413 464 … … 430 481 # ------------------------------------------------------------------------------------------ 431 482 483 sub get_resource_bundle_base 484 { 485 local ($langabbr) = @_; 486 487 return ""; 488 } 489 490 432 491 sub build_key_to_line_mapping_for_resource_bundle 433 492 { … … 451 510 452 511 # Line contains a dictionary string 453 if ($line =~ m/^((\w|\.)+):/) {512 if ($line =~ /^((\w|\.)+):(.*)$/) { 454 513 local $key = $1; 455 514 … … 474 533 475 534 535 536 # ------------------------------------------------------------------------------------------ 537 538 sub build_key_to_line_mapping_for_my_resource_bundle 539 { 540 local ($filepath) = @_; 541 542 # Open file for reading 543 if (!open(FILE_IN, "<$filepath")) { 544 print STDERR "Error: Could not open resource bundle $filepath.\n"; 545 die "\n"; 546 } 547 local @lines = <FILE_IN>; 548 close FILE_IN; 549 550 # Initialise some local variables 551 local %linehash = (); 552 553 # Process the contents of the file, line by line 554 for ($i = 0; $i < scalar(@lines); $i++) { 555 local $line = $lines[$i]; 556 $line =~ s/(\s*)$//; # Remove any nasty whitespace, carriage returns etc. 557 558 # Line contains a dictionary string 559 if ($line =~ /^((\w|\.|\-)+):\"(.*)$/) { 560 local $key = $1; 561 local $text = $3; 562 563 # While there is still text of the chunk to go... 564 local $startline = $i; 565 while ($text !~ /\"$/ || $text =~ /\\\"$/) { 566 $i++; 567 $text = $lines[$i]; 568 } 569 570 $linehash{$key} = $startline . "-" . $i; 571 } 572 } 573 574 return %linehash; 575 } 576 577 578 sub import_chunk_from_my_resource_bundle 579 { 580 local ($chunktext) = @_; 581 582 # Remove chunk key and quotes 583 $chunktext =~ s/^((\w|\.|\-)+)://; 584 $chunktext =~ s/^\"//; 585 $chunktext =~ s/\"$//; 586 587 return $chunktext; 588 } 589 590 476 591 1;
Note:
See TracChangeset
for help on using the changeset viewer.