Index: trunk/gsdl/bin/script/gti.pl
===================================================================
--- trunk/gsdl/bin/script/gti.pl	(revision 10114)
+++ trunk/gsdl/bin/script/gti.pl	(revision 10126)
@@ -948,6 +948,174 @@
     my $target_language_code = shift(@_);
 
+    # Build a mapping from source file line to chunk key
+    my %source_file_key_to_line_mapping = &build_key_to_line_mapping_for_macrofile(@$source_file_lines);
+    my %source_file_line_to_key_mapping = ();
+    foreach my $chunk_key (keys(%source_file_key_to_line_mapping)) {
+	$source_file_line_to_key_mapping{$source_file_key_to_line_mapping{$chunk_key}} = $chunk_key;
+    }
+    my @source_file_line_keys = (sort sort_by_line (keys(%source_file_line_to_key_mapping)));
+    my $source_file_line_number = 0;
+
+    # Build a mapping from target file line to chunk key
+    my %target_file_key_to_line_mapping = &build_key_to_line_mapping_for_macrofile(@$target_file_lines);
+    my %target_file_line_to_key_mapping = ();
+    foreach my $chunk_key (keys(%target_file_key_to_line_mapping)) {
+	$target_file_line_to_key_mapping{$target_file_key_to_line_mapping{$chunk_key}} = $chunk_key;
+    }
+    my @target_file_line_keys = (sort sort_by_line (keys(%target_file_line_to_key_mapping)));
+
+    # Write the new target file
+    my $target_file_path = &util::filename_cat($gsdl_root_directory, $target_file);
+    if (!open(TARGET_FILE, ">$target_file_path")) {
+	&throw_fatal_error("Could not write target file $target_file_path.");
+    }
+
+    # Use the header from the target file, to keep language and author information
+    if (scalar(@target_file_line_keys) > 0) {
+	my $target_file_line_number = 0;
+ 	my $target_file_chunk_starting_line_number = (split(/-/, $target_file_line_keys[0]))[0];
+ 	while ($target_file_line_number < $target_file_chunk_starting_line_number) {
+ 	    print TARGET_FILE @$target_file_lines[$target_file_line_number];
+ 	    $target_file_line_number++;
+ 	}
+
+	$source_file_line_number = (split(/-/, $source_file_line_keys[0]))[0];
+    }
+
+    # Model the new target file on the source file, with the target file translations
+    foreach my $line_key (@source_file_line_keys) {
+ 	# Fill in the gaps before this chunk starts
+ 	my $source_file_chunk_starting_line_number = (split(/-/, $line_key))[0];
+ 	my $source_file_chunk_finishing_line_number = (split(/-/, $line_key))[1];
+ 	while ($source_file_line_number < $source_file_chunk_starting_line_number) {
+ 	    print TARGET_FILE @$source_file_lines[$source_file_line_number];
+ 	    $source_file_line_number++;
+ 	}
+ 	$source_file_line_number = $source_file_chunk_finishing_line_number + 1;
+
+	my $chunk_key = $source_file_line_to_key_mapping{$line_key};
+	my $source_file_chunk_text = $source_file_key_to_text_mapping->{$chunk_key};
+	my $target_file_chunk_text = $target_file_key_to_text_mapping->{$chunk_key} || "";
+
+	my $macrofile_key = $chunk_key;
+	$macrofile_key =~ s/^(.+?)\.//;
+
+	# If no translation exists for this chunk, show this, and move on
+	if ($source_file_chunk_text ne "" && $target_file_chunk_text eq "") {
+	    print TARGET_FILE "# -- Missing translation: $macrofile_key\n";
+	    next;
+	}
+
+ 	# Grab the source chunk text
+ 	my $source_file_chunk = @$source_file_lines[$source_file_chunk_starting_line_number];
+ 	for (my $l = ($source_file_chunk_starting_line_number + 1); $l <= $source_file_chunk_finishing_line_number; $l++) {
+ 	    $source_file_chunk .= @$source_file_lines[$l];
+ 	}
+
+ 	# Is this an icon macro??
+ 	if ($source_file_chunk =~ /^\#\# (.*)/) {
+ 	    # Escape any newline and question mark characters so the source text is replaced correctly
+ 	    $source_file_chunk_text =~ s/\\/\\\\/g;
+	    $source_file_chunk_text =~ s/\?/\\\?/g;
+
+	    # Build the new target chunk from the source chunk
+	    my $target_file_chunk = $source_file_chunk;
+	    $target_file_chunk =~ s/$source_file_chunk_text/$target_file_chunk_text/;
+	    $target_file_chunk =~ s/(\s)*$//;
+	    print TARGET_FILE "$target_file_chunk";
+ 	}
+
+ 	# No, it is just a normal text macro
+ 	else {
+ 	    print TARGET_FILE "$macrofile_key [l=$target_language_code] {$target_file_chunk_text}";
+ 	}
+
+	# Add the update date, if one exists
+	if ($target_file_key_to_comment_date_mapping->{$chunk_key}) {
+	    print TARGET_FILE "  # Updated " . $target_file_key_to_comment_date_mapping->{$chunk_key};
+	}
+	print TARGET_FILE "\n";
+    }
+
+    close(TARGET_FILE);
+}
+
+
+sub sort_by_line
+{
+    return ((split(/-/, $a))[0] <=> (split(/-/, $b))[0]);
+}
+
+
+# ==========================================================================================
+#   RESOURCE BUNDLE FUNCTIONS
+
+sub build_key_to_line_mapping_for_resource_bundle
+{
+    my (@file_lines) = @_;
+
+    my %key_to_line_mapping = ();
+    for (my $i = 0; $i < scalar(@file_lines); $i++) {
+	my $line = $file_lines[$i];
+	$line =~ s/(\s*)$//;  # Remove any nasty whitespace, carriage returns etc.
+
+	# Line contains a dictionary string
+	if ($line =~ /^(\S+?):(.*)$/) {
+	    my $chunk_key = $1;
+
+	    # Map from chunk key to line
+	    $key_to_line_mapping{$chunk_key} = $i . "-" . $i;
+	}
+    }
+
+    return %key_to_line_mapping;
+}
+
+
+sub import_chunk_from_resource_bundle
+{
+    my ($chunk_text) = @_;
+
+    # Simple: just remove string key
+    $chunk_text =~ s/^(\S+?)://;
+    $chunk_text =~ s/(\s*)$//;  # Remove any nasty whitespace, carriage returns etc.
+
+    return $chunk_text;
+}
+
+
+sub get_resource_bundle_chunk_comment_date
+{
+    my ($chunk_text) = @_;
+
+    # Check for an "Updated DD-MMM-YYYY" comment at the end of the chunk
+    if ($chunk_text =~ /\#\s+Updated\s+(\d?\d-\D\D\D-\d\d\d\d)\s*$/i) {
+	return $1;
+    }
+
+    return undef;
+}
+
+
+sub is_resource_bundle_chunk_automatically_translated
+{
+    # No resource bundle chunks are automatically translated
+    return 0;
+}
+
+
+sub write_translated_resource_bundle
+{
+    my $source_file = shift(@_);  # Not used
+    my $source_file_lines = shift(@_);
+    my $source_file_key_to_text_mapping = shift(@_);
+    my $target_file = shift(@_);
+    my $target_file_lines = shift(@_);  # Not used
+    my $target_file_key_to_text_mapping = shift(@_);
+    my $target_file_key_to_comment_date_mapping = shift(@_);
+    my $target_language_code = shift(@_);  # Not used
+
     # Build a mapping from chunk key to source file line, and from source file line to chunk key
-    my %source_file_key_to_line_mapping = &build_key_to_line_mapping_for_macrofile(@$source_file_lines);
+    my %source_file_key_to_line_mapping = &build_key_to_line_mapping_for_resource_bundle(@$source_file_lines);
     my %source_file_line_to_key_mapping = ();
     foreach my $chunk_key (keys(%source_file_key_to_line_mapping)) {
@@ -977,151 +1145,4 @@
 	my $target_file_chunk_text = $target_file_key_to_text_mapping->{$chunk_key} || "";
 
-	my $macrofile_key = $chunk_key;
-	$macrofile_key =~ s/^(.+?)\.//;
-
-	# If no translation exists for this chunk, show this, and move on
-	if ($source_file_chunk_text ne "" && $target_file_chunk_text eq "") {
-	    print TARGET_FILE "# -- Missing translation: $macrofile_key\n";
-	    next;
-	}
-
- 	# Grab the source chunk text
- 	my $source_file_chunk = @$source_file_lines[$source_file_chunk_starting_line_number];
- 	for (my $l = ($source_file_chunk_starting_line_number + 1); $l <= $source_file_chunk_finishing_line_number; $l++) {
- 	    $source_file_chunk .= @$source_file_lines[$l];
- 	}
-
- 	# Is this an icon macro??
- 	if ($source_file_chunk =~ /^\#\# (.*)/) {
- 	    # Escape any newline and question mark characters so the source text is replaced correctly
- 	    $source_file_chunk_text =~ s/\\/\\\\/g;
-	    $source_file_chunk_text =~ s/\?/\\\?/g;
-
-	    # Build the new target chunk from the source chunk
-	    my $target_file_chunk = $source_file_chunk;
-	    $target_file_chunk =~ s/$source_file_chunk_text/$target_file_chunk_text/;
-	    $target_file_chunk =~ s/(\s)*$//;
-	    print TARGET_FILE "$target_file_chunk";
- 	}
-
- 	# No, it is just a normal text macro
- 	else {
- 	    print TARGET_FILE "$macrofile_key [l=$target_language_code] {$target_file_chunk_text}";
- 	}
-
-	# Add the update date, if one exists
-	if ($target_file_key_to_comment_date_mapping->{$chunk_key}) {
-	    print TARGET_FILE "  # Updated " . $target_file_key_to_comment_date_mapping->{$chunk_key};
-	}
-	print TARGET_FILE "\n";
-    }
-
-    close(TARGET_FILE);
-}
-
-
-sub sort_by_line
-{
-    return ((split(/-/, $a))[0] <=> (split(/-/, $b))[0]);
-}
-
-
-# ==========================================================================================
-#   RESOURCE BUNDLE FUNCTIONS
-
-sub build_key_to_line_mapping_for_resource_bundle
-{
-    my (@file_lines) = @_;
-
-    my %key_to_line_mapping = ();
-    for (my $i = 0; $i < scalar(@file_lines); $i++) {
-	my $line = $file_lines[$i];
-	$line =~ s/(\s*)$//;  # Remove any nasty whitespace, carriage returns etc.
-
-	# Line contains a dictionary string
-	if ($line =~ /^(\S+?):(.*)$/) {
-	    my $chunk_key = $1;
-
-	    # Map from chunk key to line
-	    $key_to_line_mapping{$chunk_key} = $i . "-" . $i;
-	}
-    }
-
-    return %key_to_line_mapping;
-}
-
-
-sub import_chunk_from_resource_bundle
-{
-    my ($chunk_text) = @_;
-
-    # Simple: just remove string key
-    $chunk_text =~ s/^(\S+?)://;
-    $chunk_text =~ s/(\s*)$//;  # Remove any nasty whitespace, carriage returns etc.
-
-    return $chunk_text;
-}
-
-
-sub get_resource_bundle_chunk_comment_date
-{
-    my ($chunk_text) = @_;
-
-    # Check for an "Updated DD-MMM-YYYY" comment at the end of the chunk
-    if ($chunk_text =~ /\#\s+Updated\s+(\d?\d-\D\D\D-\d\d\d\d)\s*$/i) {
-	return $1;
-    }
-
-    return undef;
-}
-
-
-sub is_resource_bundle_chunk_automatically_translated
-{
-    # No resource bundle chunks are automatically translated
-    return 0;
-}
-
-
-sub write_translated_resource_bundle
-{
-    my $source_file = shift(@_);  # Not used
-    my $source_file_lines = shift(@_);
-    my $source_file_key_to_text_mapping = shift(@_);
-    my $target_file = shift(@_);
-    my $target_file_lines = shift(@_);  # Not used
-    my $target_file_key_to_text_mapping = shift(@_);
-    my $target_file_key_to_comment_date_mapping = shift(@_);
-    my $target_language_code = shift(@_);  # Not used
-
-    # Build a mapping from chunk key to source file line, and from source file line to chunk key
-    my %source_file_key_to_line_mapping = &build_key_to_line_mapping_for_resource_bundle(@$source_file_lines);
-    my %source_file_line_to_key_mapping = ();
-    foreach my $chunk_key (keys(%source_file_key_to_line_mapping)) {
-	$source_file_line_to_key_mapping{$source_file_key_to_line_mapping{$chunk_key}} = $chunk_key;
-    }
-
-    # Write the new target file
-    my $target_file_path = &util::filename_cat($gsdl_root_directory, $target_file);
-    if (!open(TARGET_FILE, ">$target_file_path")) {
-	&throw_fatal_error("Could not write target file $target_file_path.");
-    }
-
-    # Model the new target file on the source file, with the target file translations
-    my $source_file_line_number = 0;
-    foreach my $line_key (sort sort_by_line (keys(%source_file_line_to_key_mapping))) {
- 	# Fill in the gaps before this chunk starts
- 	my $source_file_chunk_starting_line_number = (split(/-/, $line_key))[0];
- 	my $source_file_chunk_finishing_line_number = (split(/-/, $line_key))[1];
- 	while ($source_file_line_number < $source_file_chunk_starting_line_number) {
- 	    print TARGET_FILE @$source_file_lines[$source_file_line_number];
- 	    $source_file_line_number++;
- 	}
- 	$source_file_line_number = $source_file_chunk_finishing_line_number + 1;
-
-	my $chunk_key = $source_file_line_to_key_mapping{$line_key};
-	my $source_file_chunk_text = $source_file_key_to_text_mapping->{$chunk_key};
-	my $target_file_chunk_text = $target_file_key_to_text_mapping->{$chunk_key} || "";
-
 	# If no translation exists for this chunk, show this, and move on
 	if ($source_file_chunk_text ne "" && $target_file_chunk_text eq "") {
