Changeset 8102 for trunk/gsdl/perllib


Ignore:
Timestamp:
2004-09-08T13:34:23+12:00 (20 years ago)
Author:
mdewsnip
Message:

Unfinished, but I'm committing it now so I don't lose it.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl/perllib/translator.pm

    r5276 r8102  
    5555sub read_languages_from_main_cfg_file
    5656{
    57     local %name2abbrhash = ();
     57    local %code2namehash = ();
    5858
    5959    # Read the main.cfg file
     
    6868    # Parse the lines containing a language definition
    6969    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;
    7978    }
    8079    }
    8180    close(MAIN_CFG_FILE);
    8281
    83     return %name2abbrhash;
     82    return %code2namehash;
    8483}
    8584
     
    10099    # Check if a file is being defined
    101100    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);
    105103        $name2deschash{$fileid} = $filedesc;
    106104    }
     
    114112sub read_file_data_from_config_file
    115113{
    116     my ($sourcelang, $sourceabbr, $targetlang, $targetabbr, $file) = @_;
     114    my ($sourceabbr, $targetabbr, $file) = @_;
    117115
    118116    # Read the translator config file
     
    137135        local ($filetype) = ($lineparts[5] =~ m/^type=(.*)$/);
    138136
    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}
    141142        $sourcefile =~ s/\{sourceabbr\}/$sourceabbr/g;
    142         $targetfile =~ s/\{targetlang\}/$targetlang/g;
     143        $sourcefile =~ s/\{sourcebase\}/$sourcebase/g;
    143144        $targetfile =~ s/\{targetabbr\}/$targetabbr/g;
     145        $targetfile =~ s/\{targetbase\}/$targetbase/g;
    144146
    145147        close(CONFIG_FILE);
     
    164166
    165167
     168sub 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
    166184sub process_file
    167185{
     
    185203    if ($filetype =~ /^resourcebundle/) {
    186204    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);
    187208    }
    188209
     
    232253    if ($filetype =~ /^resourcebundle/) {
    233254    return &import_chunk_from_resource_bundle($chunktext);
     255    }
     256    if ($filetype =~ /^myresourcebundle/) {
     257    return &import_chunk_from_my_resource_bundle($chunktext);
    234258    }
    235259
     
    323347# ==========================================================================================
    324348
     349sub 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
    325372sub build_key_to_line_mapping_for_macrofile
    326373{
     
    355402        # While there is still text of the macro to go...
    356403        local $startline = $i;
    357         while ($line !~ /.*\}/) {
     404        while ($line !~ /\}$/) {
    358405        $i++;
    359406        $line = $lines[$i];
     407        $line =~ s/(\s*)$//;  # Remove any nasty whitespace, carriage returns etc.
    360408        }
    361409
     
    373421        local $startline = $i;
    374422        unless ($line =~ m/^\#\# .*\#\#/) {
    375         $line = $lines[++$i];
     423        $i++;
     424        $line = $lines[$i];
    376425        $macroname .= $line;
    377426        }
     
    383432
    384433        # Read the rest of the text associated with the image macro
    385         while ($line !~ /^\s*$/) {
     434        while (defined($line) && $line !~ /^\s*$/) {
    386435        $i++;
    387436        $line = $lines[$i];
     
    404453
    405454    # Is this an icon macro??
    406     if ($chunktext =~ m/^\#\# (.*)/) {
     455    if ($chunktext =~ /^\#\# (.*)/) {
    407456    # Extract image macro text
    408     $chunktext =~ s/^\#\# ((.|\n)+) \#\# (.+) \#\# (.|\n)+/$1/;
     457    $chunktext =~ /^\#\#\s+([^\#]+)\s+\#\#/;
     458    $chunktext = $1;
    409459
    410460    # Remove enclosing quotes
    411     $chunktext =~ s/^\s*\"((.|\n)+)\"\s*/$1/;
     461    $chunktext =~ s/^\"//;
     462    $chunktext =~ s/\"$//;
    412463    }
    413464
     
    430481# ------------------------------------------------------------------------------------------
    431482
     483sub get_resource_bundle_base
     484{
     485    local ($langabbr) = @_;
     486
     487    return "";
     488}
     489
     490
    432491sub build_key_to_line_mapping_for_resource_bundle
    433492{
     
    451510
    452511    # Line contains a dictionary string
    453     if ($line =~ m/^((\w|\.)+):/) {
     512    if ($line =~ /^((\w|\.)+):(.*)$/) {
    454513        local $key = $1;
    455514
     
    474533
    475534
     535
     536# ------------------------------------------------------------------------------------------
     537
     538sub 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
     578sub 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
    4765911;
Note: See TracChangeset for help on using the changeset viewer.