Ignore:
Timestamp:
2005-06-16T14:46:08+12:00 (19 years ago)
Author:
mdewsnip
Message:

For macrofiles, now uses header from target file (if it exists) instead of source file. This means that the language and author information will stick around.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl/bin/script/gti.pl

    r10114 r10126  
    948948    my $target_language_code = shift(@_);
    949949
     950    # Build a mapping from source file line to chunk key
     951    my %source_file_key_to_line_mapping = &build_key_to_line_mapping_for_macrofile(@$source_file_lines);
     952    my %source_file_line_to_key_mapping = ();
     953    foreach my $chunk_key (keys(%source_file_key_to_line_mapping)) {
     954    $source_file_line_to_key_mapping{$source_file_key_to_line_mapping{$chunk_key}} = $chunk_key;
     955    }
     956    my @source_file_line_keys = (sort sort_by_line (keys(%source_file_line_to_key_mapping)));
     957    my $source_file_line_number = 0;
     958
     959    # Build a mapping from target file line to chunk key
     960    my %target_file_key_to_line_mapping = &build_key_to_line_mapping_for_macrofile(@$target_file_lines);
     961    my %target_file_line_to_key_mapping = ();
     962    foreach my $chunk_key (keys(%target_file_key_to_line_mapping)) {
     963    $target_file_line_to_key_mapping{$target_file_key_to_line_mapping{$chunk_key}} = $chunk_key;
     964    }
     965    my @target_file_line_keys = (sort sort_by_line (keys(%target_file_line_to_key_mapping)));
     966
     967    # Write the new target file
     968    my $target_file_path = &util::filename_cat($gsdl_root_directory, $target_file);
     969    if (!open(TARGET_FILE, ">$target_file_path")) {
     970    &throw_fatal_error("Could not write target file $target_file_path.");
     971    }
     972
     973    # Use the header from the target file, to keep language and author information
     974    if (scalar(@target_file_line_keys) > 0) {
     975    my $target_file_line_number = 0;
     976    my $target_file_chunk_starting_line_number = (split(/-/, $target_file_line_keys[0]))[0];
     977    while ($target_file_line_number < $target_file_chunk_starting_line_number) {
     978        print TARGET_FILE @$target_file_lines[$target_file_line_number];
     979        $target_file_line_number++;
     980    }
     981
     982    $source_file_line_number = (split(/-/, $source_file_line_keys[0]))[0];
     983    }
     984
     985    # Model the new target file on the source file, with the target file translations
     986    foreach my $line_key (@source_file_line_keys) {
     987    # Fill in the gaps before this chunk starts
     988    my $source_file_chunk_starting_line_number = (split(/-/, $line_key))[0];
     989    my $source_file_chunk_finishing_line_number = (split(/-/, $line_key))[1];
     990    while ($source_file_line_number < $source_file_chunk_starting_line_number) {
     991        print TARGET_FILE @$source_file_lines[$source_file_line_number];
     992        $source_file_line_number++;
     993    }
     994    $source_file_line_number = $source_file_chunk_finishing_line_number + 1;
     995
     996    my $chunk_key = $source_file_line_to_key_mapping{$line_key};
     997    my $source_file_chunk_text = $source_file_key_to_text_mapping->{$chunk_key};
     998    my $target_file_chunk_text = $target_file_key_to_text_mapping->{$chunk_key} || "";
     999
     1000    my $macrofile_key = $chunk_key;
     1001    $macrofile_key =~ s/^(.+?)\.//;
     1002
     1003    # If no translation exists for this chunk, show this, and move on
     1004    if ($source_file_chunk_text ne "" && $target_file_chunk_text eq "") {
     1005        print TARGET_FILE "# -- Missing translation: $macrofile_key\n";
     1006        next;
     1007    }
     1008
     1009    # Grab the source chunk text
     1010    my $source_file_chunk = @$source_file_lines[$source_file_chunk_starting_line_number];
     1011    for (my $l = ($source_file_chunk_starting_line_number + 1); $l <= $source_file_chunk_finishing_line_number; $l++) {
     1012        $source_file_chunk .= @$source_file_lines[$l];
     1013    }
     1014
     1015    # Is this an icon macro??
     1016    if ($source_file_chunk =~ /^\#\# (.*)/) {
     1017        # Escape any newline and question mark characters so the source text is replaced correctly
     1018        $source_file_chunk_text =~ s/\\/\\\\/g;
     1019        $source_file_chunk_text =~ s/\?/\\\?/g;
     1020
     1021        # Build the new target chunk from the source chunk
     1022        my $target_file_chunk = $source_file_chunk;
     1023        $target_file_chunk =~ s/$source_file_chunk_text/$target_file_chunk_text/;
     1024        $target_file_chunk =~ s/(\s)*$//;
     1025        print TARGET_FILE "$target_file_chunk";
     1026    }
     1027
     1028    # No, it is just a normal text macro
     1029    else {
     1030        print TARGET_FILE "$macrofile_key [l=$target_language_code] {$target_file_chunk_text}";
     1031    }
     1032
     1033    # Add the update date, if one exists
     1034    if ($target_file_key_to_comment_date_mapping->{$chunk_key}) {
     1035        print TARGET_FILE "  # Updated " . $target_file_key_to_comment_date_mapping->{$chunk_key};
     1036    }
     1037    print TARGET_FILE "\n";
     1038    }
     1039
     1040    close(TARGET_FILE);
     1041}
     1042
     1043
     1044sub sort_by_line
     1045{
     1046    return ((split(/-/, $a))[0] <=> (split(/-/, $b))[0]);
     1047}
     1048
     1049
     1050# ==========================================================================================
     1051#   RESOURCE BUNDLE FUNCTIONS
     1052
     1053sub build_key_to_line_mapping_for_resource_bundle
     1054{
     1055    my (@file_lines) = @_;
     1056
     1057    my %key_to_line_mapping = ();
     1058    for (my $i = 0; $i < scalar(@file_lines); $i++) {
     1059    my $line = $file_lines[$i];
     1060    $line =~ s/(\s*)$//;  # Remove any nasty whitespace, carriage returns etc.
     1061
     1062    # Line contains a dictionary string
     1063    if ($line =~ /^(\S+?):(.*)$/) {
     1064        my $chunk_key = $1;
     1065
     1066        # Map from chunk key to line
     1067        $key_to_line_mapping{$chunk_key} = $i . "-" . $i;
     1068    }
     1069    }
     1070
     1071    return %key_to_line_mapping;
     1072}
     1073
     1074
     1075sub import_chunk_from_resource_bundle
     1076{
     1077    my ($chunk_text) = @_;
     1078
     1079    # Simple: just remove string key
     1080    $chunk_text =~ s/^(\S+?)://;
     1081    $chunk_text =~ s/(\s*)$//;  # Remove any nasty whitespace, carriage returns etc.
     1082
     1083    return $chunk_text;
     1084}
     1085
     1086
     1087sub get_resource_bundle_chunk_comment_date
     1088{
     1089    my ($chunk_text) = @_;
     1090
     1091    # Check for an "Updated DD-MMM-YYYY" comment at the end of the chunk
     1092    if ($chunk_text =~ /\#\s+Updated\s+(\d?\d-\D\D\D-\d\d\d\d)\s*$/i) {
     1093    return $1;
     1094    }
     1095
     1096    return undef;
     1097}
     1098
     1099
     1100sub is_resource_bundle_chunk_automatically_translated
     1101{
     1102    # No resource bundle chunks are automatically translated
     1103    return 0;
     1104}
     1105
     1106
     1107sub write_translated_resource_bundle
     1108{
     1109    my $source_file = shift(@_);  # Not used
     1110    my $source_file_lines = shift(@_);
     1111    my $source_file_key_to_text_mapping = shift(@_);
     1112    my $target_file = shift(@_);
     1113    my $target_file_lines = shift(@_);  # Not used
     1114    my $target_file_key_to_text_mapping = shift(@_);
     1115    my $target_file_key_to_comment_date_mapping = shift(@_);
     1116    my $target_language_code = shift(@_);  # Not used
     1117
    9501118    # Build a mapping from chunk key to source file line, and from source file line to chunk key
    951     my %source_file_key_to_line_mapping = &build_key_to_line_mapping_for_macrofile(@$source_file_lines);
     1119    my %source_file_key_to_line_mapping = &build_key_to_line_mapping_for_resource_bundle(@$source_file_lines);
    9521120    my %source_file_line_to_key_mapping = ();
    9531121    foreach my $chunk_key (keys(%source_file_key_to_line_mapping)) {
     
    9771145    my $target_file_chunk_text = $target_file_key_to_text_mapping->{$chunk_key} || "";
    9781146
    979     my $macrofile_key = $chunk_key;
    980     $macrofile_key =~ s/^(.+?)\.//;
    981 
    982     # If no translation exists for this chunk, show this, and move on
    983     if ($source_file_chunk_text ne "" && $target_file_chunk_text eq "") {
    984         print TARGET_FILE "# -- Missing translation: $macrofile_key\n";
    985         next;
    986     }
    987 
    988     # Grab the source chunk text
    989     my $source_file_chunk = @$source_file_lines[$source_file_chunk_starting_line_number];
    990     for (my $l = ($source_file_chunk_starting_line_number + 1); $l <= $source_file_chunk_finishing_line_number; $l++) {
    991         $source_file_chunk .= @$source_file_lines[$l];
    992     }
    993 
    994     # Is this an icon macro??
    995     if ($source_file_chunk =~ /^\#\# (.*)/) {
    996         # Escape any newline and question mark characters so the source text is replaced correctly
    997         $source_file_chunk_text =~ s/\\/\\\\/g;
    998         $source_file_chunk_text =~ s/\?/\\\?/g;
    999 
    1000         # Build the new target chunk from the source chunk
    1001         my $target_file_chunk = $source_file_chunk;
    1002         $target_file_chunk =~ s/$source_file_chunk_text/$target_file_chunk_text/;
    1003         $target_file_chunk =~ s/(\s)*$//;
    1004         print TARGET_FILE "$target_file_chunk";
    1005     }
    1006 
    1007     # No, it is just a normal text macro
    1008     else {
    1009         print TARGET_FILE "$macrofile_key [l=$target_language_code] {$target_file_chunk_text}";
    1010     }
    1011 
    1012     # Add the update date, if one exists
    1013     if ($target_file_key_to_comment_date_mapping->{$chunk_key}) {
    1014         print TARGET_FILE "  # Updated " . $target_file_key_to_comment_date_mapping->{$chunk_key};
    1015     }
    1016     print TARGET_FILE "\n";
    1017     }
    1018 
    1019     close(TARGET_FILE);
    1020 }
    1021 
    1022 
    1023 sub sort_by_line
    1024 {
    1025     return ((split(/-/, $a))[0] <=> (split(/-/, $b))[0]);
    1026 }
    1027 
    1028 
    1029 # ==========================================================================================
    1030 #   RESOURCE BUNDLE FUNCTIONS
    1031 
    1032 sub build_key_to_line_mapping_for_resource_bundle
    1033 {
    1034     my (@file_lines) = @_;
    1035 
    1036     my %key_to_line_mapping = ();
    1037     for (my $i = 0; $i < scalar(@file_lines); $i++) {
    1038     my $line = $file_lines[$i];
    1039     $line =~ s/(\s*)$//;  # Remove any nasty whitespace, carriage returns etc.
    1040 
    1041     # Line contains a dictionary string
    1042     if ($line =~ /^(\S+?):(.*)$/) {
    1043         my $chunk_key = $1;
    1044 
    1045         # Map from chunk key to line
    1046         $key_to_line_mapping{$chunk_key} = $i . "-" . $i;
    1047     }
    1048     }
    1049 
    1050     return %key_to_line_mapping;
    1051 }
    1052 
    1053 
    1054 sub import_chunk_from_resource_bundle
    1055 {
    1056     my ($chunk_text) = @_;
    1057 
    1058     # Simple: just remove string key
    1059     $chunk_text =~ s/^(\S+?)://;
    1060     $chunk_text =~ s/(\s*)$//;  # Remove any nasty whitespace, carriage returns etc.
    1061 
    1062     return $chunk_text;
    1063 }
    1064 
    1065 
    1066 sub get_resource_bundle_chunk_comment_date
    1067 {
    1068     my ($chunk_text) = @_;
    1069 
    1070     # Check for an "Updated DD-MMM-YYYY" comment at the end of the chunk
    1071     if ($chunk_text =~ /\#\s+Updated\s+(\d?\d-\D\D\D-\d\d\d\d)\s*$/i) {
    1072     return $1;
    1073     }
    1074 
    1075     return undef;
    1076 }
    1077 
    1078 
    1079 sub is_resource_bundle_chunk_automatically_translated
    1080 {
    1081     # No resource bundle chunks are automatically translated
    1082     return 0;
    1083 }
    1084 
    1085 
    1086 sub write_translated_resource_bundle
    1087 {
    1088     my $source_file = shift(@_);  # Not used
    1089     my $source_file_lines = shift(@_);
    1090     my $source_file_key_to_text_mapping = shift(@_);
    1091     my $target_file = shift(@_);
    1092     my $target_file_lines = shift(@_);  # Not used
    1093     my $target_file_key_to_text_mapping = shift(@_);
    1094     my $target_file_key_to_comment_date_mapping = shift(@_);
    1095     my $target_language_code = shift(@_);  # Not used
    1096 
    1097     # Build a mapping from chunk key to source file line, and from source file line to chunk key
    1098     my %source_file_key_to_line_mapping = &build_key_to_line_mapping_for_resource_bundle(@$source_file_lines);
    1099     my %source_file_line_to_key_mapping = ();
    1100     foreach my $chunk_key (keys(%source_file_key_to_line_mapping)) {
    1101     $source_file_line_to_key_mapping{$source_file_key_to_line_mapping{$chunk_key}} = $chunk_key;
    1102     }
    1103 
    1104     # Write the new target file
    1105     my $target_file_path = &util::filename_cat($gsdl_root_directory, $target_file);
    1106     if (!open(TARGET_FILE, ">$target_file_path")) {
    1107     &throw_fatal_error("Could not write target file $target_file_path.");
    1108     }
    1109 
    1110     # Model the new target file on the source file, with the target file translations
    1111     my $source_file_line_number = 0;
    1112     foreach my $line_key (sort sort_by_line (keys(%source_file_line_to_key_mapping))) {
    1113     # Fill in the gaps before this chunk starts
    1114     my $source_file_chunk_starting_line_number = (split(/-/, $line_key))[0];
    1115     my $source_file_chunk_finishing_line_number = (split(/-/, $line_key))[1];
    1116     while ($source_file_line_number < $source_file_chunk_starting_line_number) {
    1117         print TARGET_FILE @$source_file_lines[$source_file_line_number];
    1118         $source_file_line_number++;
    1119     }
    1120     $source_file_line_number = $source_file_chunk_finishing_line_number + 1;
    1121 
    1122     my $chunk_key = $source_file_line_to_key_mapping{$line_key};
    1123     my $source_file_chunk_text = $source_file_key_to_text_mapping->{$chunk_key};
    1124     my $target_file_chunk_text = $target_file_key_to_text_mapping->{$chunk_key} || "";
    1125 
    11261147    # If no translation exists for this chunk, show this, and move on
    11271148    if ($source_file_chunk_text ne "" && $target_file_chunk_text eq "") {
Note: See TracChangeset for help on using the changeset viewer.