[10019] | 1 | #!/usr/bin/perl -w
|
---|
| 2 |
|
---|
| 3 | ###########################################################################
|
---|
| 4 | #
|
---|
| 5 | # gti.pl
|
---|
| 6 | #
|
---|
| 7 | # A component of the Greenstone digital library software
|
---|
| 8 | # from the New Zealand Digital Library Project at the
|
---|
| 9 | # University of Waikato, New Zealand.
|
---|
| 10 | #
|
---|
| 11 | # Copyright (C) 2005 New Zealand Digital Library Project
|
---|
| 12 | #
|
---|
| 13 | # This program is free software; you can redistribute it and/or modify
|
---|
| 14 | # it under the terms of the GNU General Public License as published by
|
---|
| 15 | # the Free Software Foundation; either version 2 of the License, or
|
---|
| 16 | # (at your option) any later version.
|
---|
| 17 | #
|
---|
| 18 | # This program is distributed in the hope that it will be useful,
|
---|
| 19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 21 | # GNU General Public License for more details.
|
---|
| 22 | #
|
---|
| 23 | # You should have received a copy of the GNU General Public License
|
---|
| 24 | # along with this program; if not, write to the Free Software
|
---|
| 25 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 26 | #
|
---|
| 27 | ###########################################################################
|
---|
| 28 |
|
---|
| 29 |
|
---|
| 30 | BEGIN {
|
---|
| 31 | die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
|
---|
| 32 | unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
|
---|
| 33 | }
|
---|
| 34 |
|
---|
[24627] | 35 |
|
---|
[10019] | 36 | use iso639;
|
---|
| 37 | use strict;
|
---|
| 38 | use util;
|
---|
[29415] | 39 | use FileUtils;
|
---|
[10019] | 40 |
|
---|
| 41 | my $gsdl_root_directory = "$ENV{'GSDLHOME'}";
|
---|
| 42 | my $gti_log_file = &util::filename_cat($gsdl_root_directory, "etc", "gti.log");
|
---|
[28976] | 43 | my $source_language_code = "en"; # This is non-negotiable
|
---|
[10019] | 44 |
|
---|
| 45 | my $gti_translation_files =
|
---|
[24627] | 46 | [ # Greenstone macrofiles
|
---|
| 47 | { 'key' => "coredm",
|
---|
[10019] | 48 | 'file_type' => "macrofile",
|
---|
| 49 | 'source_file' => "macros/english.dm",
|
---|
[11224] | 50 | 'target_file' => "macros/{bn:bengali;fa:farsi;gd:gaelic;id:indo;lv:latvian;pt-br:port-br;pt-pt:port-pt;zh-tr:chinese-trad;iso_639_1_target_language_name}.dm" },
|
---|
[24627] | 51 |
|
---|
| 52 | { 'key' => "auxdm",
|
---|
[10019] | 53 | 'file_type' => "macrofile",
|
---|
| 54 | 'source_file' => "macros/english2.dm",
|
---|
[11224] | 55 | 'target_file' => "macros/{bn:bengali;fa:farsi;gd:gaelic;id:indo;lv:latvian;pt-br:port-br;pt-pt:port-pt;zh-tr:chinese-trad;iso_639_1_target_language_name}2.dm" },
|
---|
[10019] | 56 |
|
---|
[29411] | 57 | #{ 'key' => "paperspastdm",
|
---|
| 58 | # 'file_type' => "macrofile",
|
---|
| 59 | # 'source_file' => "macros/paperspast-english.dm",
|
---|
| 60 | # 'target_file' => "macros/paperspast-{bn:bengali;fa:farsi;gd:gaelic;id:indo;lv:latvian;pt-br:port-br;pt-pt:port-pt;zh-tr:chinese-trad;iso_639_1_target_language_name}.dm" },
|
---|
[25285] | 61 |
|
---|
[24627] | 62 | # GLI dictionary
|
---|
| 63 | { 'key' => "glidict",
|
---|
[13946] | 64 | 'file_type' => "resource_bundle",
|
---|
| 65 | 'source_file' => "gli/classes/dictionary.properties",
|
---|
| 66 | 'target_file' => "gli/classes/dictionary_{target_language_code}.properties" },
|
---|
[10114] | 67 |
|
---|
[24627] | 68 | # GLI help
|
---|
| 69 | { 'key' => "glihelp",
|
---|
[13946] | 70 | 'file_type' => "greenstone_xml",
|
---|
| 71 | 'source_file' => "gli/help/en/help.xml",
|
---|
| 72 | 'target_file' => "gli/help/{target_language_code}/help.xml" },
|
---|
[11602] | 73 |
|
---|
[24627] | 74 | # Greenstone Perl modules
|
---|
| 75 | { 'key' => "perlmodules",
|
---|
[10019] | 76 | 'file_type' => "resource_bundle",
|
---|
[11634] | 77 | 'source_file' => "perllib/strings.properties",
|
---|
| 78 | 'target_file' => "perllib/strings_{target_language_code}.properties" },
|
---|
[10019] | 79 |
|
---|
[28337] | 80 | # Greenstone Installer interface
|
---|
| 81 | { 'key' => "gsinstaller",
|
---|
| 82 | 'file_type' => "resource_bundle",
|
---|
| 83 | 'source_file' => "gsinstaller/LanguagePack.properties",
|
---|
| 84 | 'target_file' => "gsinstaller/LanguagePack_{target_language_code}.properties" },
|
---|
| 85 |
|
---|
[24627] | 86 | # Greenstone tutorial exercises
|
---|
| 87 | # { 'key' => "tutorials",
|
---|
| 88 | # 'file_type' => "greenstone_xml",
|
---|
| 89 | # 'source_file' => "gsdl-documentation/tutorials/xml-source/tutorial_en.xml",
|
---|
| 90 | # 'target_file' => "gsdl-documentation/tutorials/xml-source/tutorial_{target_language_code}.xml" },
|
---|
[11366] | 91 |
|
---|
[24627] | 92 | # new Greenstone.org
|
---|
| 93 | { 'key' => "greenorg",
|
---|
| 94 | 'file_type' => "resource_bundle",
|
---|
| 95 | 'source_file' => "greenstoneorg/website/classes/Gsc.properties",
|
---|
| 96 | 'target_file' => "greenstoneorg/website/classes/Gsc_{target_language_code}.properties"
|
---|
[29411] | 97 | },
|
---|
| 98 |
|
---|
[29412] | 99 | # greenstone 3 interface files, from http://svn.greenstone.org/main/trunk/greenstone3/web/WEB-INF/classes
|
---|
[29413] | 100 | # check it out as greenstone3
|
---|
[29411] | 101 | { 'key' => "gs3interface",
|
---|
[29415] | 102 | 'file_type' => "resource_bundle",
|
---|
| 103 | 'source_file' => "greenstone3",
|
---|
| 104 | 'target_file' => "greenstone3"
|
---|
[30581] | 105 | },
|
---|
| 106 |
|
---|
| 107 | # collection config display items of GS3 demo collections. Checked out as gs3-collection-configs
|
---|
| 108 | # from http://svn.greenstone.org/main/trunk/gs3-collection-configs
|
---|
| 109 | { 'key' => "gs3colcfg",
|
---|
| 110 | 'file_type' => "resource_bundle",
|
---|
| 111 | 'source_file' => "gs3-collection-configs",
|
---|
| 112 | 'target_file' => "gs3-collection-configs"
|
---|
[24627] | 113 | }
|
---|
| 114 | ];
|
---|
[10019] | 115 |
|
---|
[30581] | 116 | my @gs3_col_cfg_files = ("lucene-jdbm-demo", "solr-jdbm-demo");
|
---|
| 117 |
|
---|
[30490] | 118 | my @gs3_interface_files = ("interface_default", "ServiceRack", "metadata_names");
|
---|
| 119 | #"AbstractBrowse", "AbstractGS2FieldSearch", "AbstractSearch", "AbstractTextSearch", "Authentication", "CrossCollectionSearch", "GS2LuceneSearch", "LuceneSearch", "MapRetrieve", "MapSearch", "PhindPhraseBrowse", "SharedSoleneGS2FieldSearch");
|
---|
[11026] | 120 |
|
---|
[26547] | 121 | # Auxilliary GS3 interface files. This list is not used at present
|
---|
| 122 | # Combine with above list if generating translation spreadsheet for all interface files
|
---|
[30490] | 123 | my @gs3_aux_interface_files = ("GATEServices","QBRWebServicesHelp", "Visualizer", "IViaSearch", "GS2Construct");
|
---|
[18460] | 124 |
|
---|
[30490] | 125 | my @gs3_other_interface_files = ("interface_default2", "interface_basic", "interface_basic2", "interface_nzdl", "interface_gs2");
|
---|
| 126 |
|
---|
[26547] | 127 | # Not: i18n, log4j
|
---|
| 128 |
|
---|
[10019] | 129 | sub main
|
---|
| 130 | {
|
---|
| 131 | # Get the command to process, and any arguments
|
---|
| 132 | my $gti_command = shift(@_);
|
---|
| 133 | my @gti_command_arguments = @_;
|
---|
[18460] | 134 | my $module = $_[1];
|
---|
[26544] | 135 |
|
---|
| 136 | # for GS3, set gsdl_root_dir to GSDL3HOME
|
---|
[29411] | 137 | #if($module && $module eq "gs3interface"){ # module is empty when the gti-command is create-glihelp-zip-file
|
---|
| 138 | #if($ENV{'GSDL3SRCHOME'}) {
|
---|
| 139 | # $gsdl_root_directory = (defined $ENV{'GSDL3HOME'}) ? $ENV{'GSDL3HOME'} : &util::filename_cat($ENV{'GSDL3SRCHOME'}, "web");
|
---|
| 140 | # $gti_log_file = &util::filename_cat($gsdl_root_directory, "logs", "gti.log");
|
---|
| 141 | #}
|
---|
| 142 | #}
|
---|
[24627] | 143 |
|
---|
[10019] | 144 | # Open the GTI log file for appending, or write to STDERR if that fails
|
---|
| 145 | if (!open(GTI_LOG, ">>$gti_log_file")) {
|
---|
[24627] | 146 | open(GTI_LOG, ">&STDERR");
|
---|
[10019] | 147 | }
|
---|
[24627] | 148 |
|
---|
[10019] | 149 | # Log the command that launched this script
|
---|
| 150 | &log_message("Command: $0 @ARGV");
|
---|
[24627] | 151 |
|
---|
[10019] | 152 | # Check that a command was supplied
|
---|
| 153 | if (!$gti_command) {
|
---|
[24627] | 154 | &throw_fatal_error("Missing command.");
|
---|
[18460] | 155 | }
|
---|
[24627] | 156 |
|
---|
[10019] | 157 | # Process the command
|
---|
[13948] | 158 | if ($gti_command =~ /^get-all-chunks$/i) {
|
---|
[24627] | 159 | # Check that GS3 interface is the target
|
---|
[30582] | 160 | if ($module =~ m/^gs3/) { # gs3interface, gs3colcfg
|
---|
[24627] | 161 | print &get_all_chunks_for_gs3(@gti_command_arguments);
|
---|
| 162 | } else {
|
---|
| 163 | print &get_all_chunks(@gti_command_arguments);
|
---|
| 164 | }
|
---|
[13948] | 165 | }
|
---|
[24627] | 166 | elsif ($gti_command =~ /^get-first-n-chunks-requiring-work$/i) {
|
---|
[30582] | 167 | if ($module =~ m/^gs3/) {
|
---|
[24627] | 168 | print &get_first_n_chunks_requiring_work_for_gs3(@gti_command_arguments);
|
---|
| 169 | } else {
|
---|
| 170 | print &get_first_n_chunks_requiring_work(@gti_command_arguments);
|
---|
| 171 | }
|
---|
[10019] | 172 | }
|
---|
[25249] | 173 | elsif ($gti_command =~ /^get-uptodate-chunks$/i) {
|
---|
[30582] | 174 | if ($module =~ m/^gs3/) {
|
---|
[25249] | 175 | print &get_uptodate_chunks_for_gs3(@gti_command_arguments);
|
---|
| 176 | } else {
|
---|
| 177 | print &get_uptodate_chunks(@gti_command_arguments);
|
---|
| 178 | }
|
---|
| 179 | }
|
---|
[24627] | 180 | elsif ($gti_command =~ /^get-language-status$/i) {
|
---|
| 181 | print &get_language_status(@gti_command_arguments);
|
---|
[10019] | 182 | }
|
---|
[24627] | 183 | elsif ($gti_command =~ /^search-chunks$/i) {
|
---|
| 184 | print &search_chunks(@gti_command_arguments);
|
---|
[10019] | 185 | }
|
---|
[24627] | 186 | elsif ($gti_command =~ /^submit-translations$/i) {
|
---|
| 187 | # This command cannot produce any output since it reads input
|
---|
| 188 | &submit_translations(@gti_command_arguments);
|
---|
[10019] | 189 | }
|
---|
[24627] | 190 | elsif ($gti_command =~ /^create-glihelp-zip-file$/i) {
|
---|
| 191 | # This command cannot produce any output since it reads input
|
---|
| 192 | &create_glihelp_zip_file(@gti_command_arguments);
|
---|
[18460] | 193 | }
|
---|
[24627] | 194 | else {
|
---|
| 195 | # The command was not recognized
|
---|
| 196 | &throw_fatal_error("Unknown command \"$gti_command\".");
|
---|
| 197 | }
|
---|
[10019] | 198 | }
|
---|
| 199 |
|
---|
| 200 |
|
---|
| 201 | sub throw_fatal_error
|
---|
| 202 | {
|
---|
| 203 | my $error_message = shift(@_);
|
---|
[24627] | 204 |
|
---|
[10019] | 205 | # Write an XML error response
|
---|
| 206 | print "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
|
---|
| 207 | print "<GTIResponse>\n";
|
---|
| 208 | print " <GTIError time=\"" . time() . "\">" . $error_message . "</GTIError>\n";
|
---|
| 209 | print "</GTIResponse>\n";
|
---|
[24627] | 210 |
|
---|
[10019] | 211 | # Log the error message, then die
|
---|
| 212 | &log_message("Error: $error_message");
|
---|
| 213 | die "\n";
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 |
|
---|
| 217 | sub log_message
|
---|
| 218 | {
|
---|
| 219 | my $log_message = shift(@_);
|
---|
| 220 | print GTI_LOG time() . " -- " . $log_message . "\n";
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 |
|
---|
[13948] | 224 | sub get_all_chunks
|
---|
| 225 | {
|
---|
| 226 | # The code of the target language (ensure it is lowercase)
|
---|
| 227 | my $target_language_code = lc(shift(@_));
|
---|
| 228 | # The key of the file to translate (ensure it is lowercase)
|
---|
| 229 | my $translation_file_key = lc(shift(@_));
|
---|
[24627] | 230 |
|
---|
[13948] | 231 | # Check that the necessary arguments were supplied
|
---|
| 232 | if (!$target_language_code || !$translation_file_key) {
|
---|
[24627] | 233 | &throw_fatal_error("Missing command argument.");
|
---|
[13948] | 234 | }
|
---|
[24627] | 235 |
|
---|
[13948] | 236 | # Get (and check) the translation configuration
|
---|
| 237 | my ($source_file, $target_file, $translation_file_type)
|
---|
| 238 | = &get_translation_configuration($target_language_code, $translation_file_key);
|
---|
[24627] | 239 |
|
---|
[13948] | 240 | # Parse the source language and target language files
|
---|
| 241 | my $source_file_path = &util::filename_cat($gsdl_root_directory, $source_file);
|
---|
| 242 | my @source_file_lines = &read_file_lines($source_file_path);
|
---|
| 243 | my %source_file_key_to_line_mapping = &build_key_to_line_mapping(\@source_file_lines, $translation_file_type);
|
---|
| 244 |
|
---|
| 245 | my $target_file_path = &util::filename_cat($gsdl_root_directory, $target_file);
|
---|
| 246 | my @target_file_lines = &read_file_lines($target_file_path);
|
---|
| 247 | my %target_file_key_to_line_mapping = &build_key_to_line_mapping(\@target_file_lines, $translation_file_type);
|
---|
[24627] | 248 |
|
---|
[13948] | 249 | # Filter out any automatically translated chunks
|
---|
| 250 | foreach my $chunk_key (keys(%source_file_key_to_line_mapping)) {
|
---|
[24627] | 251 | if (&is_chunk_automatically_translated($chunk_key, $translation_file_type)) {
|
---|
| 252 | delete $source_file_key_to_line_mapping{$chunk_key};
|
---|
| 253 | delete $target_file_key_to_line_mapping{$chunk_key};
|
---|
| 254 | }
|
---|
[13948] | 255 | }
|
---|
[24627] | 256 |
|
---|
[13948] | 257 | my %source_file_key_to_text_mapping = &build_key_to_text_mapping(\@source_file_lines, \%source_file_key_to_line_mapping, $translation_file_type);
|
---|
| 258 | my %target_file_key_to_text_mapping = &build_key_to_text_mapping(\@target_file_lines, \%target_file_key_to_line_mapping, $translation_file_type);
|
---|
| 259 | &log_message("Number of source chunks: " . scalar(keys(%source_file_key_to_text_mapping)));
|
---|
| 260 | &log_message("Number of target chunks: " . scalar(keys(%target_file_key_to_text_mapping)));
|
---|
[24627] | 261 |
|
---|
[13948] | 262 | my %source_file_key_to_last_update_date_mapping = &build_key_to_last_update_date_mapping($source_file, \@source_file_lines, \%source_file_key_to_line_mapping, $translation_file_type);
|
---|
| 263 | my %target_file_key_to_last_update_date_mapping = &build_key_to_last_update_date_mapping($target_file, \@target_file_lines, \%target_file_key_to_line_mapping, $translation_file_type);
|
---|
[24627] | 264 |
|
---|
[18460] | 265 | my $xml_response = &create_xml_response_for_all_chunks($translation_file_key, $target_file, \%source_file_key_to_text_mapping, \%target_file_key_to_text_mapping, \%source_file_key_to_last_update_date_mapping, \%target_file_key_to_last_update_date_mapping);
|
---|
[13948] | 266 |
|
---|
| 267 | return $xml_response;
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 |
|
---|
[25249] | 271 | sub get_uptodate_chunks
|
---|
| 272 | {
|
---|
| 273 | # The code of the target language (ensure it is lowercase)
|
---|
| 274 | my $target_language_code = lc(shift(@_));
|
---|
| 275 | # The key of the file to translate (ensure it is lowercase)
|
---|
| 276 | my $translation_file_key = lc(shift(@_));
|
---|
| 277 |
|
---|
| 278 | # Check that the necessary arguments were supplied
|
---|
| 279 | if (!$target_language_code || !$translation_file_key) {
|
---|
| 280 | &throw_fatal_error("Missing command argument.");
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | # Get (and check) the translation configuration
|
---|
| 284 | my ($source_file, $target_file, $translation_file_type)
|
---|
| 285 | = &get_translation_configuration($target_language_code, $translation_file_key);
|
---|
| 286 |
|
---|
| 287 | # Parse the source language and target language files
|
---|
| 288 | my $source_file_path = &util::filename_cat($gsdl_root_directory, $source_file);
|
---|
| 289 | my @source_file_lines = &read_file_lines($source_file_path);
|
---|
| 290 | my %source_file_key_to_line_mapping = &build_key_to_line_mapping(\@source_file_lines, $translation_file_type);
|
---|
| 291 |
|
---|
| 292 | my $target_file_path = &util::filename_cat($gsdl_root_directory, $target_file);
|
---|
| 293 | my @target_file_lines = &read_file_lines($target_file_path);
|
---|
| 294 | my %target_file_key_to_line_mapping = &build_key_to_line_mapping(\@target_file_lines, $translation_file_type);
|
---|
| 295 |
|
---|
| 296 | # Filter out any automatically translated chunks
|
---|
| 297 | foreach my $chunk_key (keys(%source_file_key_to_line_mapping)) {
|
---|
| 298 | if (&is_chunk_automatically_translated($chunk_key, $translation_file_type)) {
|
---|
| 299 | delete $source_file_key_to_line_mapping{$chunk_key};
|
---|
| 300 | delete $target_file_key_to_line_mapping{$chunk_key};
|
---|
| 301 | }
|
---|
| 302 | }
|
---|
| 303 |
|
---|
| 304 | my %source_file_key_to_text_mapping = &build_key_to_text_mapping(\@source_file_lines, \%source_file_key_to_line_mapping, $translation_file_type);
|
---|
| 305 | my %target_file_key_to_text_mapping = &build_key_to_text_mapping(\@target_file_lines, \%target_file_key_to_line_mapping, $translation_file_type);
|
---|
| 306 | &log_message("Number of source chunks: " . scalar(keys(%source_file_key_to_text_mapping)));
|
---|
| 307 | &log_message("Number of target chunks: " . scalar(keys(%target_file_key_to_text_mapping)));
|
---|
| 308 |
|
---|
| 309 | my %source_file_key_to_last_update_date_mapping = &build_key_to_last_update_date_mapping($source_file, \@source_file_lines, \%source_file_key_to_line_mapping, $translation_file_type);
|
---|
| 310 | my %target_file_key_to_last_update_date_mapping = &build_key_to_last_update_date_mapping($target_file, \@target_file_lines, \%target_file_key_to_line_mapping, $translation_file_type);
|
---|
| 311 |
|
---|
| 312 |
|
---|
| 313 | # Chunks needing updating are those in the target file that have been more recently edited in the source file
|
---|
| 314 | # All others are uptodate (which implies that they have certainly been translated at some point and would not be empty)
|
---|
| 315 | my @uptodate_target_file_keys = ();
|
---|
| 316 | foreach my $chunk_key (keys(%source_file_key_to_last_update_date_mapping)) {
|
---|
| 317 | my $source_chunk_last_update_date = $source_file_key_to_last_update_date_mapping{$chunk_key};
|
---|
| 318 | my $target_chunk_last_update_date = $target_file_key_to_last_update_date_mapping{$chunk_key};
|
---|
| 319 |
|
---|
| 320 | # print "key: $chunk_key\nsource date : $source_chunk_last_update_date\ntarget date : $target_chunk_last_update_date\nafter? ". &is_date_after($source_chunk_last_update_date, $target_chunk_last_update_date) . "\n\n";
|
---|
| 321 |
|
---|
| 322 | if (defined($target_chunk_last_update_date) && !&is_date_after($source_chunk_last_update_date, $target_chunk_last_update_date)) {
|
---|
| 323 | # &log_message("Chunk with key $chunk_key needs updating.");
|
---|
| 324 | push(@uptodate_target_file_keys, $chunk_key);
|
---|
| 325 | }
|
---|
| 326 | }
|
---|
| 327 |
|
---|
| 328 | my $xml_response = &create_xml_response_for_uptodate_chunks($translation_file_key, $target_file, \@uptodate_target_file_keys, \%source_file_key_to_text_mapping, \%target_file_key_to_text_mapping, \%source_file_key_to_last_update_date_mapping, \%target_file_key_to_last_update_date_mapping);
|
---|
| 329 |
|
---|
| 330 | return $xml_response;
|
---|
| 331 | }
|
---|
| 332 |
|
---|
| 333 |
|
---|
[10019] | 334 | sub get_first_n_chunks_requiring_work
|
---|
| 335 | {
|
---|
| 336 | # The code of the target language (ensure it is lowercase)
|
---|
| 337 | my $target_language_code = lc(shift(@_));
|
---|
| 338 | # The key of the file to translate (ensure it is lowercase)
|
---|
| 339 | my $translation_file_key = lc(shift(@_));
|
---|
| 340 | # The number of chunks to return (defaults to one if not specified)
|
---|
| 341 | my $num_chunks_to_return = shift(@_) || "1";
|
---|
[24627] | 342 |
|
---|
[10019] | 343 | # Check that the necessary arguments were supplied
|
---|
| 344 | if (!$target_language_code || !$translation_file_key) {
|
---|
[24627] | 345 | &throw_fatal_error("Missing command argument.");
|
---|
[10019] | 346 | }
|
---|
[24627] | 347 |
|
---|
[10019] | 348 | # Get (and check) the translation configuration
|
---|
| 349 | my ($source_file, $target_file, $translation_file_type)
|
---|
| 350 | = &get_translation_configuration($target_language_code, $translation_file_key);
|
---|
[28755] | 351 |
|
---|
[10019] | 352 | # Parse the source language and target language files
|
---|
| 353 | my $source_file_path = &util::filename_cat($gsdl_root_directory, $source_file);
|
---|
| 354 | my @source_file_lines = &read_file_lines($source_file_path);
|
---|
| 355 | my %source_file_key_to_line_mapping = &build_key_to_line_mapping(\@source_file_lines, $translation_file_type);
|
---|
[14258] | 356 |
|
---|
[10019] | 357 | my $target_file_path = &util::filename_cat($gsdl_root_directory, $target_file);
|
---|
| 358 | my @target_file_lines = &read_file_lines($target_file_path);
|
---|
| 359 | my %target_file_key_to_line_mapping = &build_key_to_line_mapping(\@target_file_lines, $translation_file_type);
|
---|
[24627] | 360 |
|
---|
[10019] | 361 | # Filter out any automatically translated chunks
|
---|
| 362 | foreach my $chunk_key (keys(%source_file_key_to_line_mapping)) {
|
---|
[24627] | 363 | if (&is_chunk_automatically_translated($chunk_key, $translation_file_type)) {
|
---|
| 364 | delete $source_file_key_to_line_mapping{$chunk_key};
|
---|
| 365 | delete $target_file_key_to_line_mapping{$chunk_key};
|
---|
| 366 | }
|
---|
[10019] | 367 | }
|
---|
[24627] | 368 |
|
---|
[10019] | 369 | my %source_file_key_to_text_mapping = &build_key_to_text_mapping(\@source_file_lines, \%source_file_key_to_line_mapping, $translation_file_type);
|
---|
| 370 | my %target_file_key_to_text_mapping = &build_key_to_text_mapping(\@target_file_lines, \%target_file_key_to_line_mapping, $translation_file_type);
|
---|
| 371 | &log_message("Number of source chunks: " . scalar(keys(%source_file_key_to_text_mapping)));
|
---|
| 372 | &log_message("Number of target chunks: " . scalar(keys(%target_file_key_to_text_mapping)));
|
---|
[24627] | 373 |
|
---|
[10019] | 374 | # Determine the target file chunks requiring translation
|
---|
| 375 | my @target_file_keys_requiring_translation = &determine_chunks_requiring_translation(\%source_file_key_to_text_mapping, \%target_file_key_to_text_mapping);
|
---|
| 376 | &log_message("Number of target chunks requiring translation: " . scalar(@target_file_keys_requiring_translation));
|
---|
[24627] | 377 |
|
---|
[10019] | 378 | # Determine the target file chunks requiring updating
|
---|
| 379 | my %source_file_key_to_last_update_date_mapping = &build_key_to_last_update_date_mapping($source_file, \@source_file_lines, \%source_file_key_to_line_mapping, $translation_file_type);
|
---|
| 380 | my %target_file_key_to_last_update_date_mapping = &build_key_to_last_update_date_mapping($target_file, \@target_file_lines, \%target_file_key_to_line_mapping, $translation_file_type);
|
---|
| 381 | my @target_file_keys_requiring_updating = &determine_chunks_requiring_updating(\%source_file_key_to_last_update_date_mapping, \%target_file_key_to_last_update_date_mapping);
|
---|
| 382 | &log_message("Number of target chunks requiring updating: " . scalar(@target_file_keys_requiring_updating));
|
---|
[24627] | 383 |
|
---|
[18460] | 384 | my $xml_response = &create_xml_response_for_chunks_requiring_work($translation_file_key, $target_file, scalar(keys(%source_file_key_to_text_mapping)), \@target_file_keys_requiring_translation, \@target_file_keys_requiring_updating, $num_chunks_to_return, \%source_file_key_to_text_mapping, \%target_file_key_to_text_mapping, \%source_file_key_to_last_update_date_mapping, \%target_file_key_to_last_update_date_mapping);
|
---|
| 385 |
|
---|
[10040] | 386 | return $xml_response;
|
---|
[10019] | 387 | }
|
---|
| 388 |
|
---|
| 389 |
|
---|
| 390 | sub get_language_status
|
---|
| 391 | {
|
---|
| 392 | # The code of the target language (ensure it is lowercase)
|
---|
| 393 | my $target_language_code = lc(shift(@_));
|
---|
[24627] | 394 |
|
---|
[10019] | 395 | # Check that the necessary arguments were supplied
|
---|
| 396 | if (!$target_language_code) {
|
---|
[24627] | 397 | &throw_fatal_error("Missing command argument.");
|
---|
[10019] | 398 | }
|
---|
[24627] | 399 |
|
---|
[10040] | 400 | # Form an XML response to the command
|
---|
| 401 | my $xml_response = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
|
---|
| 402 | $xml_response .= "<GTIResponse>\n";
|
---|
| 403 | $xml_response .= " <LanguageStatus code=\"$target_language_code\">\n";
|
---|
[24627] | 404 |
|
---|
[18460] | 405 | foreach my $translation_file (@$gti_translation_files) {
|
---|
[24627] | 406 | my ($num_source_chunks, $num_target_chunks, $num_chunks_requiring_translation, $num_chunks_requiring_updating) = 0;
|
---|
| 407 | my $target_file_name = "";
|
---|
| 408 |
|
---|
[30582] | 409 | if ($translation_file->{'key'} =~ m/^gs3/) { # gs3interface, gs3colcfg
|
---|
[24627] | 410 | my (%source_file_key_to_text_mapping, %target_file_key_to_text_mapping, %source_file_key_to_last_update_date_mapping, %target_file_key_to_last_update_date_mapping ) = ();
|
---|
[30581] | 411 | &build_gs3_configuration($translation_file->{'key'}, $target_language_code, \%source_file_key_to_text_mapping, \%target_file_key_to_text_mapping, \%source_file_key_to_last_update_date_mapping, \%target_file_key_to_last_update_date_mapping );
|
---|
[24627] | 412 |
|
---|
| 413 | my @target_file_keys_requiring_translation = &determine_chunks_requiring_translation(\%source_file_key_to_text_mapping, \%target_file_key_to_text_mapping);
|
---|
| 414 | my @target_file_keys_requiring_updating = &determine_chunks_requiring_updating(\%source_file_key_to_last_update_date_mapping, \%target_file_key_to_last_update_date_mapping);
|
---|
| 415 |
|
---|
| 416 | $num_source_chunks = scalar(keys(%source_file_key_to_text_mapping));
|
---|
| 417 | $num_target_chunks = scalar(keys(%target_file_key_to_text_mapping));
|
---|
| 418 | $num_chunks_requiring_translation = scalar(@target_file_keys_requiring_translation);
|
---|
| 419 | $num_chunks_requiring_updating = scalar(@target_file_keys_requiring_updating);
|
---|
[18460] | 420 | }
|
---|
[24627] | 421 | else {
|
---|
| 422 | # Get (and check) the translation configuration
|
---|
| 423 | my ($source_file, $target_file, $translation_file_type) = &get_translation_configuration($target_language_code, $translation_file->{'key'});
|
---|
| 424 | $target_file_name = $target_file;
|
---|
| 425 |
|
---|
| 426 | # Parse the source language and target language files
|
---|
| 427 | my $source_file_path = &util::filename_cat($gsdl_root_directory, $source_file);
|
---|
| 428 | my @source_file_lines = &read_file_lines($source_file_path);
|
---|
| 429 | my %source_file_key_to_line_mapping = &build_key_to_line_mapping(\@source_file_lines, $translation_file_type);
|
---|
| 430 |
|
---|
| 431 | my $target_file_path = &util::filename_cat($gsdl_root_directory, $target_file);
|
---|
| 432 | my @target_file_lines = &read_file_lines($target_file_path);
|
---|
| 433 | my %target_file_key_to_line_mapping = &build_key_to_line_mapping(\@target_file_lines, $translation_file_type);
|
---|
| 434 |
|
---|
| 435 | # Filter out any automatically translated chunks
|
---|
| 436 | foreach my $chunk_key (keys(%source_file_key_to_line_mapping)) {
|
---|
| 437 | if (&is_chunk_automatically_translated($chunk_key, $translation_file_type)) {
|
---|
| 438 | delete $source_file_key_to_line_mapping{$chunk_key};
|
---|
| 439 | delete $target_file_key_to_line_mapping{$chunk_key};
|
---|
| 440 | }
|
---|
| 441 | }
|
---|
| 442 |
|
---|
| 443 | my %source_file_key_to_text_mapping = &build_key_to_text_mapping(\@source_file_lines, \%source_file_key_to_line_mapping, $translation_file_type);
|
---|
| 444 | my %target_file_key_to_text_mapping = &build_key_to_text_mapping(\@target_file_lines, \%target_file_key_to_line_mapping, $translation_file_type);
|
---|
| 445 |
|
---|
| 446 | # Determine the target file chunks requiring translation
|
---|
| 447 | my @target_file_keys_requiring_translation = &determine_chunks_requiring_translation(\%source_file_key_to_text_mapping, \%target_file_key_to_text_mapping);
|
---|
| 448 |
|
---|
| 449 | # Determine the target file chunks requiring updating
|
---|
| 450 | my @target_file_keys_requiring_updating = ();
|
---|
| 451 | if (-e $target_file_path) {
|
---|
| 452 | my %source_file_key_to_last_update_date_mapping = &build_key_to_last_update_date_mapping($source_file, \@source_file_lines, \%source_file_key_to_line_mapping, $translation_file_type);
|
---|
| 453 | my %target_file_key_to_last_update_date_mapping = &build_key_to_last_update_date_mapping($target_file, \@target_file_lines, \%target_file_key_to_line_mapping, $translation_file_type);
|
---|
| 454 | @target_file_keys_requiring_updating = &determine_chunks_requiring_updating(\%source_file_key_to_last_update_date_mapping, \%target_file_key_to_last_update_date_mapping);
|
---|
| 455 | }
|
---|
| 456 |
|
---|
| 457 | $num_source_chunks = scalar(keys(%source_file_key_to_text_mapping));
|
---|
| 458 | $num_target_chunks = scalar(keys(%target_file_key_to_text_mapping));
|
---|
| 459 | $num_chunks_requiring_translation = scalar(@target_file_keys_requiring_translation);
|
---|
| 460 | $num_chunks_requiring_updating = scalar(@target_file_keys_requiring_updating);
|
---|
| 461 | }
|
---|
| 462 |
|
---|
| 463 | &log_message("Status of " . $translation_file->{'key'});
|
---|
| 464 | &log_message("Number of source chunks: " . $num_source_chunks);
|
---|
| 465 | &log_message("Number of target chunks: " . $num_target_chunks);
|
---|
| 466 | &log_message("Number of target chunks requiring translation: " . $num_chunks_requiring_translation);
|
---|
| 467 | &log_message("Number of target chunks requiring updating: " . $num_chunks_requiring_updating);
|
---|
| 468 |
|
---|
| 469 | $xml_response .= " <TranslationFile"
|
---|
[10019] | 470 | . " key=\"" . $translation_file->{'key'} . "\""
|
---|
[18460] | 471 | . " target_file_path=\"" . $target_file_name . "\""
|
---|
| 472 | . " num_chunks_translated=\"" . ($num_source_chunks - $num_chunks_requiring_translation) . "\""
|
---|
| 473 | . " num_chunks_requiring_translation=\"" . $num_chunks_requiring_translation . "\""
|
---|
| 474 | . " num_chunks_requiring_updating=\"" . $num_chunks_requiring_updating . "\"\/>\n";
|
---|
[10019] | 475 | }
|
---|
[24627] | 476 |
|
---|
[10040] | 477 | $xml_response .= " </LanguageStatus>\n";
|
---|
[24627] | 478 |
|
---|
[10040] | 479 | $xml_response .= "</GTIResponse>\n";
|
---|
| 480 | return $xml_response;
|
---|
[10019] | 481 | }
|
---|
| 482 |
|
---|
| 483 |
|
---|
| 484 | sub search_chunks
|
---|
| 485 | {
|
---|
| 486 | # The code of the target language (ensure it is lowercase)
|
---|
| 487 | my $target_language_code = lc(shift(@_));
|
---|
| 488 | # The key of the file to translate (ensure it is lowercase)
|
---|
| 489 | my $translation_file_key = lc(shift(@_));
|
---|
| 490 | # The query string
|
---|
[10020] | 491 | my $query_string = join(' ', @_);
|
---|
[24627] | 492 |
|
---|
[10019] | 493 | # Check that the necessary arguments were supplied
|
---|
| 494 | if (!$target_language_code || !$translation_file_key || !$query_string) {
|
---|
[24627] | 495 | &throw_fatal_error("Missing command argument.");
|
---|
[10019] | 496 | }
|
---|
[24627] | 497 |
|
---|
[18460] | 498 | my ($source_file, $target_file, $translation_file_type) = ();
|
---|
| 499 | my %source_file_key_to_text_mapping = ();
|
---|
| 500 | my %target_file_key_to_text_mapping = ();
|
---|
| 501 |
|
---|
| 502 |
|
---|
[30582] | 503 | if ($translation_file_key !~ m/^gs3/) {
|
---|
[24627] | 504 | # Get (and check) the translation configuration
|
---|
| 505 | ($source_file, $target_file, $translation_file_type) = &get_translation_configuration($target_language_code, $translation_file_key);
|
---|
| 506 |
|
---|
| 507 | # Parse the source language and target language files
|
---|
| 508 | my $source_file_path = &util::filename_cat($gsdl_root_directory, $source_file);
|
---|
| 509 | my @source_file_lines = &read_file_lines($source_file_path);
|
---|
| 510 | my %source_file_key_to_line_mapping = &build_key_to_line_mapping(\@source_file_lines, $translation_file_type);
|
---|
| 511 |
|
---|
| 512 | my $target_file_path = &util::filename_cat($gsdl_root_directory, $target_file);
|
---|
| 513 | my @target_file_lines = &read_file_lines($target_file_path);
|
---|
| 514 | my %target_file_key_to_line_mapping = &build_key_to_line_mapping(\@target_file_lines, $translation_file_type);
|
---|
| 515 |
|
---|
| 516 | # Filter out any automatically translated chunks
|
---|
| 517 | foreach my $chunk_key (keys(%source_file_key_to_line_mapping)) {
|
---|
| 518 | if (&is_chunk_automatically_translated($chunk_key, $translation_file_type)) {
|
---|
| 519 | delete $source_file_key_to_line_mapping{$chunk_key};
|
---|
| 520 | delete $target_file_key_to_line_mapping{$chunk_key};
|
---|
| 521 | }
|
---|
| 522 | }
|
---|
| 523 |
|
---|
| 524 | %source_file_key_to_text_mapping = &build_key_to_text_mapping(\@source_file_lines, \%source_file_key_to_line_mapping, $translation_file_type);
|
---|
| 525 | %target_file_key_to_text_mapping = &build_key_to_text_mapping(\@target_file_lines, \%target_file_key_to_line_mapping, $translation_file_type);
|
---|
[10019] | 526 | }
|
---|
[18460] | 527 | else {
|
---|
[24627] | 528 | # Not needed in this case
|
---|
| 529 | my (%source_file_key_to_gti_command_mapping, %target_file_key_to_gti_command_mapping) = ();
|
---|
[30581] | 530 | &build_gs3_configuration($translation_file_key, $target_language_code, \%source_file_key_to_text_mapping, \%target_file_key_to_text_mapping,
|
---|
[30548] | 531 | \%source_file_key_to_gti_command_mapping, \%target_file_key_to_gti_command_mapping, 1);
|
---|
[18460] | 532 | }
|
---|
[24627] | 533 |
|
---|
[10019] | 534 | &log_message("Number of source chunks: " . scalar(keys(%source_file_key_to_text_mapping)));
|
---|
| 535 | &log_message("Number of target chunks: " . scalar(keys(%target_file_key_to_text_mapping)));
|
---|
[24627] | 536 |
|
---|
[10019] | 537 | # Determine the target file chunks matching the query
|
---|
| 538 | my @target_file_keys_matching_query = ();
|
---|
| 539 | foreach my $chunk_key (keys(%target_file_key_to_text_mapping)) {
|
---|
[24627] | 540 | my $target_file_text = $target_file_key_to_text_mapping{$chunk_key};
|
---|
| 541 | if ($target_file_text =~ /$query_string/i) {
|
---|
| 542 | # &log_message("Chunk with key $chunk_key matches query.");
|
---|
| 543 | push(@target_file_keys_matching_query, $chunk_key);
|
---|
| 544 | }
|
---|
[10019] | 545 | }
|
---|
[24627] | 546 |
|
---|
[10040] | 547 | # Form an XML response to the command
|
---|
| 548 | my $xml_response = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
|
---|
| 549 | $xml_response .= "<GTIResponse>\n";
|
---|
[24627] | 550 |
|
---|
[10040] | 551 | $xml_response .= " <ChunksMatchingQuery size=\"" . scalar(@target_file_keys_matching_query) . "\">\n";
|
---|
[10019] | 552 | foreach my $chunk_key (@target_file_keys_matching_query) {
|
---|
[24627] | 553 | my $target_file_chunk_text = &make_text_xml_safe($target_file_key_to_text_mapping{$chunk_key});
|
---|
| 554 |
|
---|
| 555 | $xml_response .= " <Chunk key=\"$chunk_key\">\n";
|
---|
| 556 | $xml_response .= " <TargetFileText>$target_file_chunk_text</TargetFileText>\n";
|
---|
| 557 | $xml_response .= " </Chunk>\n";
|
---|
[10019] | 558 | }
|
---|
[10040] | 559 | $xml_response .= " </ChunksMatchingQuery>\n";
|
---|
[24627] | 560 |
|
---|
[10040] | 561 | $xml_response .= "</GTIResponse>\n";
|
---|
| 562 | return $xml_response;
|
---|
[10019] | 563 | }
|
---|
| 564 |
|
---|
| 565 |
|
---|
[10050] | 566 | sub submit_translations
|
---|
[10019] | 567 | {
|
---|
| 568 | # The code of the target language (ensure it is lowercase)
|
---|
| 569 | my $target_language_code = lc(shift(@_));
|
---|
| 570 | # The key of the file to translate (ensure it is lowercase)
|
---|
| 571 | my $translation_file_key = lc(shift(@_));
|
---|
[12484] | 572 | # The username of the translation submitter
|
---|
| 573 | my $submitter_username = shift(@_);
|
---|
[10027] | 574 | # Whether to submit a target chunk even if it hasn't changed
|
---|
| 575 | my $force_submission_flag = shift(@_);
|
---|
[24627] | 576 |
|
---|
[10019] | 577 | # Check that the necessary arguments were supplied
|
---|
[12484] | 578 | if (!$target_language_code || !$translation_file_key || !$submitter_username) {
|
---|
[24627] | 579 | &log_message("Fatal error (but cannot be thrown): Missing command argument.");
|
---|
| 580 | die "\n";
|
---|
[10019] | 581 | }
|
---|
[18460] | 582 |
|
---|
| 583 | my %source_file_key_to_text_mapping = ();
|
---|
| 584 | my %source_file_key_to_gti_comment_mapping = ();
|
---|
| 585 | my %target_file_key_to_text_mapping = ();
|
---|
| 586 | my %target_file_key_to_gti_comment_mapping = ();
|
---|
[24627] | 587 |
|
---|
[18460] | 588 | my (@source_file_lines, @target_file_lines) = ();
|
---|
| 589 | my ($source_file, $target_file, $translation_file_type);
|
---|
[24627] | 590 |
|
---|
[18460] | 591 |
|
---|
[30582] | 592 | if ($translation_file_key !~ m/^gs3/) {
|
---|
[24627] | 593 | # Get (and check) the translation configuration
|
---|
| 594 | ($source_file, $target_file, $translation_file_type)
|
---|
[18460] | 595 | = &get_translation_configuration($target_language_code, $translation_file_key);
|
---|
[24627] | 596 |
|
---|
| 597 | # Parse the source language and target language files
|
---|
| 598 | @source_file_lines = &read_file_lines(&util::filename_cat($gsdl_root_directory, $source_file));
|
---|
| 599 | my %source_file_key_to_line_mapping = &build_key_to_line_mapping(\@source_file_lines, $translation_file_type);
|
---|
| 600 | %source_file_key_to_text_mapping = &build_key_to_text_mapping(\@source_file_lines, \%source_file_key_to_line_mapping, $translation_file_type);
|
---|
| 601 | %source_file_key_to_gti_comment_mapping = &build_key_to_gti_comment_mapping(\@source_file_lines, \%source_file_key_to_line_mapping, $translation_file_type);
|
---|
| 602 |
|
---|
| 603 | @target_file_lines = &read_file_lines(&util::filename_cat($gsdl_root_directory, $target_file));
|
---|
| 604 | my %target_file_key_to_line_mapping = &build_key_to_line_mapping(\@target_file_lines, $translation_file_type);
|
---|
| 605 | %target_file_key_to_text_mapping = &build_key_to_text_mapping(\@target_file_lines, \%target_file_key_to_line_mapping, $translation_file_type);
|
---|
| 606 | %target_file_key_to_gti_comment_mapping = &build_key_to_gti_comment_mapping(\@target_file_lines, \%target_file_key_to_line_mapping, $translation_file_type);
|
---|
[18460] | 607 | }
|
---|
| 608 | else {
|
---|
[30581] | 609 | &build_gs3_configuration($translation_file_key, $target_language_code, \%source_file_key_to_text_mapping, \%target_file_key_to_text_mapping,
|
---|
[30548] | 610 | \%source_file_key_to_gti_comment_mapping, \%target_file_key_to_gti_comment_mapping, 1);
|
---|
[18460] | 611 | }
|
---|
[10019] | 612 | &log_message("Number of source chunks: " . scalar(keys(%source_file_key_to_text_mapping)));
|
---|
| 613 | &log_message("Number of target chunks: " . scalar(keys(%target_file_key_to_text_mapping)));
|
---|
[24627] | 614 |
|
---|
[10019] | 615 | # Submission date
|
---|
| 616 | my $day = (localtime)[3];
|
---|
| 617 | my $month = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")[(localtime)[4]];
|
---|
| 618 | my $year = (localtime)[5] + 1900;
|
---|
| 619 | my $submission_date = "$day-$month-$year";
|
---|
[24627] | 620 |
|
---|
[10019] | 621 | open(SUBMISSION, "-");
|
---|
| 622 | my @submission_lines = <SUBMISSION>;
|
---|
| 623 | close(SUBMISSION);
|
---|
[24627] | 624 |
|
---|
[10019] | 625 | # Remove any nasty carriage returns
|
---|
[18460] | 626 | # &log_message("Submission:");
|
---|
[10019] | 627 | foreach my $submission_line (@submission_lines) {
|
---|
[24627] | 628 | $submission_line =~ s/\r$//;
|
---|
| 629 | #&log_message(" $submission_line");
|
---|
[10019] | 630 | }
|
---|
[24627] | 631 |
|
---|
[10019] | 632 | my %source_file_key_to_submission_mapping = ();
|
---|
| 633 | my %target_file_key_to_submission_mapping = ();
|
---|
| 634 | for (my $i = 0; $i < scalar(@submission_lines); $i++) {
|
---|
[24627] | 635 | # Read source file part of submission
|
---|
| 636 | if ($submission_lines[$i] =~ /^\<SourceFileText key=\"(.+)\"\>/) {
|
---|
| 637 | my $chunk_key = $1;
|
---|
| 638 |
|
---|
| 639 | # Read the source file text
|
---|
| 640 | my $source_file_chunk_text = "";
|
---|
| 641 | $i++;
|
---|
| 642 | while ($i < scalar(@submission_lines) && $submission_lines[$i] !~ /^\<\/SourceFileText\>/) {
|
---|
| 643 | $source_file_chunk_text .= $submission_lines[$i];
|
---|
| 644 | $i++;
|
---|
| 645 | }
|
---|
| 646 | $source_file_chunk_text =~ s/\n$//; # Strip the extra newline character added
|
---|
| 647 | $source_file_chunk_text = &unmake_text_xml_safe($source_file_chunk_text);
|
---|
| 648 |
|
---|
| 649 | #&log_message("Source file key: $chunk_key");
|
---|
| 650 | #&log_message("Source file text: $source_file_chunk_text");
|
---|
| 651 | $source_file_key_to_submission_mapping{$chunk_key} = $source_file_chunk_text;
|
---|
| 652 | }
|
---|
| 653 |
|
---|
| 654 | # Read target file part of submission
|
---|
| 655 | if ($submission_lines[$i] =~ /^\<TargetFileText key=\"(.+)\"\>/) {
|
---|
| 656 | my $chunk_key = $1;
|
---|
| 657 |
|
---|
| 658 | # Read the target file text
|
---|
| 659 | my $target_file_chunk_text = "";
|
---|
| 660 | $i++;
|
---|
| 661 | while ($i < scalar(@submission_lines) && $submission_lines[$i] !~ /^\<\/TargetFileText\>/) {
|
---|
| 662 | $target_file_chunk_text .= $submission_lines[$i];
|
---|
| 663 | $i++;
|
---|
| 664 | }
|
---|
| 665 | $target_file_chunk_text =~ s/\n$//; # Strip the extra newline character added
|
---|
| 666 | $target_file_chunk_text = &unmake_text_xml_safe($target_file_chunk_text);
|
---|
| 667 |
|
---|
| 668 | #&log_message("Target file key: $chunk_key");
|
---|
| 669 | #&log_message("Target file text: $target_file_chunk_text");
|
---|
| 670 | $target_file_key_to_submission_mapping{$chunk_key} = $target_file_chunk_text;
|
---|
| 671 | }
|
---|
[10019] | 672 | }
|
---|
[24627] | 673 |
|
---|
[10019] | 674 | # -----------------------------------------
|
---|
| 675 | # Validate the translation submissions
|
---|
| 676 | # -----------------------------------------
|
---|
[24627] | 677 |
|
---|
[10019] | 678 | # Check that the translations are valid
|
---|
| 679 | foreach my $chunk_key (keys(%source_file_key_to_submission_mapping)) {
|
---|
[24627] | 680 | # Make sure the submitted chunk still exists in the source file
|
---|
| 681 | if (!defined($source_file_key_to_text_mapping{$chunk_key})) {
|
---|
| 682 | &log_message("Warning: Source chunk $chunk_key no longer exists (ignoring submission).");
|
---|
| 683 | delete $source_file_key_to_submission_mapping{$chunk_key};
|
---|
| 684 | delete $target_file_key_to_submission_mapping{$chunk_key};
|
---|
| 685 | next;
|
---|
| 686 | }
|
---|
| 687 |
|
---|
| 688 | # Make sure the submitted source chunk matches the source file chunk
|
---|
[28518] | 689 | if ($source_file_key_to_submission_mapping{$chunk_key} ne &unmake_text_xml_safe($source_file_key_to_text_mapping{$chunk_key})) {
|
---|
| 690 | #if (&unmake_text_xml_safe($source_file_key_to_submission_mapping{$chunk_key}) ne &unmake_text_xml_safe($source_file_key_to_text_mapping{$chunk_key})) {
|
---|
[28503] | 691 | #print STDERR "**** $source_file_key_to_submission_mapping{$chunk_key}\n";
|
---|
| 692 | #print STDERR "**** " . &unmake_text_xml_safe($source_file_key_to_text_mapping{$chunk_key}) ."\n";
|
---|
| 693 |
|
---|
[24627] | 694 | &log_message("Warning: Source chunk $chunk_key has changed (ignoring submission).");
|
---|
| 695 | &log_message("Submission source: $source_file_key_to_submission_mapping{$chunk_key}");
|
---|
| 696 | &log_message(" Source text: $source_file_key_to_text_mapping{$chunk_key}");
|
---|
| 697 | delete $source_file_key_to_submission_mapping{$chunk_key};
|
---|
| 698 | delete $target_file_key_to_submission_mapping{$chunk_key};
|
---|
| 699 | next;
|
---|
| 700 | }
|
---|
[10027] | 701 | }
|
---|
[24627] | 702 |
|
---|
[10027] | 703 | # Apply the submitted translations
|
---|
| 704 | foreach my $chunk_key (keys(%target_file_key_to_submission_mapping)) {
|
---|
[24627] | 705 | # Only apply the submission if it is a change, unless -force_submission has been specified
|
---|
| 706 | if ($force_submission_flag || !defined($target_file_key_to_text_mapping{$chunk_key}) || $target_file_key_to_submission_mapping{$chunk_key} ne $target_file_key_to_text_mapping{$chunk_key}) {
|
---|
| 707 | $target_file_key_to_text_mapping{$chunk_key} = $target_file_key_to_submission_mapping{$chunk_key};
|
---|
| 708 | $target_file_key_to_gti_comment_mapping{$chunk_key} = "Updated $submission_date by $submitter_username";
|
---|
| 709 | }
|
---|
[10019] | 710 | }
|
---|
[18460] | 711 |
|
---|
[30582] | 712 | if ($translation_file_key !~ m/^gs3/) {
|
---|
[24627] | 713 | eval "&write_translated_${translation_file_type}(\$source_file, \\\@source_file_lines, \\\%source_file_key_to_text_mapping, \$target_file, \\\@target_file_lines, \\\%target_file_key_to_text_mapping, \\\%target_file_key_to_gti_comment_mapping, \$target_language_code)";
|
---|
[18460] | 714 | } else {
|
---|
[30581] | 715 | eval "&write_translated_gs3interface(\$translation_file_key, \\\%source_file_key_to_text_mapping, \\\%target_file_key_to_text_mapping, \\\%target_file_key_to_gti_comment_mapping, \$target_language_code)";
|
---|
[18460] | 716 | }
|
---|
| 717 | }
|
---|
[10019] | 718 |
|
---|
[18460] | 719 |
|
---|
| 720 | sub create_glihelp_zip_file
|
---|
| 721 | {
|
---|
| 722 | my $target_language_code = shift(@_);
|
---|
| 723 | my $translation_file_key = "glihelp";
|
---|
| 724 |
|
---|
| 725 | &log_message("Creating GLI Help zip file for $target_language_code");
|
---|
[24627] | 726 |
|
---|
[18460] | 727 | my ($source_file, $target_file, $translation_file_type) = &get_translation_data_for($target_language_code, $translation_file_key);
|
---|
| 728 |
|
---|
| 729 | my $classpath = &util::filename_cat($gsdl_root_directory, "gti-lib");
|
---|
[28977] | 730 | my $oldclasspath = $classpath;
|
---|
[18460] | 731 | if ( ! -e $classpath) {
|
---|
[28977] | 732 | $classpath = &util::filename_cat($gsdl_root_directory, "gli", "shared");
|
---|
| 733 | }
|
---|
| 734 | if ( ! -e $classpath) {
|
---|
| 735 | &throw_fatal_error("$classpath doesn't exist! (Neither does $oldclasspath.) Need the files in this directory (ApplyXLST and its related files) to create the zip file for GLI Help");
|
---|
| 736 | }
|
---|
| 737 |
|
---|
[28991] | 738 |
|
---|
| 739 | my $perllib_path = &util::filename_cat($gsdl_root_directory, "perllib"); # strings.properties
|
---|
| 740 | my $gliclasses_path = &util::filename_cat($gsdl_root_directory, "gli", "classes"); # dictionary.properties
|
---|
| 741 | my $os = $^O;
|
---|
| 742 | my $path_separator = ($^O =~ m/mswin/i) ? ";" : ":";
|
---|
| 743 | my $xalan_path = &util::filename_cat($classpath, "xalan.jar");
|
---|
| 744 | $classpath = "$perllib_path$path_separator$gliclasses_path$path_separator$classpath$path_separator$xalan_path";
|
---|
| 745 |
|
---|
[18460] | 746 | my $gli_help_directory = &util::filename_cat($gsdl_root_directory, "gli");
|
---|
| 747 | $gli_help_directory = &util::filename_cat($gli_help_directory, "help");
|
---|
| 748 |
|
---|
| 749 | my $gen_many_html_xsl_filepath = &util::filename_cat($gli_help_directory, "gen-many-html.xsl");
|
---|
| 750 | if ( ! -e $gen_many_html_xsl_filepath) {
|
---|
[24627] | 751 | &throw_fatal_error("$gen_many_html_xsl_filepath doesn't exist! Need this file to create the zip file for GLI Help");
|
---|
[18460] | 752 | }
|
---|
[24627] | 753 |
|
---|
[18460] | 754 | my $gen_index_xml_xsl_filepath = &util::filename_cat($gli_help_directory, "gen-index-xml.xsl");
|
---|
| 755 | my $split_script_filepath = &util::filename_cat($gli_help_directory, "splithelpdocument.pl");
|
---|
| 756 |
|
---|
| 757 | my $target_file_directory = &util::filename_cat($gli_help_directory, $target_language_code);
|
---|
| 758 | $target_file_directory = $target_file_directory."/";
|
---|
[24627] | 759 |
|
---|
[18460] | 760 | my $target_filepath = &util::filename_cat($gsdl_root_directory, $target_file);
|
---|
[28991] | 761 |
|
---|
| 762 | # if gli/help/nl doesn't exist, create it by copying over gli/help/en/help.xml, then process the copied file
|
---|
| 763 | my ($tailname, $glihelp_lang_dir, $suffix) = &File::Basename::fileparse($target_filepath, "\\.[^\\.]+\$");
|
---|
| 764 | if(!&FileUtils::directoryExists($glihelp_lang_dir)) {
|
---|
| 765 |
|
---|
| 766 | # copy across the gli/help/en/help.xml into a new folder for the new language gli/help/<newlang>
|
---|
| 767 | my $en_glihelp_dir = &util::filename_cat($gli_help_directory, "en");
|
---|
[28992] | 768 | my $en_helpxml_file = &util::filename_cat($en_glihelp_dir, "$tailname$suffix"); #$tailname$suffix="help.xml"
|
---|
[28991] | 769 | &FileUtils::copyFilesRecursiveNoSVN($en_helpxml_file, $glihelp_lang_dir);
|
---|
| 770 |
|
---|
| 771 | # In gli/help/<newlang>/help.xml, replace all occurrences of
|
---|
| 772 | # <Text id="1">This text in en will be removed for new langcode</Text>
|
---|
| 773 | # with <!-- Missing translation: 1 -->
|
---|
| 774 | open(FIN,"<$target_filepath") or &throw_fatal_error("Could not open $target_filepath for READING after creating it");
|
---|
| 775 | my $help_xml_contents;
|
---|
| 776 | # Read in the entire contents of the file in one hit
|
---|
| 777 | sysread(FIN, $help_xml_contents, -s FIN);
|
---|
| 778 | close(FIN);
|
---|
[24627] | 779 |
|
---|
[28991] | 780 | $help_xml_contents =~ s@<Text id="([^"]+?)">(.*?)</Text>@<!-- Missing translation: $1 -->@sg;
|
---|
| 781 |
|
---|
| 782 | open(FOUT, ">$target_filepath") or &throw_fatal_error("Could not open $target_filepath for WRITING after creating it");
|
---|
| 783 | print FOUT $help_xml_contents;
|
---|
| 784 | close(FOUT);
|
---|
| 785 | }
|
---|
| 786 |
|
---|
[24627] | 787 | my $perl_exec = &util::get_perl_exec();
|
---|
| 788 | my $java_exec = "java";
|
---|
| 789 | if(defined($ENV{'JAVA_HOME'}) && $ENV{'JAVA_HOME'} ne ""){
|
---|
| 790 | $java_exec = &util::filename_cat($ENV{'JAVA_HOME'}, "bin", "java");
|
---|
| 791 | }
|
---|
[28991] | 792 |
|
---|
| 793 | #my $cmd = "$java_exec -cp $classpath:$classpath/xalan.jar ApplyXSLT $target_language_code $gen_many_html_xsl_filepath $target_filepath | \"$perl_exec\" -S $split_script_filepath $target_file_directory";
|
---|
| 794 | my $cmd = "$java_exec -DGSDLHOME=$gsdl_root_directory -cp $classpath ApplyXSLT $target_language_code $gen_many_html_xsl_filepath $target_filepath | \"$perl_exec\" -S $split_script_filepath $target_file_directory";
|
---|
| 795 | #&throw_fatal_error("RAN gti command: $cmd");
|
---|
[18460] | 796 | my $response = `$cmd`;
|
---|
[28991] | 797 |
|
---|
| 798 | #$cmd = "$java_exec -cp $classpath:$classpath/xalan.jar ApplyXSLT $target_language_code $gen_index_xml_xsl_filepath $target_filepath > " . $target_file_directory . "help_index.xml"; # 2>/dev/null";
|
---|
| 799 | $cmd = "$java_exec -cp $classpath -DGSDLHOME=$gsdl_root_directory ApplyXSLT $target_language_code $gen_index_xml_xsl_filepath $target_filepath > " . $target_file_directory . "help_index.xml"; # 2>/dev/null";
|
---|
[18460] | 800 | $response = `$cmd`;
|
---|
[28991] | 801 |
|
---|
| 802 | # create a gti/tmp folder, if one doesn't already exist, and store the downloadable zip file in there
|
---|
| 803 | my $tmpdir = &util::filename_cat($gsdl_root_directory, "tmp");
|
---|
| 804 | if(!&FileUtils::directoryExists($tmpdir)) {
|
---|
| 805 | &FileUtils::makeDirectory($tmpdir);
|
---|
| 806 | }
|
---|
[28977] | 807 | #my $zip_file_path = "/greenstone/custom/gti/" . $target_language_code . "_GLIHelp.zip";
|
---|
[28991] | 808 | my $zip_file_path = &util::filename_cat($tmpdir, $target_language_code . "_GLIHelp.zip");
|
---|
[18460] | 809 | $cmd = "zip -rj $zip_file_path $target_file_directory -i \*.htm \*.xml";
|
---|
[28991] | 810 |
|
---|
[18460] | 811 | $response = `$cmd`;
|
---|
[10019] | 812 | }
|
---|
| 813 |
|
---|
| 814 |
|
---|
| 815 | sub get_translation_configuration
|
---|
| 816 | {
|
---|
| 817 | # Get the code of the target language
|
---|
| 818 | my $target_language_code = shift(@_);
|
---|
| 819 | # Get the key of the file to translate
|
---|
| 820 | my $translation_file_key = shift(@_);
|
---|
[24627] | 821 |
|
---|
[10019] | 822 | # Read the translation data from the gti.cfg file
|
---|
| 823 | my ($source_file, $target_file, $translation_file_type) =
|
---|
| 824 | &get_translation_data_for($target_language_code, $translation_file_key);
|
---|
[24627] | 825 |
|
---|
[10019] | 826 | # Check that the file to translate is defined in the gti.cfg file
|
---|
| 827 | if (!$source_file || !$target_file || !$translation_file_type) {
|
---|
[24627] | 828 | &throw_fatal_error("Missing or incomplete specification for translation file \"$translation_file_key\" in gti.pl.");
|
---|
[10019] | 829 | }
|
---|
[24627] | 830 |
|
---|
[10019] | 831 | # Check that the source file exists
|
---|
| 832 | my $source_file_path = &util::filename_cat($gsdl_root_directory, $source_file);
|
---|
| 833 | if (!-e $source_file_path) {
|
---|
[24627] | 834 | &throw_fatal_error("Source file $source_file_path does not exist.");
|
---|
[10019] | 835 | }
|
---|
[24627] | 836 |
|
---|
[10019] | 837 | # Check that the source file is up to date
|
---|
[11104] | 838 | # The "2>/dev/null" is very important! If it is missing this will never return when run from the receptionist
|
---|
[10019] | 839 | # unless ($translation_file_is_not_in_cvs) {
|
---|
[14258] | 840 | #my $source_file_cvs_status = `cd $gsdl_root_directory; cvs -d $anonymous_cvs_root update $source_file 2>/dev/null`;
|
---|
[24627] | 841 | my $source_file_cvs_status = `cd $gsdl_root_directory; svn status $source_file 2>/dev/null`;
|
---|
[10019] | 842 | if ($source_file_cvs_status =~ /^C /) {
|
---|
| 843 | &throw_fatal_error("Source file $source_file_path conflicts with the repository.");
|
---|
| 844 | }
|
---|
| 845 | if ($source_file_cvs_status =~ /^M /) {
|
---|
| 846 | &throw_fatal_error("Source file $source_file_path contains uncommitted changes.");
|
---|
| 847 | }
|
---|
| 848 | # }
|
---|
[24627] | 849 |
|
---|
[10019] | 850 | return ($source_file, $target_file, $translation_file_type);
|
---|
| 851 | }
|
---|
| 852 |
|
---|
| 853 |
|
---|
| 854 | sub get_translation_data_for
|
---|
| 855 | {
|
---|
| 856 | my ($target_language_code, $translation_file_key) = @_;
|
---|
[24627] | 857 |
|
---|
[10019] | 858 | foreach my $translation_file (@$gti_translation_files) {
|
---|
[24627] | 859 | # If this isn't the correct translation file, move onto the next one
|
---|
| 860 | next if ($translation_file_key ne $translation_file->{'key'});
|
---|
| 861 |
|
---|
| 862 | # Resolve the target language file
|
---|
| 863 | my $target_language_file = $translation_file->{'target_file'};
|
---|
| 864 | if ($target_language_file =~ /(\{.+\;.+\})/) {
|
---|
| 865 | my $unresolved_target_language_file_part = $1;
|
---|
| 866 |
|
---|
| 867 | # Check for a special case for the target language code
|
---|
| 868 | if ($unresolved_target_language_file_part =~ /(\{|\;)$target_language_code:([^\;]+)(\;|\})/) {
|
---|
| 869 | my $resolved_target_language_file_part = $2;
|
---|
| 870 | $target_language_file =~ s/$unresolved_target_language_file_part/$resolved_target_language_file_part/;
|
---|
| 871 | }
|
---|
| 872 | # Otherwise use the last part as the default value
|
---|
| 873 | else {
|
---|
| 874 | my ($default_target_language_file_part) = $unresolved_target_language_file_part =~ /([^\;]+)\}/;
|
---|
| 875 | $target_language_file =~ s/$unresolved_target_language_file_part/\{$default_target_language_file_part\}/;
|
---|
[10019] | 876 | }
|
---|
[10041] | 877 | }
|
---|
[24627] | 878 |
|
---|
[10041] | 879 | # Resolve instances of {iso_639_1_target_language_name}
|
---|
| 880 | my $iso_639_1_target_language_name = $iso639::fromiso639{$target_language_code};
|
---|
| 881 | $iso_639_1_target_language_name =~ tr/A-Z/a-z/ if $iso_639_1_target_language_name;
|
---|
| 882 | $target_language_file =~ s/\{iso_639_1_target_language_name\}/$iso_639_1_target_language_name/g;
|
---|
[24627] | 883 |
|
---|
[10041] | 884 | # Resolve instances of {target_language_code}
|
---|
| 885 | $target_language_file =~ s/\{target_language_code\}/$target_language_code/g;
|
---|
[24627] | 886 |
|
---|
[10041] | 887 | return ($translation_file->{'source_file'}, $target_language_file, $translation_file->{'file_type'});
|
---|
[24627] | 888 | }
|
---|
[10019] | 889 |
|
---|
[24627] | 890 | return ();
|
---|
[10019] | 891 | }
|
---|
| 892 |
|
---|
| 893 |
|
---|
| 894 | sub read_file_lines
|
---|
| 895 | {
|
---|
| 896 | my ($file_path) = @_;
|
---|
[24627] | 897 |
|
---|
[10019] | 898 | if (!open(FILE_IN, "<$file_path")) {
|
---|
[24627] | 899 | &log_message("Note: Could not open file $file_path.");
|
---|
| 900 | return ();
|
---|
[10019] | 901 | }
|
---|
| 902 | my @file_lines = <FILE_IN>;
|
---|
| 903 | close(FILE_IN);
|
---|
[24627] | 904 |
|
---|
[10019] | 905 | return @file_lines;
|
---|
| 906 | }
|
---|
| 907 |
|
---|
| 908 |
|
---|
| 909 | sub build_key_to_line_mapping
|
---|
| 910 | {
|
---|
| 911 | my ($file_lines, $translation_file_type) = @_;
|
---|
| 912 | eval "return &build_key_to_line_mapping_for_${translation_file_type}(\@\$file_lines)";
|
---|
| 913 | }
|
---|
| 914 |
|
---|
| 915 |
|
---|
| 916 | sub build_key_to_text_mapping
|
---|
| 917 | {
|
---|
| 918 | my ($file_lines, $key_to_line_mapping, $translation_file_type) = @_;
|
---|
[24627] | 919 |
|
---|
[10019] | 920 | my %key_to_text_mapping = ();
|
---|
| 921 | foreach my $chunk_key (keys(%$key_to_line_mapping)) {
|
---|
[24627] | 922 | my $chunk_starting_line = (split(/-/, $key_to_line_mapping->{$chunk_key}))[0];
|
---|
| 923 | my $chunk_finishing_line = (split(/-/, $key_to_line_mapping->{$chunk_key}))[1];
|
---|
| 924 |
|
---|
| 925 | my $chunk_text = @$file_lines[$chunk_starting_line];
|
---|
| 926 | for (my $l = ($chunk_starting_line + 1); $l <= $chunk_finishing_line; $l++) {
|
---|
| 927 | $chunk_text .= @$file_lines[$l];
|
---|
| 928 | }
|
---|
| 929 |
|
---|
| 930 | # Map from chunk key to text
|
---|
| 931 | eval "\$key_to_text_mapping{\${chunk_key}} = &import_chunk_from_${translation_file_type}(\$chunk_text)";
|
---|
[10019] | 932 | }
|
---|
[24627] | 933 |
|
---|
[10019] | 934 | return %key_to_text_mapping;
|
---|
| 935 | }
|
---|
| 936 |
|
---|
| 937 |
|
---|
| 938 | sub build_key_to_last_update_date_mapping
|
---|
| 939 | {
|
---|
| 940 | my ($file, $file_lines, $key_to_line_mapping, $translation_file_type) = @_;
|
---|
[24627] | 941 |
|
---|
[10019] | 942 | # If the files aren't in CVS then we can't tell anything about what needs updating
|
---|
| 943 | # return () if ($translation_file_is_not_in_cvs);
|
---|
[24627] | 944 |
|
---|
[10019] | 945 | # Build a mapping from key to CVS date
|
---|
| 946 | # Need to be careful with this mapping because the chunk keys won't necessarily all be valid
|
---|
| 947 | my %key_to_cvs_date_mapping = &build_key_to_cvs_date_mapping($file, $translation_file_type);
|
---|
[24627] | 948 |
|
---|
[10019] | 949 | # Build a mapping from key to comment date
|
---|
[12483] | 950 | my %key_to_gti_comment_mapping = &build_key_to_gti_comment_mapping($file_lines, $key_to_line_mapping, $translation_file_type);
|
---|
[24627] | 951 |
|
---|
[10019] | 952 | # Build a mapping from key to last update date (the latter of the CVS date and comment date)
|
---|
| 953 | my %key_to_last_update_date_mapping = ();
|
---|
| 954 | foreach my $chunk_key (keys(%$key_to_line_mapping)) {
|
---|
[24627] | 955 | # Use the CVS date as a starting point
|
---|
| 956 | my $chunk_cvs_date = $key_to_cvs_date_mapping{$chunk_key};
|
---|
| 957 | $key_to_last_update_date_mapping{$chunk_key} = $chunk_cvs_date;
|
---|
| 958 |
|
---|
| 959 | # If a comment date exists and it is after the CVS date, use that instead
|
---|
[14258] | 960 | # need to convert the comment date format to SVN format
|
---|
[24627] | 961 | my $chunk_gti_comment = $key_to_gti_comment_mapping{$chunk_key};
|
---|
| 962 | if (defined($chunk_gti_comment) && $chunk_gti_comment =~ /(\d?\d-\D\D\D-\d\d\d\d)/) {
|
---|
| 963 | my $chunk_comment_date = $1;
|
---|
| 964 | if ((!defined($chunk_cvs_date) || &is_date_after($chunk_comment_date, $chunk_cvs_date))) {
|
---|
| 965 | $key_to_last_update_date_mapping{$chunk_key} = $chunk_comment_date;
|
---|
| 966 | }
|
---|
| 967 | }
|
---|
[10019] | 968 | }
|
---|
[24627] | 969 |
|
---|
[10019] | 970 | return %key_to_last_update_date_mapping;
|
---|
| 971 | }
|
---|
| 972 |
|
---|
| 973 |
|
---|
| 974 | sub build_key_to_cvs_date_mapping
|
---|
| 975 | {
|
---|
| 976 | my ($filename, $translation_file_type) = @_;
|
---|
[24627] | 977 |
|
---|
| 978 | # Use SVN to annotate each line of the file with the date it was last edited
|
---|
[10019] | 979 | # The "2>/dev/null" is very important! If it is missing this will never return when run from the receptionist
|
---|
[24627] | 980 | my $cvs_annotated_file = `cd $gsdl_root_directory; svn annotate -v --force $filename 2>/dev/null`;
|
---|
[14258] | 981 |
|
---|
[10019] | 982 | my @cvs_annotated_file_lines = split(/\n/, $cvs_annotated_file);
|
---|
[24627] | 983 |
|
---|
[10019] | 984 | my @cvs_annotated_file_lines_date = ();
|
---|
| 985 | foreach my $cvs_annotated_file_line (@cvs_annotated_file_lines) {
|
---|
[24627] | 986 | # Extract the date from the SVN annotation at the front
|
---|
| 987 | # svn format : 2007-07-16
|
---|
[14258] | 988 | $cvs_annotated_file_line =~ s/^\s+\S+\s+\S+\s(\S+)//;
|
---|
| 989 |
|
---|
| 990 | push(@cvs_annotated_file_lines_date, $1);
|
---|
| 991 |
|
---|
| 992 | # trim extra date information in svn annotation format
|
---|
| 993 | # 15:42:49 +1200 (Wed, 21 Jun 2006)
|
---|
| 994 | $cvs_annotated_file_line =~ s/^\s+\S+\s\S+\s\((.+?)\)\s//;
|
---|
| 995 | }
|
---|
| 996 |
|
---|
[10019] | 997 | # Build a key to line mapping for the CVS annotated file, for matching the chunk key to the CVS date
|
---|
| 998 | my %key_to_line_mapping = &build_key_to_line_mapping(\@cvs_annotated_file_lines, $translation_file_type);
|
---|
[24627] | 999 |
|
---|
[10019] | 1000 | my %key_to_cvs_date_mapping = ();
|
---|
| 1001 | foreach my $chunk_key (keys(%key_to_line_mapping)) {
|
---|
[24627] | 1002 | my $chunk_starting_line = (split(/-/, $key_to_line_mapping{$chunk_key}))[0];
|
---|
| 1003 | my $chunk_finishing_line = (split(/-/, $key_to_line_mapping{$chunk_key}))[1];
|
---|
| 1004 |
|
---|
| 1005 | # Find the date this chunk was last edited, from the CVS annotation
|
---|
| 1006 | my $chunk_date = $cvs_annotated_file_lines_date[$chunk_starting_line];
|
---|
| 1007 | for (my $l = ($chunk_starting_line + 1); $l <= $chunk_finishing_line; $l++) {
|
---|
| 1008 | if (&is_date_after($cvs_annotated_file_lines_date[$l], $chunk_date)) {
|
---|
| 1009 | # This part of the chunk has been updated more recently
|
---|
| 1010 | $chunk_date = $cvs_annotated_file_lines_date[$l];
|
---|
| 1011 |
|
---|
| 1012 | }
|
---|
| 1013 | }
|
---|
| 1014 |
|
---|
| 1015 | # Map from chunk key to CVS date
|
---|
| 1016 | $key_to_cvs_date_mapping{$chunk_key} = $chunk_date;
|
---|
[10019] | 1017 | }
|
---|
[24627] | 1018 |
|
---|
[10019] | 1019 | return %key_to_cvs_date_mapping;
|
---|
| 1020 | }
|
---|
| 1021 |
|
---|
| 1022 |
|
---|
[12483] | 1023 | sub build_key_to_gti_comment_mapping
|
---|
[10019] | 1024 | {
|
---|
| 1025 | my ($file_lines, $key_to_line_mapping, $translation_file_type) = @_;
|
---|
[24627] | 1026 |
|
---|
[12483] | 1027 | my %key_to_gti_comment_mapping = ();
|
---|
[10019] | 1028 | foreach my $chunk_key (keys(%$key_to_line_mapping)) {
|
---|
[24627] | 1029 | my $chunk_starting_line = (split(/-/, $key_to_line_mapping->{$chunk_key}))[0];
|
---|
| 1030 | my $chunk_finishing_line = (split(/-/, $key_to_line_mapping->{$chunk_key}))[1];
|
---|
| 1031 |
|
---|
| 1032 | my $chunk_text = @$file_lines[$chunk_starting_line];
|
---|
| 1033 | for (my $l = ($chunk_starting_line + 1); $l <= $chunk_finishing_line; $l++) {
|
---|
| 1034 | $chunk_text .= @$file_lines[$l];
|
---|
| 1035 | }
|
---|
| 1036 |
|
---|
| 1037 | # Map from chunk key to GTI comment
|
---|
| 1038 | my $chunk_gti_comment;
|
---|
| 1039 | eval "\$chunk_gti_comment = &get_${translation_file_type}_chunk_gti_comment(\$chunk_text)";
|
---|
| 1040 | $key_to_gti_comment_mapping{$chunk_key} = $chunk_gti_comment if (defined($chunk_gti_comment));
|
---|
[10019] | 1041 | }
|
---|
[24627] | 1042 |
|
---|
[12483] | 1043 | return %key_to_gti_comment_mapping;
|
---|
[10019] | 1044 | }
|
---|
| 1045 |
|
---|
| 1046 |
|
---|
| 1047 | sub determine_chunks_requiring_translation
|
---|
| 1048 | {
|
---|
| 1049 | my $source_file_key_to_text_mapping = shift(@_);
|
---|
| 1050 | my $target_file_key_to_text_mapping = shift(@_);
|
---|
[24627] | 1051 |
|
---|
[10019] | 1052 | # Chunks needing translation are those in the source file with no translation in the target file
|
---|
| 1053 | my @target_file_keys_requiring_translation = ();
|
---|
| 1054 | foreach my $chunk_key (keys(%$source_file_key_to_text_mapping)) {
|
---|
[24627] | 1055 | if ($source_file_key_to_text_mapping->{$chunk_key} && !$target_file_key_to_text_mapping->{$chunk_key}) {
|
---|
| 1056 | # &log_message("Chunk with key $chunk_key needs translating.");
|
---|
| 1057 | push(@target_file_keys_requiring_translation, $chunk_key);
|
---|
| 1058 | }
|
---|
[10019] | 1059 | }
|
---|
[24627] | 1060 |
|
---|
[10019] | 1061 | return @target_file_keys_requiring_translation;
|
---|
| 1062 | }
|
---|
| 1063 |
|
---|
| 1064 |
|
---|
| 1065 | sub determine_chunks_requiring_updating
|
---|
| 1066 | {
|
---|
| 1067 | my $source_file_key_to_last_update_date_mapping = shift(@_);
|
---|
| 1068 | my $target_file_key_to_last_update_date_mapping = shift(@_);
|
---|
[24627] | 1069 |
|
---|
[10019] | 1070 | # Chunks needing updating are those in the target file that have been more recently edited in the source file
|
---|
| 1071 | my @target_file_keys_requiring_updating = ();
|
---|
| 1072 | foreach my $chunk_key (keys(%$source_file_key_to_last_update_date_mapping)) {
|
---|
[24627] | 1073 | my $source_chunk_last_update_date = $source_file_key_to_last_update_date_mapping->{$chunk_key};
|
---|
| 1074 | my $target_chunk_last_update_date = $target_file_key_to_last_update_date_mapping->{$chunk_key};
|
---|
[14258] | 1075 |
|
---|
[30548] | 1076 | # print "key: $chunk_key\nsource date : $source_chunk_last_update_date\ntarget date : $target_chunk_last_update_date\nafter? ". &is_date_after($source_chunk_last_update_date, $target_chunk_last_update_date) . "\n\n";
|
---|
| 1077 |
|
---|
[14258] | 1078 | if (defined($target_chunk_last_update_date) && &is_date_after($source_chunk_last_update_date, $target_chunk_last_update_date)) {
|
---|
[24627] | 1079 | # &log_message("Chunk with key $chunk_key needs updating.");
|
---|
[30548] | 1080 | # &log_message("key: $chunk_key\nsource date : $source_chunk_last_update_date\ntarget date : $target_chunk_last_update_date\nafter? ". &is_date_after($source_chunk_last_update_date, $target_chunk_last_update_date) . "\n\n");
|
---|
[24627] | 1081 | push(@target_file_keys_requiring_updating, $chunk_key);
|
---|
| 1082 | }
|
---|
[10019] | 1083 | }
|
---|
[24627] | 1084 |
|
---|
[10019] | 1085 | return @target_file_keys_requiring_updating;
|
---|
| 1086 | }
|
---|
| 1087 |
|
---|
| 1088 |
|
---|
| 1089 | sub is_chunk_automatically_translated
|
---|
| 1090 | {
|
---|
| 1091 | my ($chunk_key, $translation_file_type) = @_;
|
---|
| 1092 | eval "return &is_${translation_file_type}_chunk_automatically_translated(\$chunk_key)";
|
---|
| 1093 | }
|
---|
| 1094 |
|
---|
| 1095 |
|
---|
| 1096 | sub make_text_xml_safe
|
---|
| 1097 | {
|
---|
| 1098 | my $text = shift(@_);
|
---|
[11452] | 1099 | $text =~ s/\&/\&\;/g;
|
---|
| 1100 | $text =~ s/\&\;lt\;/\&\;amp\;lt\;/g;
|
---|
| 1101 | $text =~ s/\&\;gt\;/\&\;amp\;gt\;/g;
|
---|
[11498] | 1102 | $text =~ s/\&\;rarr\;/\&\;amp\;rarr\;/g;
|
---|
| 1103 | $text =~ s/\&\;mdash\;/\&\;amp\;mdash\;/g;
|
---|
[10019] | 1104 | $text =~ s/</\<\;/g;
|
---|
| 1105 | $text =~ s/>/\>\;/g;
|
---|
| 1106 | return $text;
|
---|
| 1107 | }
|
---|
| 1108 |
|
---|
| 1109 |
|
---|
[11448] | 1110 | sub unmake_text_xml_safe
|
---|
| 1111 | {
|
---|
| 1112 | my $text = shift(@_);
|
---|
| 1113 | $text =~ s/\<\;/</g;
|
---|
| 1114 | $text =~ s/\>\;/>/g;
|
---|
| 1115 | $text =~ s/\&\;/\&/g;
|
---|
| 1116 | return $text;
|
---|
| 1117 | }
|
---|
| 1118 |
|
---|
| 1119 |
|
---|
[10019] | 1120 | # Returns 1 if $date1 is after $date2, 0 otherwise
|
---|
[14258] | 1121 | sub is_date_after_cvs
|
---|
[10019] | 1122 | {
|
---|
| 1123 | my ($date1, $date2) = @_;
|
---|
| 1124 | my %months = ("Jan", 1, "Feb", 2, "Mar", 3, "Apr", 4, "May", 5, "Jun", 6,
|
---|
[24627] | 1125 | "Jul", 7, "Aug", 8, "Sep", 9, "Oct", 10, "Nov", 11, "Dec", 12);
|
---|
| 1126 |
|
---|
[14258] | 1127 | if(!defined $date1) {
|
---|
| 1128 | return 1;
|
---|
| 1129 | }
|
---|
[24627] | 1130 |
|
---|
[10019] | 1131 | my @date1parts = split(/-/, $date1);
|
---|
| 1132 | my @date2parts = split(/-/, $date2);
|
---|
[24627] | 1133 |
|
---|
[10019] | 1134 | # Compare year - nasty because we have rolled over into a new century
|
---|
| 1135 | my $year1 = $date1parts[2];
|
---|
| 1136 | if ($year1 < 80) {
|
---|
[14258] | 1137 | $year1 += 2000;
|
---|
[10019] | 1138 | }
|
---|
| 1139 | my $year2 = $date2parts[2];
|
---|
| 1140 | if ($year2 < 80) {
|
---|
[14258] | 1141 | $year2 += 2000;
|
---|
[10019] | 1142 | }
|
---|
[24627] | 1143 |
|
---|
[10019] | 1144 | # Compare year
|
---|
| 1145 | if ($year1 > $year2) {
|
---|
[24627] | 1146 | return 1;
|
---|
[10019] | 1147 | }
|
---|
| 1148 | elsif ($year1 == $year2) {
|
---|
[24627] | 1149 | # Year is the same, so compare month
|
---|
| 1150 | if ($months{$date1parts[1]} > $months{$date2parts[1]}) {
|
---|
| 1151 | return 1;
|
---|
| 1152 | }
|
---|
| 1153 | elsif ($months{$date1parts[1]} == $months{$date2parts[1]}) {
|
---|
| 1154 | # Month is the same, so compare day
|
---|
| 1155 | if ($date1parts[0] > $date2parts[0]) {
|
---|
| 1156 | return 1;
|
---|
| 1157 | }
|
---|
| 1158 | }
|
---|
[10019] | 1159 | }
|
---|
[24627] | 1160 |
|
---|
[10019] | 1161 | return 0;
|
---|
| 1162 | }
|
---|
| 1163 |
|
---|
[14258] | 1164 | sub is_date_after
|
---|
| 1165 | {
|
---|
| 1166 | my ($date1, $date2) = @_;
|
---|
| 1167 |
|
---|
| 1168 | if(!defined $date1) {
|
---|
[24627] | 1169 | return 1;
|
---|
[14258] | 1170 | }
|
---|
| 1171 | if(!defined $date2) {
|
---|
[24627] | 1172 | return 0;
|
---|
[14258] | 1173 | }
|
---|
| 1174 |
|
---|
| 1175 | # 16-Aug-2006
|
---|
| 1176 | if($date1=~ /(\d+?)-(\S\S\S)-(\d\d\d\d)/){
|
---|
[24627] | 1177 | my %months = ("Jan", "01", "Feb", "02", "Mar", "03", "Apr", "04", "May", "05", "Jun", "06",
|
---|
| 1178 | "Jul", "07", "Aug", "08", "Sep", "09", "Oct", "10", "Nov", "11", "Dec", "12");
|
---|
| 1179 | $date1=$3 . "-" . $months{$2} . "-" . $1;
|
---|
| 1180 | # print "** converted date1: $date1\n";
|
---|
[14258] | 1181 | }
|
---|
| 1182 | if($date2=~ /(\d+?)-(\S\S\S)-(\d\d\d\d)/){
|
---|
[24627] | 1183 | my %months = ("Jan", "01", "Feb", "02", "Mar", "03", "Apr", "04", "May", "05", "Jun", "06",
|
---|
| 1184 | "Jul", "07", "Aug", "08", "Sep", "09", "Oct", "10", "Nov", "11", "Dec", "12");
|
---|
| 1185 | $date2=$3 . "-" . $months{$2} . "-" . $1;
|
---|
| 1186 | # print "** converted date2: $date2\n";
|
---|
[14258] | 1187 | }
|
---|
| 1188 |
|
---|
| 1189 |
|
---|
| 1190 | # 2006-08-16
|
---|
| 1191 | my @date1parts = split(/-/, $date1);
|
---|
| 1192 | my @date2parts = split(/-/, $date2);
|
---|
| 1193 |
|
---|
| 1194 | # Compare year
|
---|
| 1195 | if ($date1parts[0] > $date2parts[0]) {
|
---|
[24627] | 1196 | return 1;
|
---|
[14258] | 1197 | }
|
---|
| 1198 | elsif ($date1parts[0] == $date2parts[0]) {
|
---|
[24627] | 1199 | # Year is the same, so compare month
|
---|
| 1200 | if ($date1parts[1] > $date2parts[1]) {
|
---|
| 1201 | return 1;
|
---|
| 1202 | }
|
---|
| 1203 | elsif ($date1parts[1] == $date2parts[1]) {
|
---|
| 1204 | # Month is the same, so compare day
|
---|
| 1205 | if ($date1parts[2] > $date2parts[2]) {
|
---|
| 1206 | return 1;
|
---|
| 1207 | }
|
---|
| 1208 | }
|
---|
[14258] | 1209 | }
|
---|
| 1210 |
|
---|
| 1211 | return 0;
|
---|
| 1212 | }
|
---|
[10019] | 1213 |
|
---|
[14258] | 1214 |
|
---|
[18460] | 1215 | sub create_xml_response_for_chunks_requiring_work
|
---|
| 1216 | {
|
---|
| 1217 | my ($translation_file_key, $target_file, $total_num_chunks, $target_files_keys_requiring_translation, $target_files_keys_requiring_updating, $num_chunks_to_return, $source_files_key_to_text_mapping, $target_files_key_to_text_mapping, $source_files_key_to_last_update_date_mapping, $target_files_key_to_last_update_date_mapping) = @_;
|
---|
[24627] | 1218 |
|
---|
[18460] | 1219 | # Form an XML response to the command
|
---|
| 1220 | my $xml_response = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
|
---|
| 1221 | $xml_response .= "<GTIResponse>\n";
|
---|
| 1222 | $xml_response .= " <TranslationFile"
|
---|
| 1223 | . " key=\"" . $translation_file_key . "\""
|
---|
| 1224 | . " target_file_path=\"" . $target_file . "\""
|
---|
| 1225 | . " num_chunks_translated=\"" . ($total_num_chunks - scalar(@$target_files_keys_requiring_translation)) . "\""
|
---|
| 1226 | . " num_chunks_requiring_translation=\"" . scalar(@$target_files_keys_requiring_translation) . "\""
|
---|
| 1227 | . " num_chunks_requiring_updating=\"" . scalar(@$target_files_keys_requiring_updating) . "\"\/>\n";
|
---|
[24627] | 1228 |
|
---|
[18460] | 1229 | # Do chunks requiring translation first
|
---|
| 1230 | if ($num_chunks_to_return > scalar(@$target_files_keys_requiring_translation)) {
|
---|
[24627] | 1231 | $xml_response .= " <ChunksRequiringTranslation size=\"" . scalar(@$target_files_keys_requiring_translation) . "\">\n";
|
---|
[18460] | 1232 | }
|
---|
| 1233 | else {
|
---|
[24627] | 1234 | $xml_response .= " <ChunksRequiringTranslation size=\"" . $num_chunks_to_return . "\">\n";
|
---|
[18460] | 1235 | }
|
---|
[24627] | 1236 |
|
---|
[18460] | 1237 | my @sorted_chunk_keys = sort (@$target_files_keys_requiring_translation);
|
---|
| 1238 | foreach my $chunk_key (@sorted_chunk_keys) {
|
---|
[24627] | 1239 | last if ($num_chunks_to_return == 0);
|
---|
| 1240 |
|
---|
| 1241 | my $source_file_chunk_date = $source_files_key_to_last_update_date_mapping->{$chunk_key} || "";
|
---|
| 1242 | my $source_file_chunk_text = &make_text_xml_safe($source_files_key_to_text_mapping->{$chunk_key});
|
---|
| 1243 |
|
---|
| 1244 | $xml_response .= " <Chunk key=\"" . &make_text_xml_safe($chunk_key) . "\">\n";
|
---|
| 1245 | $xml_response .= " <SourceFileText date=\"$source_file_chunk_date\">$source_file_chunk_text</SourceFileText>\n";
|
---|
| 1246 | $xml_response .= " <TargetFileText></TargetFileText>\n";
|
---|
| 1247 | $xml_response .= " </Chunk>\n";
|
---|
| 1248 |
|
---|
| 1249 | $num_chunks_to_return--;
|
---|
| 1250 | }
|
---|
[18460] | 1251 |
|
---|
| 1252 | $xml_response .= " </ChunksRequiringTranslation>\n";
|
---|
[24627] | 1253 |
|
---|
[18460] | 1254 | # Then do chunks requiring updating
|
---|
| 1255 | if ($num_chunks_to_return > scalar(@$target_files_keys_requiring_updating)) {
|
---|
[24627] | 1256 | $xml_response .= " <ChunksRequiringUpdating size=\"" . scalar(@$target_files_keys_requiring_updating) . "\">\n";
|
---|
[18460] | 1257 | }
|
---|
| 1258 | else {
|
---|
[24627] | 1259 | $xml_response .= " <ChunksRequiringUpdating size=\"" . $num_chunks_to_return . "\">\n";
|
---|
[18460] | 1260 | }
|
---|
[24627] | 1261 |
|
---|
[18460] | 1262 | # foreach my $chunk_key (@target_file_keys_requiring_updating) {
|
---|
| 1263 | @sorted_chunk_keys = sort (@$target_files_keys_requiring_updating);
|
---|
| 1264 | foreach my $chunk_key (@sorted_chunk_keys) {
|
---|
[24627] | 1265 | last if ($num_chunks_to_return == 0);
|
---|
| 1266 |
|
---|
| 1267 | my $source_file_chunk_date = $source_files_key_to_last_update_date_mapping->{$chunk_key} || "";
|
---|
| 1268 | my $source_file_chunk_text = &make_text_xml_safe($source_files_key_to_text_mapping->{$chunk_key});
|
---|
| 1269 | my $target_file_chunk_date = $target_files_key_to_last_update_date_mapping->{$chunk_key} || "";
|
---|
| 1270 | my $target_file_chunk_text = &make_text_xml_safe($target_files_key_to_text_mapping->{$chunk_key});
|
---|
| 1271 |
|
---|
| 1272 | $xml_response .= " <Chunk key=\"" . &make_text_xml_safe($chunk_key) . "\">\n";
|
---|
| 1273 | $xml_response .= " <SourceFileText date=\"$source_file_chunk_date\">$source_file_chunk_text</SourceFileText>\n";
|
---|
| 1274 | $xml_response .= " <TargetFileText date=\"$target_file_chunk_date\">$target_file_chunk_text</TargetFileText>\n";
|
---|
| 1275 | $xml_response .= " </Chunk>\n";
|
---|
| 1276 |
|
---|
| 1277 | $num_chunks_to_return--;
|
---|
[18460] | 1278 | }
|
---|
[24627] | 1279 |
|
---|
[18460] | 1280 | $xml_response .= " </ChunksRequiringUpdating>\n";
|
---|
[24627] | 1281 |
|
---|
[18460] | 1282 | $xml_response .= "</GTIResponse>\n";
|
---|
[24627] | 1283 |
|
---|
[18460] | 1284 | return $xml_response;
|
---|
| 1285 | }
|
---|
| 1286 |
|
---|
[25249] | 1287 | sub create_xml_response_for_uptodate_chunks
|
---|
| 1288 | {
|
---|
| 1289 | my ($translation_file_key, $target_file, $uptodate_target_files_keys, $source_files_key_to_text_mapping, $target_files_key_to_text_mapping, $source_files_key_to_last_update_date_mapping, $target_files_key_to_last_update_date_mapping) = @_;
|
---|
| 1290 |
|
---|
| 1291 | # Form an XML response to the command
|
---|
| 1292 | my $xml_response = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
|
---|
| 1293 | $xml_response .= "<GTIResponse>\n";
|
---|
| 1294 | $xml_response .= " <TranslationFile"
|
---|
| 1295 | . " key=\"" . $translation_file_key . "\""
|
---|
| 1296 | . " target_file_path=\"" . $target_file . "\""
|
---|
| 1297 | . " num_chunks_uptodate=\"" . scalar(@$uptodate_target_files_keys) . "\"\/>\n";
|
---|
| 1298 |
|
---|
| 1299 |
|
---|
| 1300 | # Then do chunks requiring updating
|
---|
| 1301 | $xml_response .= " <UptodateChunks size=\"" . scalar(@$uptodate_target_files_keys) . "\">\n";
|
---|
| 1302 |
|
---|
| 1303 |
|
---|
| 1304 | # foreach my $chunk_key (@uptodate_target_file_keys) {
|
---|
| 1305 | my @sorted_chunk_keys = sort (@$uptodate_target_files_keys);
|
---|
| 1306 | foreach my $chunk_key (@sorted_chunk_keys) {
|
---|
| 1307 |
|
---|
| 1308 | my $source_file_chunk_date = $source_files_key_to_last_update_date_mapping->{$chunk_key} || "";
|
---|
| 1309 | my $source_file_chunk_text = &make_text_xml_safe($source_files_key_to_text_mapping->{$chunk_key});
|
---|
| 1310 | my $target_file_chunk_date = $target_files_key_to_last_update_date_mapping->{$chunk_key} || "";
|
---|
| 1311 | my $target_file_chunk_text = &make_text_xml_safe($target_files_key_to_text_mapping->{$chunk_key});
|
---|
| 1312 |
|
---|
| 1313 | $xml_response .= " <Chunk key=\"" . &make_text_xml_safe($chunk_key) . "\">\n";
|
---|
| 1314 | $xml_response .= " <SourceFileText date=\"$source_file_chunk_date\">$source_file_chunk_text</SourceFileText>\n";
|
---|
| 1315 | $xml_response .= " <TargetFileText date=\"$target_file_chunk_date\">$target_file_chunk_text</TargetFileText>\n";
|
---|
| 1316 | $xml_response .= " </Chunk>\n";
|
---|
[18460] | 1317 |
|
---|
[25249] | 1318 | }
|
---|
| 1319 |
|
---|
| 1320 | $xml_response .= " </UptodateChunks>\n";
|
---|
| 1321 |
|
---|
| 1322 | $xml_response .= "</GTIResponse>\n";
|
---|
| 1323 |
|
---|
| 1324 | return $xml_response;
|
---|
| 1325 | }
|
---|
| 1326 |
|
---|
[18460] | 1327 | sub create_xml_response_for_all_chunks
|
---|
| 1328 | {
|
---|
| 1329 | my ($translation_file_key, $target_file, $source_file_key_to_text_mapping, $target_file_key_to_text_mapping, $source_file_key_to_last_update_date_mapping, $target_file_key_to_last_update_date_mapping) = @_;
|
---|
[24627] | 1330 |
|
---|
[18460] | 1331 | # Form an XML response to the command
|
---|
| 1332 | my $xml_response = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
|
---|
| 1333 | $xml_response .= "<GTIResponse>\n";
|
---|
| 1334 | $xml_response .= " <TranslationFile"
|
---|
| 1335 | . " key=\"" . $translation_file_key . "\""
|
---|
| 1336 | . " target_file_path=\"" . $target_file . "\"\/>\n";
|
---|
| 1337 |
|
---|
| 1338 | # Do all the chunks
|
---|
| 1339 | $xml_response .= " <Chunks size=\"" . scalar(keys(%$source_file_key_to_text_mapping)) . "\">\n";
|
---|
[24627] | 1340 |
|
---|
[18460] | 1341 | my @sorted_chunk_keys = sort (keys(%$source_file_key_to_text_mapping));
|
---|
| 1342 | foreach my $chunk_key (@sorted_chunk_keys) {
|
---|
[24627] | 1343 | my $source_file_chunk_date = $source_file_key_to_last_update_date_mapping->{$chunk_key} || "";
|
---|
| 1344 | my $source_file_chunk_text = &make_text_xml_safe($source_file_key_to_text_mapping->{$chunk_key});
|
---|
| 1345 |
|
---|
| 1346 | $xml_response .= " <Chunk key=\"" . &make_text_xml_safe($chunk_key) . "\">\n";
|
---|
| 1347 | $xml_response .= " <SourceFileText date=\"$source_file_chunk_date\">$source_file_chunk_text</SourceFileText>\n";
|
---|
| 1348 | if (defined($target_file_key_to_text_mapping->{$chunk_key})) {
|
---|
| 1349 | my $target_file_chunk_date = $target_file_key_to_last_update_date_mapping->{$chunk_key} || "";
|
---|
| 1350 | my $target_file_chunk_text = &make_text_xml_safe($target_file_key_to_text_mapping->{$chunk_key});
|
---|
| 1351 | $xml_response .= " <TargetFileText date=\"$target_file_chunk_date\">$target_file_chunk_text</TargetFileText>\n";
|
---|
| 1352 | }
|
---|
| 1353 | else {
|
---|
| 1354 | $xml_response .= " <TargetFileText></TargetFileText>\n";
|
---|
| 1355 | }
|
---|
| 1356 |
|
---|
| 1357 | $xml_response .= " </Chunk>\n";
|
---|
[18460] | 1358 | }
|
---|
| 1359 | $xml_response .= " </Chunks>\n";
|
---|
| 1360 |
|
---|
| 1361 | $xml_response .= "</GTIResponse>\n";
|
---|
| 1362 | return $xml_response;
|
---|
| 1363 | }
|
---|
| 1364 |
|
---|
| 1365 |
|
---|
| 1366 |
|
---|
[10019] | 1367 | # ==========================================================================================
|
---|
| 1368 | # MACROFILE FUNCTIONS
|
---|
| 1369 |
|
---|
| 1370 | sub build_key_to_line_mapping_for_macrofile
|
---|
| 1371 | {
|
---|
| 1372 | my (@file_lines) = @_;
|
---|
[24627] | 1373 |
|
---|
[10019] | 1374 | my $macro_package;
|
---|
| 1375 | my %chunk_key_to_line_mapping = ();
|
---|
| 1376 | # Process the contents of the file, line by line
|
---|
| 1377 | for (my $i = 0; $i < scalar(@file_lines); $i++) {
|
---|
[24627] | 1378 | my $line = $file_lines[$i];
|
---|
| 1379 | $line =~ s/(\s*)$//; # Remove any nasty whitespace, carriage returns etc.
|
---|
| 1380 |
|
---|
| 1381 | # Check if a new package is being defined
|
---|
| 1382 | if ($line =~ m/^package\s+(.+)/) {
|
---|
| 1383 | $macro_package = $1;
|
---|
[10041] | 1384 | }
|
---|
[24627] | 1385 |
|
---|
| 1386 | # Line contains a macro name
|
---|
| 1387 | elsif ($line =~ m/^(_\w+_)/) {
|
---|
| 1388 | my $macro_key = $1;
|
---|
| 1389 | $line =~ s/\s*([^\\]\#[^\}]+)?$//; # Remove any comments and nasty whitespace
|
---|
| 1390 |
|
---|
| 1391 | # While there is still text of the macro to go...
|
---|
| 1392 | my $startline = $i;
|
---|
| 1393 | while ($line !~ /\}$/) {
|
---|
| 1394 | $i++;
|
---|
| 1395 | if ($i == scalar(@file_lines)) {
|
---|
| 1396 | &throw_fatal_error("Could not find end of macro $macro_key.");
|
---|
| 1397 | }
|
---|
| 1398 | $line = $file_lines[$i];
|
---|
| 1399 | $line =~ s/\s*([^\\]\#[^\}]+)?$//; # Remove any comments and nasty whitespace
|
---|
| 1400 | }
|
---|
| 1401 |
|
---|
[10019] | 1402 | # The chunk key consists of the package name and the macro key
|
---|
| 1403 | my $chunk_key = $macro_package . "." . $macro_key;
|
---|
| 1404 | # Map from chunk key to line
|
---|
| 1405 | $chunk_key_to_line_mapping{$chunk_key} = $startline . "-" . $i;
|
---|
| 1406 | }
|
---|
[24627] | 1407 |
|
---|
[10019] | 1408 | # Icon: line in format ## "image text" ## image_type ## macro_name ##
|
---|
| 1409 | elsif ($line =~ m/^\#\# .* \#\# .* \#\# (.*) \#\#/) {
|
---|
[24627] | 1410 | # The chunk key consists of package name and macro key
|
---|
| 1411 | my $chunk_key = $macro_package . "." . $1;
|
---|
| 1412 | # Map from chunk key to line
|
---|
| 1413 | $chunk_key_to_line_mapping{$chunk_key} = $i . "-" . $i;
|
---|
| 1414 | }
|
---|
| 1415 | }
|
---|
[10019] | 1416 |
|
---|
[24627] | 1417 | return %chunk_key_to_line_mapping;
|
---|
[10019] | 1418 | }
|
---|
| 1419 |
|
---|
| 1420 |
|
---|
| 1421 | sub import_chunk_from_macrofile
|
---|
| 1422 | {
|
---|
| 1423 | my ($chunk_text) = @_;
|
---|
[24627] | 1424 |
|
---|
[10019] | 1425 | # Is this an icon macro??
|
---|
| 1426 | if ($chunk_text =~ /^\#\# (.*)/) {
|
---|
[24627] | 1427 | # Extract image macro text
|
---|
| 1428 | $chunk_text =~ /^\#\#\s+([^\#]+)\s+\#\#/;
|
---|
| 1429 | $chunk_text = $1;
|
---|
| 1430 |
|
---|
[10019] | 1431 | # Remove enclosing quotes
|
---|
| 1432 | $chunk_text =~ s/^\"//;
|
---|
| 1433 | $chunk_text =~ s/\"$//;
|
---|
[30562] | 1434 | }
|
---|
[10019] | 1435 |
|
---|
[30562] | 1436 | # No, so it must be a text macro
|
---|
| 1437 | else {
|
---|
[10019] | 1438 | # Remove macro key
|
---|
| 1439 | $chunk_text =~ s/^_([^_]+)_(\s*)//;
|
---|
[24627] | 1440 |
|
---|
[10019] | 1441 | # Remove language specifier
|
---|
[30562] | 1442 | $chunk_text =~ s/^\[l=[^\]]*\](\s*)//; # only remove until first closing square bracket, ]
|
---|
[24627] | 1443 |
|
---|
[10019] | 1444 | # Remove braces enclosing text
|
---|
| 1445 | $chunk_text =~ s/^{(\s*)((.|\n)*)}(\s*)(\#.+\s*)?/$2/;
|
---|
[30562] | 1446 | }
|
---|
[10019] | 1447 |
|
---|
[30562] | 1448 | return $chunk_text;
|
---|
[10019] | 1449 | }
|
---|
| 1450 |
|
---|
| 1451 |
|
---|
[12483] | 1452 | sub get_macrofile_chunk_gti_comment
|
---|
[10019] | 1453 | {
|
---|
| 1454 | my ($chunk_text) = @_;
|
---|
[24627] | 1455 |
|
---|
[10019] | 1456 | # Check for an "Updated DD-MMM-YYYY" comment at the end of the chunk
|
---|
[12484] | 1457 | if ($chunk_text =~ /\#\s+(Updated\s+\d?\d-\D\D\D-\d\d\d\d.*)\s*$/i) {
|
---|
[24627] | 1458 | return $1;
|
---|
| 1459 | }
|
---|
[10019] | 1460 |
|
---|
[24627] | 1461 | return undef;
|
---|
[10019] | 1462 | }
|
---|
| 1463 |
|
---|
| 1464 |
|
---|
| 1465 | sub is_macrofile_chunk_automatically_translated
|
---|
| 1466 | {
|
---|
| 1467 | my ($chunk_key) = @_;
|
---|
[24627] | 1468 |
|
---|
[10019] | 1469 | # The _httpiconX_, _widthX_ and _heightX_ image macros are automatically translated
|
---|
| 1470 | if ($chunk_key =~ /\._(httpicon|width|height)/) {
|
---|
[24627] | 1471 | return 1;
|
---|
[10019] | 1472 | }
|
---|
[24627] | 1473 |
|
---|
[10019] | 1474 | return 0;
|
---|
| 1475 | }
|
---|
| 1476 |
|
---|
| 1477 |
|
---|
| 1478 | # Use the source file to generate a target file that is formatted the same
|
---|
| 1479 | sub write_translated_macrofile
|
---|
| 1480 | {
|
---|
| 1481 | my $source_file = shift(@_); # Not used
|
---|
[11321] | 1482 | my @source_file_lines = @{shift(@_)};
|
---|
[10019] | 1483 | my $source_file_key_to_text_mapping = shift(@_);
|
---|
| 1484 | my $target_file = shift(@_);
|
---|
[11321] | 1485 | my @target_file_lines = @{shift(@_)};
|
---|
[10019] | 1486 | my $target_file_key_to_text_mapping = shift(@_);
|
---|
[12483] | 1487 | my $target_file_key_to_gti_comment_mapping = shift(@_);
|
---|
[10019] | 1488 | my $target_language_code = shift(@_);
|
---|
[24627] | 1489 |
|
---|
[10126] | 1490 | # Build a mapping from source file line to chunk key
|
---|
[11321] | 1491 | my %source_file_key_to_line_mapping = &build_key_to_line_mapping_for_macrofile(@source_file_lines);
|
---|
[10019] | 1492 | my %source_file_line_to_key_mapping = ();
|
---|
| 1493 | foreach my $chunk_key (keys(%source_file_key_to_line_mapping)) {
|
---|
[24627] | 1494 | $source_file_line_to_key_mapping{$source_file_key_to_line_mapping{$chunk_key}} = $chunk_key;
|
---|
[10019] | 1495 | }
|
---|
[10126] | 1496 | my @source_file_line_keys = (sort sort_by_line (keys(%source_file_line_to_key_mapping)));
|
---|
| 1497 | my $source_file_line_number = 0;
|
---|
[24627] | 1498 |
|
---|
[10126] | 1499 | # Build a mapping from target file line to chunk key
|
---|
[11321] | 1500 | my %target_file_key_to_line_mapping = &build_key_to_line_mapping_for_macrofile(@target_file_lines);
|
---|
[10126] | 1501 | my %target_file_line_to_key_mapping = ();
|
---|
| 1502 | foreach my $chunk_key (keys(%target_file_key_to_line_mapping)) {
|
---|
[24627] | 1503 | $target_file_line_to_key_mapping{$target_file_key_to_line_mapping{$chunk_key}} = $chunk_key;
|
---|
[10126] | 1504 | }
|
---|
| 1505 | my @target_file_line_keys = (sort sort_by_line (keys(%target_file_line_to_key_mapping)));
|
---|
[24627] | 1506 |
|
---|
[10019] | 1507 | # Write the new target file
|
---|
| 1508 | my $target_file_path = &util::filename_cat($gsdl_root_directory, $target_file);
|
---|
| 1509 | if (!open(TARGET_FILE, ">$target_file_path")) {
|
---|
[24627] | 1510 | &throw_fatal_error("Could not write target file $target_file_path.");
|
---|
[10019] | 1511 | }
|
---|
[24627] | 1512 |
|
---|
[10126] | 1513 | # Use the header from the target file, to keep language and author information
|
---|
| 1514 | if (scalar(@target_file_line_keys) > 0) {
|
---|
[24627] | 1515 | my $target_file_line_number = 0;
|
---|
| 1516 | my $target_file_chunk_starting_line_number = (split(/-/, $target_file_line_keys[0]))[0];
|
---|
| 1517 | while ($target_file_line_number < $target_file_chunk_starting_line_number) {
|
---|
| 1518 | my $target_file_line = $target_file_lines[$target_file_line_number];
|
---|
| 1519 | last if ($target_file_line =~ /^\# -- Missing translation: /); # We don't want to get into the macros
|
---|
| 1520 | print TARGET_FILE $target_file_line;
|
---|
| 1521 | $target_file_line_number++;
|
---|
| 1522 | }
|
---|
| 1523 |
|
---|
| 1524 | $source_file_line_number = (split(/-/, $source_file_line_keys[0]))[0];
|
---|
[10126] | 1525 | }
|
---|
[24627] | 1526 |
|
---|
[10019] | 1527 | # Model the new target file on the source file, with the target file translations
|
---|
[10126] | 1528 | foreach my $line_key (@source_file_line_keys) {
|
---|
[24627] | 1529 | # Fill in the gaps before this chunk starts
|
---|
| 1530 | my $source_file_chunk_starting_line_number = (split(/-/, $line_key))[0];
|
---|
| 1531 | my $source_file_chunk_finishing_line_number = (split(/-/, $line_key))[1];
|
---|
| 1532 | while ($source_file_line_number < $source_file_chunk_starting_line_number) {
|
---|
| 1533 | print TARGET_FILE $source_file_lines[$source_file_line_number];
|
---|
| 1534 | $source_file_line_number++;
|
---|
| 1535 | }
|
---|
| 1536 | $source_file_line_number = $source_file_chunk_finishing_line_number + 1;
|
---|
| 1537 |
|
---|
| 1538 | my $chunk_key = $source_file_line_to_key_mapping{$line_key};
|
---|
| 1539 | my $source_file_chunk_text = $source_file_key_to_text_mapping->{$chunk_key};
|
---|
| 1540 | my $target_file_chunk_text = $target_file_key_to_text_mapping->{$chunk_key} || "";
|
---|
| 1541 |
|
---|
| 1542 | my $macrofile_key = $chunk_key;
|
---|
| 1543 | $macrofile_key =~ s/^(.+?)\.//;
|
---|
| 1544 |
|
---|
| 1545 | # If no translation exists for this chunk, show this, and move on
|
---|
| 1546 | if ($source_file_chunk_text ne "" && $target_file_chunk_text eq "") {
|
---|
| 1547 | print TARGET_FILE "# -- Missing translation: $macrofile_key\n";
|
---|
| 1548 | next;
|
---|
| 1549 | }
|
---|
| 1550 |
|
---|
| 1551 | # Grab the source chunk text
|
---|
| 1552 | my $source_file_chunk = $source_file_lines[$source_file_chunk_starting_line_number];
|
---|
| 1553 | for (my $l = ($source_file_chunk_starting_line_number + 1); $l <= $source_file_chunk_finishing_line_number; $l++) {
|
---|
| 1554 | $source_file_chunk .= $source_file_lines[$l];
|
---|
| 1555 | }
|
---|
| 1556 |
|
---|
| 1557 | # Is this an icon macro??
|
---|
| 1558 | if ($source_file_chunk =~ /^\#\# (.*)/) {
|
---|
| 1559 | # Escape any newline and question mark characters so the source text is replaced correctly
|
---|
| 1560 | $source_file_chunk_text =~ s/\\/\\\\/g;
|
---|
[10019] | 1561 | $source_file_chunk_text =~ s/\?/\\\?/g;
|
---|
[24627] | 1562 |
|
---|
[10019] | 1563 | # Build the new target chunk from the source chunk
|
---|
| 1564 | my $target_file_chunk = $source_file_chunk;
|
---|
| 1565 | $target_file_chunk =~ s/$source_file_chunk_text/$target_file_chunk_text/;
|
---|
[10050] | 1566 | $target_file_chunk =~ s/(\s)*$//;
|
---|
[10019] | 1567 | print TARGET_FILE "$target_file_chunk";
|
---|
| 1568 | }
|
---|
[24627] | 1569 |
|
---|
[10019] | 1570 | # No, it is just a normal text macro
|
---|
| 1571 | else {
|
---|
| 1572 | print TARGET_FILE "$macrofile_key [l=$target_language_code] {$target_file_chunk_text}";
|
---|
| 1573 | }
|
---|
[24627] | 1574 |
|
---|
[12483] | 1575 | # Add the "updated" comment, if one exists
|
---|
| 1576 | if ($target_file_key_to_gti_comment_mapping->{$chunk_key}) {
|
---|
| 1577 | print TARGET_FILE " # " . $target_file_key_to_gti_comment_mapping->{$chunk_key};
|
---|
[10050] | 1578 | }
|
---|
| 1579 | print TARGET_FILE "\n";
|
---|
[24627] | 1580 | }
|
---|
[10019] | 1581 |
|
---|
[24627] | 1582 | close(TARGET_FILE);
|
---|
[10019] | 1583 | }
|
---|
| 1584 |
|
---|
| 1585 |
|
---|
| 1586 | sub sort_by_line
|
---|
| 1587 | {
|
---|
| 1588 | return ((split(/-/, $a))[0] <=> (split(/-/, $b))[0]);
|
---|
| 1589 | }
|
---|
| 1590 |
|
---|
| 1591 |
|
---|
| 1592 | # ==========================================================================================
|
---|
| 1593 | # RESOURCE BUNDLE FUNCTIONS
|
---|
| 1594 |
|
---|
[29456] | 1595 | # need to handle multi-line properties. A multiline ends on \ if it continues over the next line
|
---|
[10019] | 1596 | sub build_key_to_line_mapping_for_resource_bundle
|
---|
| 1597 | {
|
---|
| 1598 | my (@file_lines) = @_;
|
---|
[24627] | 1599 |
|
---|
[11321] | 1600 | my %chunk_key_to_line_mapping = ();
|
---|
[29456] | 1601 |
|
---|
| 1602 | my $chunk_key;
|
---|
| 1603 | my $startindex = -1;
|
---|
| 1604 |
|
---|
[10019] | 1605 | for (my $i = 0; $i < scalar(@file_lines); $i++) {
|
---|
[24627] | 1606 | my $line = $file_lines[$i];
|
---|
| 1607 | $line =~ s/(\s*)$//; # Remove any nasty whitespace, carriage returns etc.
|
---|
| 1608 |
|
---|
| 1609 | # Line contains a dictionary string
|
---|
| 1610 | if ($line =~ /^(\S+?)[:|=](.*)$/) {
|
---|
[29456] | 1611 | $chunk_key = $1;
|
---|
| 1612 | $startindex = $i;
|
---|
| 1613 | }
|
---|
| 1614 | if ($startindex != -1) {
|
---|
| 1615 | if($line !~ m/\\$/) { # line finished
|
---|
| 1616 | # $i keeps track of the line at which this property (chunk_key) finishes
|
---|
| 1617 |
|
---|
[24627] | 1618 | # Map from chunk key to line
|
---|
[29456] | 1619 | $chunk_key_to_line_mapping{$chunk_key} = $startindex . "-" . $i;
|
---|
| 1620 | $startindex = -1;
|
---|
| 1621 | $chunk_key = "";
|
---|
| 1622 | }
|
---|
| 1623 | }
|
---|
[10019] | 1624 | }
|
---|
[24627] | 1625 |
|
---|
[11321] | 1626 | return %chunk_key_to_line_mapping;
|
---|
[10019] | 1627 | }
|
---|
| 1628 |
|
---|
| 1629 |
|
---|
| 1630 | sub import_chunk_from_resource_bundle
|
---|
| 1631 | {
|
---|
| 1632 | my ($chunk_text) = @_;
|
---|
[24627] | 1633 |
|
---|
[10019] | 1634 | # Simple: just remove string key
|
---|
[29456] | 1635 | $chunk_text =~ s/^(\S+?)[:|=](\s*)//s;
|
---|
| 1636 | $chunk_text =~ s/(\s*)$//s; # Remove any nasty whitespace, carriage returns etc.
|
---|
| 1637 | $chunk_text =~ s/(\s*)\#\s+Updated\s+(\d?\d-\D\D\D-\d\d\d\d.*)\s*$//is;
|
---|
[24627] | 1638 |
|
---|
[10019] | 1639 | return $chunk_text;
|
---|
| 1640 | }
|
---|
| 1641 |
|
---|
| 1642 |
|
---|
[12483] | 1643 | sub get_resource_bundle_chunk_gti_comment
|
---|
[10019] | 1644 | {
|
---|
| 1645 | my ($chunk_text) = @_;
|
---|
[24627] | 1646 |
|
---|
[10019] | 1647 | # Check for an "Updated DD-MMM-YYYY" comment at the end of the chunk
|
---|
[12484] | 1648 | if ($chunk_text =~ /\#\s+(Updated\s+\d?\d-\D\D\D-\d\d\d\d.*)\s*$/i) {
|
---|
[24627] | 1649 | return $1;
|
---|
| 1650 | }
|
---|
[10019] | 1651 |
|
---|
[24627] | 1652 | return undef;
|
---|
[10019] | 1653 | }
|
---|
| 1654 |
|
---|
| 1655 |
|
---|
| 1656 | sub is_resource_bundle_chunk_automatically_translated
|
---|
| 1657 | {
|
---|
| 1658 | # No resource bundle chunks are automatically translated
|
---|
| 1659 | return 0;
|
---|
| 1660 | }
|
---|
| 1661 |
|
---|
| 1662 |
|
---|
| 1663 | sub write_translated_resource_bundle
|
---|
| 1664 | {
|
---|
| 1665 | my $source_file = shift(@_); # Not used
|
---|
[11321] | 1666 | my @source_file_lines = @{shift(@_)};
|
---|
[10019] | 1667 | my $source_file_key_to_text_mapping = shift(@_);
|
---|
| 1668 | my $target_file = shift(@_);
|
---|
[11321] | 1669 | my @target_file_lines = @{shift(@_)}; # Not used
|
---|
[10019] | 1670 | my $target_file_key_to_text_mapping = shift(@_);
|
---|
[12483] | 1671 | my $target_file_key_to_gti_comment_mapping = shift(@_);
|
---|
[10019] | 1672 | my $target_language_code = shift(@_); # Not used
|
---|
[24627] | 1673 |
|
---|
[10019] | 1674 | # Build a mapping from chunk key to source file line, and from source file line to chunk key
|
---|
[11321] | 1675 | my %source_file_key_to_line_mapping = &build_key_to_line_mapping_for_resource_bundle(@source_file_lines);
|
---|
[10019] | 1676 | my %source_file_line_to_key_mapping = ();
|
---|
| 1677 | foreach my $chunk_key (keys(%source_file_key_to_line_mapping)) {
|
---|
[24627] | 1678 | $source_file_line_to_key_mapping{$source_file_key_to_line_mapping{$chunk_key}} = $chunk_key;
|
---|
[10019] | 1679 | }
|
---|
[24627] | 1680 |
|
---|
[10019] | 1681 | # Write the new target file
|
---|
| 1682 | my $target_file_path = &util::filename_cat($gsdl_root_directory, $target_file);
|
---|
| 1683 | if (!open(TARGET_FILE, ">$target_file_path")) {
|
---|
[24627] | 1684 | &throw_fatal_error("Could not write target file $target_file_path.");
|
---|
[10019] | 1685 | }
|
---|
[24627] | 1686 |
|
---|
[10019] | 1687 | # Model the new target file on the source file, with the target file translations
|
---|
[10091] | 1688 | my $source_file_line_number = 0;
|
---|
[10019] | 1689 | foreach my $line_key (sort sort_by_line (keys(%source_file_line_to_key_mapping))) {
|
---|
[24627] | 1690 | # Fill in the gaps before this chunk starts
|
---|
| 1691 | my $source_file_chunk_starting_line_number = (split(/-/, $line_key))[0];
|
---|
| 1692 | my $source_file_chunk_finishing_line_number = (split(/-/, $line_key))[1];
|
---|
| 1693 | while ($source_file_line_number < $source_file_chunk_starting_line_number) {
|
---|
| 1694 | print TARGET_FILE $source_file_lines[$source_file_line_number];
|
---|
| 1695 | $source_file_line_number++;
|
---|
| 1696 | }
|
---|
| 1697 | $source_file_line_number = $source_file_chunk_finishing_line_number + 1;
|
---|
| 1698 |
|
---|
| 1699 | my $chunk_key = $source_file_line_to_key_mapping{$line_key};
|
---|
| 1700 | my $source_file_chunk_text = $source_file_key_to_text_mapping->{$chunk_key};
|
---|
| 1701 | my $target_file_chunk_text = $target_file_key_to_text_mapping->{$chunk_key} || "";
|
---|
| 1702 |
|
---|
| 1703 | # If no translation exists for this chunk, show this, and move on
|
---|
| 1704 | if ($source_file_chunk_text ne "" && $target_file_chunk_text eq "") {
|
---|
| 1705 | print TARGET_FILE "# -- Missing translation: $chunk_key\n";
|
---|
| 1706 | next;
|
---|
| 1707 | }
|
---|
| 1708 |
|
---|
| 1709 | print TARGET_FILE "$chunk_key:$target_file_chunk_text";
|
---|
| 1710 | if ($target_file_key_to_gti_comment_mapping->{$chunk_key}) {
|
---|
| 1711 | print TARGET_FILE " # " . $target_file_key_to_gti_comment_mapping->{$chunk_key};
|
---|
| 1712 | }
|
---|
| 1713 | print TARGET_FILE "\n";
|
---|
[10019] | 1714 | }
|
---|
[24627] | 1715 |
|
---|
[10019] | 1716 | close(TARGET_FILE);
|
---|
| 1717 | }
|
---|
| 1718 |
|
---|
| 1719 |
|
---|
[11321] | 1720 | # ==========================================================================================
|
---|
| 1721 | # GREENSTONE XML FUNCTIONS
|
---|
| 1722 |
|
---|
| 1723 | sub build_key_to_line_mapping_for_greenstone_xml
|
---|
| 1724 | {
|
---|
| 1725 | my (@file_lines) = @_;
|
---|
[24627] | 1726 |
|
---|
[11321] | 1727 | my %chunk_key_to_line_mapping = ();
|
---|
| 1728 | for (my $i = 0; $i < scalar(@file_lines); $i++) {
|
---|
[24627] | 1729 | my $line = $file_lines[$i];
|
---|
| 1730 | $line =~ s/(\s*)$//; # Remove any nasty whitespace, carriage returns etc.
|
---|
| 1731 |
|
---|
| 1732 | # Line contains a string to translate
|
---|
| 1733 | if ($line =~ /^\s*<Text id=\"(.*?)\">/) {
|
---|
| 1734 | my $chunk_key = $1;
|
---|
| 1735 | $line =~ s/\s*$//; # Remove any nasty whitespace
|
---|
| 1736 | $line =~ s/<Updated date=\"\d?\d-\D\D\D-\d\d\d\d.*\"\/>$//;
|
---|
| 1737 |
|
---|
| 1738 | # While there is still text of the string to go...
|
---|
| 1739 | my $startline = $i;
|
---|
| 1740 | while ($line !~ /<\/Text>$/) {
|
---|
| 1741 | $i++;
|
---|
| 1742 | if ($i == scalar(@file_lines)) {
|
---|
| 1743 | &throw_fatal_error("Could not find end of string $chunk_key.");
|
---|
| 1744 | }
|
---|
| 1745 | $line = $file_lines[$i];
|
---|
| 1746 | $line =~ s/\s*$//; # Remove any nasty whitespace
|
---|
| 1747 | $line =~ s/<Updated date=\"\d?\d-\D\D\D-\d\d\d\d.*\"\/>$//;
|
---|
| 1748 | }
|
---|
| 1749 |
|
---|
| 1750 | # Map from chunk key to line
|
---|
| 1751 | if (!defined($chunk_key_to_line_mapping{$chunk_key})) {
|
---|
| 1752 | $chunk_key_to_line_mapping{$chunk_key} = $startline . "-" . $i;
|
---|
| 1753 | }
|
---|
| 1754 | else {
|
---|
| 1755 | &throw_fatal_error("Duplicate key $chunk_key.");
|
---|
| 1756 | }
|
---|
[11321] | 1757 | }
|
---|
| 1758 | }
|
---|
[24627] | 1759 |
|
---|
[11321] | 1760 | return %chunk_key_to_line_mapping;
|
---|
| 1761 | }
|
---|
| 1762 |
|
---|
| 1763 |
|
---|
| 1764 | sub import_chunk_from_greenstone_xml
|
---|
| 1765 | {
|
---|
| 1766 | my ($chunk_text) = @_;
|
---|
[24627] | 1767 |
|
---|
[11321] | 1768 | # Simple: just remove the Text tags
|
---|
[11447] | 1769 | $chunk_text =~ s/^\s*<Text id=\"(.*?)\">(\s*)//;
|
---|
[12484] | 1770 | $chunk_text =~ s/<Updated date=\"\d?\d-\D\D\D-\d\d\d\d.*\"\/>$//;
|
---|
[11321] | 1771 | $chunk_text =~ s/<\/Text>$//;
|
---|
[24627] | 1772 |
|
---|
[11321] | 1773 | return $chunk_text;
|
---|
| 1774 | }
|
---|
| 1775 |
|
---|
| 1776 |
|
---|
[12483] | 1777 | sub get_greenstone_xml_chunk_gti_comment
|
---|
[11321] | 1778 | {
|
---|
| 1779 | my ($chunk_text) = @_;
|
---|
[24627] | 1780 |
|
---|
[11321] | 1781 | # Check for an "Updated DD-MMM-YYYY" comment at the end of the chunk
|
---|
[12484] | 1782 | if ($chunk_text =~ /<Updated date=\"(\d?\d-\D\D\D-\d\d\d\d.*)\"\/>$/i) {
|
---|
[24627] | 1783 | return $1;
|
---|
[11487] | 1784 | }
|
---|
[24627] | 1785 |
|
---|
[11321] | 1786 | return undef;
|
---|
| 1787 | }
|
---|
| 1788 |
|
---|
| 1789 |
|
---|
| 1790 | sub is_greenstone_xml_chunk_automatically_translated
|
---|
| 1791 | {
|
---|
| 1792 | # No greenstone XML chunks are automatically translated
|
---|
| 1793 | return 0;
|
---|
| 1794 | }
|
---|
| 1795 |
|
---|
| 1796 |
|
---|
| 1797 | sub write_translated_greenstone_xml
|
---|
| 1798 | {
|
---|
| 1799 | my $source_file = shift(@_); # Not used
|
---|
| 1800 | my @source_file_lines = @{shift(@_)};
|
---|
| 1801 | my $source_file_key_to_text_mapping = shift(@_);
|
---|
| 1802 | my $target_file = shift(@_);
|
---|
| 1803 | my @target_file_lines = @{shift(@_)}; # Not used
|
---|
| 1804 | my $target_file_key_to_text_mapping = shift(@_);
|
---|
[12483] | 1805 | my $target_file_key_to_gti_comment_mapping = shift(@_);
|
---|
[11321] | 1806 | my $target_language_code = shift(@_); # Not used
|
---|
[24627] | 1807 |
|
---|
[11321] | 1808 | # Build a mapping from chunk key to source file line, and from source file line to chunk key
|
---|
| 1809 | my %source_file_key_to_line_mapping = &build_key_to_line_mapping_for_greenstone_xml(@source_file_lines);
|
---|
| 1810 | my %source_file_line_to_key_mapping = ();
|
---|
| 1811 | foreach my $chunk_key (keys(%source_file_key_to_line_mapping)) {
|
---|
[24627] | 1812 | $source_file_line_to_key_mapping{$source_file_key_to_line_mapping{$chunk_key}} = $chunk_key;
|
---|
[11321] | 1813 | }
|
---|
[24627] | 1814 |
|
---|
[11321] | 1815 | # Write the new target file
|
---|
| 1816 | my $target_file_path = &util::filename_cat($gsdl_root_directory, $target_file);
|
---|
| 1817 | if (!open(TARGET_FILE, ">$target_file_path")) {
|
---|
[24627] | 1818 | &throw_fatal_error("Could not write target file $target_file_path.");
|
---|
[11321] | 1819 | }
|
---|
[24627] | 1820 |
|
---|
[11321] | 1821 | # Model the new target file on the source file, with the target file translations
|
---|
| 1822 | my $source_file_line_number = 0;
|
---|
| 1823 | foreach my $line_key (sort sort_by_line (keys(%source_file_line_to_key_mapping))) {
|
---|
[24627] | 1824 | # Fill in the gaps before this chunk starts
|
---|
| 1825 | my $source_file_chunk_starting_line_number = (split(/-/, $line_key))[0];
|
---|
| 1826 | my $source_file_chunk_finishing_line_number = (split(/-/, $line_key))[1];
|
---|
| 1827 | while ($source_file_line_number < $source_file_chunk_starting_line_number) {
|
---|
| 1828 | print TARGET_FILE $source_file_lines[$source_file_line_number];
|
---|
| 1829 | $source_file_line_number++;
|
---|
| 1830 | }
|
---|
| 1831 | $source_file_line_number = $source_file_chunk_finishing_line_number + 1;
|
---|
| 1832 |
|
---|
| 1833 | my $chunk_key = $source_file_line_to_key_mapping{$line_key};
|
---|
| 1834 | my $source_file_chunk_text = $source_file_key_to_text_mapping->{$chunk_key};
|
---|
| 1835 | my $target_file_chunk_text = $target_file_key_to_text_mapping->{$chunk_key} || "";
|
---|
| 1836 | $target_file_chunk_text =~ s/(\n)*$//g;
|
---|
| 1837 |
|
---|
| 1838 | # If no translation exists for this chunk, show this, and move on
|
---|
| 1839 | if ($source_file_chunk_text ne "" && $target_file_chunk_text eq "") {
|
---|
| 1840 | print TARGET_FILE "<!-- Missing translation: $chunk_key -->\n";
|
---|
| 1841 | next;
|
---|
| 1842 | }
|
---|
| 1843 |
|
---|
| 1844 | print TARGET_FILE "<Text id=\"$chunk_key\">$target_file_chunk_text</Text>";
|
---|
| 1845 | if ($target_file_key_to_gti_comment_mapping->{$chunk_key}) {
|
---|
| 1846 | my $chunk_gti_comment = $target_file_key_to_gti_comment_mapping->{$chunk_key};
|
---|
| 1847 | $chunk_gti_comment =~ s/^Updated //;
|
---|
| 1848 | print TARGET_FILE "<Updated date=\"" . $chunk_gti_comment . "\"\/>";
|
---|
| 1849 | }
|
---|
| 1850 | print TARGET_FILE "\n";
|
---|
[11321] | 1851 | }
|
---|
[24627] | 1852 |
|
---|
[11321] | 1853 | # Fill in the end of the file
|
---|
| 1854 | while ($source_file_line_number < scalar(@source_file_lines)) {
|
---|
[24627] | 1855 | print TARGET_FILE $source_file_lines[$source_file_line_number];
|
---|
| 1856 | $source_file_line_number++;
|
---|
[11321] | 1857 | }
|
---|
[24627] | 1858 |
|
---|
[11321] | 1859 | close(TARGET_FILE);
|
---|
| 1860 | }
|
---|
| 1861 |
|
---|
| 1862 |
|
---|
[18460] | 1863 | # ==========================================================================================
|
---|
| 1864 | # GREENSTONE3 FUNCTIONS
|
---|
| 1865 |
|
---|
| 1866 | sub get_all_chunks_for_gs3
|
---|
| 1867 | {
|
---|
| 1868 | # The code of the target language (ensure it is lowercase)
|
---|
| 1869 | my $target_language_code = lc(shift(@_));
|
---|
| 1870 | my $translation_file_key = lc(shift(@_));
|
---|
[24627] | 1871 |
|
---|
[18460] | 1872 | # Check that the necessary arguments were supplied
|
---|
| 1873 | if (!$target_language_code) {
|
---|
[24627] | 1874 | &throw_fatal_error("Missing command argument.");
|
---|
[18460] | 1875 | }
|
---|
[24627] | 1876 |
|
---|
[18460] | 1877 | # Get (and check) the translation configuration
|
---|
| 1878 | # my ($source_file_dir, $target_file, $translation_file_type) = &get_translation_configuration($target_language_code, $translation_file_key);
|
---|
| 1879 |
|
---|
| 1880 | my %source_files_key_to_text_mapping = ();
|
---|
| 1881 | my %target_files_key_to_text_mapping = ();
|
---|
| 1882 | my %source_files_key_to_last_update_date_mapping = ();
|
---|
| 1883 | my %target_files_key_to_last_update_date_mapping = ();
|
---|
[24627] | 1884 |
|
---|
[30581] | 1885 | &build_gs3_configuration($translation_file_key, $target_language_code, \%source_files_key_to_text_mapping, \%target_files_key_to_text_mapping, \%source_files_key_to_last_update_date_mapping, \%target_files_key_to_last_update_date_mapping);
|
---|
[24627] | 1886 |
|
---|
[18460] | 1887 | &log_message("Total number of source chunks: " . scalar(keys(%source_files_key_to_text_mapping)));
|
---|
| 1888 | &log_message("Total number of target chunks: " . scalar(keys(%target_files_key_to_text_mapping)));
|
---|
[24627] | 1889 |
|
---|
[18460] | 1890 | my $xml_response = &create_xml_response_for_all_chunks($translation_file_key, "", \%source_files_key_to_text_mapping, \%target_files_key_to_text_mapping, \%source_files_key_to_last_update_date_mapping, \%target_files_key_to_last_update_date_mapping);
|
---|
| 1891 | return $xml_response;
|
---|
| 1892 | }
|
---|
| 1893 |
|
---|
| 1894 |
|
---|
| 1895 | sub get_first_n_chunks_requiring_work_for_gs3
|
---|
| 1896 | {
|
---|
| 1897 | # The code of the target language (ensure it is lowercase)
|
---|
| 1898 | my $target_language_code = lc(shift(@_));
|
---|
| 1899 | # The key of the file to translate (ensure it is lowercase)
|
---|
| 1900 | my $translation_file_key = lc(shift(@_));
|
---|
| 1901 | # The number of chunks to return (defaults to one if not specified)
|
---|
| 1902 | my $num_chunks_to_return = shift(@_) || "1";
|
---|
| 1903 |
|
---|
| 1904 | # Check that the necessary arguments were supplied
|
---|
| 1905 | if (!$target_language_code || !$translation_file_key) {
|
---|
[24627] | 1906 | &throw_fatal_error("Missing command argument.");
|
---|
[18460] | 1907 | }
|
---|
[28755] | 1908 |
|
---|
[18460] | 1909 | my %source_files_key_to_text_mapping = ();
|
---|
| 1910 | my %target_files_key_to_text_mapping = ();
|
---|
| 1911 | my %source_files_key_to_last_update_date_mapping = ();
|
---|
| 1912 | my %target_files_key_to_last_update_date_mapping = ();
|
---|
[24627] | 1913 |
|
---|
[30581] | 1914 | &build_gs3_configuration($translation_file_key, $target_language_code, \%source_files_key_to_text_mapping, \%target_files_key_to_text_mapping,
|
---|
[24627] | 1915 | \%source_files_key_to_last_update_date_mapping, \%target_files_key_to_last_update_date_mapping);
|
---|
[18460] | 1916 |
|
---|
| 1917 | # Determine the target file chunks requiring translation
|
---|
| 1918 | my @target_files_keys_requiring_translation = &determine_chunks_requiring_translation(\%source_files_key_to_text_mapping, \%target_files_key_to_text_mapping);
|
---|
| 1919 | # Determine the target file chunks requiring updating
|
---|
| 1920 | my @target_files_keys_requiring_updating = &determine_chunks_requiring_updating(\%source_files_key_to_last_update_date_mapping, \%target_files_key_to_last_update_date_mapping);
|
---|
| 1921 | &log_message("Total number of target chunks requiring translation: " . scalar(@target_files_keys_requiring_translation));
|
---|
| 1922 | &log_message("Total number of target chunks requiring updating: " . scalar(@target_files_keys_requiring_updating));
|
---|
[29415] | 1923 |
|
---|
| 1924 | my $download_target_filepath = "";
|
---|
| 1925 |
|
---|
| 1926 |
|
---|
| 1927 | # ****** DOWNLOADING LANGUAGE FILES WAS NOT YET IMPLEMENTED FOR GS3. RUDIMENTARY VERSION ****** #
|
---|
| 1928 |
|
---|
| 1929 | # if there is no copy of the language files for download, there's also no link to the spreadsheet
|
---|
| 1930 | # for translating offline. So GS3's download option, we will zip up all the relevant greenstone 3
|
---|
| 1931 | # interface *.properties files,and link to that zip as the file for offline translation.
|
---|
| 1932 | # Selecting only properties files for English and the language they're working on (if the last exists)
|
---|
| 1933 |
|
---|
| 1934 | # tar -cvzf gs3interface.tar.gz greenstone3/AbstractBrowse.properties greenstone3/AbstractBrowse_nl.properties
|
---|
| 1935 | # will generate a tar file containing a folder called "greenstone3" with the specified *.properties files
|
---|
| 1936 |
|
---|
| 1937 | my $zip = &FileUtils::filenameConcatenate("tmp", "gs3interface_".$target_language_code.".tar.gz");
|
---|
| 1938 | my $tar_cmd = "tar -cvzf $zip";
|
---|
| 1939 |
|
---|
| 1940 |
|
---|
| 1941 | # store cur dir and cd to gsdlhome to generate the correct path in the zip file
|
---|
| 1942 | my $curdir = `pwd`;
|
---|
| 1943 | chdir $gsdl_root_directory;
|
---|
| 1944 |
|
---|
[30581] | 1945 | $tar_cmd .= " " . &get_gs3_zip_file_listing($target_language_code, "greenstone3", \@gs3_interface_files);
|
---|
| 1946 | $tar_cmd .= " " . &get_gs3_zip_file_listing($target_language_code, "gs3-collection-configs", \@gs3_col_cfg_files);
|
---|
[29415] | 1947 |
|
---|
| 1948 | # tar command will overwrite the previous version, but want to check we've created it
|
---|
| 1949 | if(&FileUtils::fileExists($zip)) {
|
---|
| 1950 | &FileUtils::removeFiles($zip);
|
---|
| 1951 | }
|
---|
| 1952 |
|
---|
| 1953 | #my $tar_result = system($tar_cmd); # works but then interface breaks
|
---|
| 1954 | `$tar_cmd`;
|
---|
| 1955 | my $tar_result = $?;
|
---|
| 1956 |
|
---|
| 1957 | if(&FileUtils::fileExists($zip)) { ## if($tar_result == 0) {, # breaks the interface
|
---|
| 1958 | $download_target_filepath = $zip;
|
---|
| 1959 | } else {
|
---|
| 1960 | &log_message("Unable to generate zip containing gs3interface files " . $download_target_filepath . "$!");
|
---|
| 1961 | }
|
---|
| 1962 |
|
---|
| 1963 | # change back to original working directory (cgi-bin/linux probably)
|
---|
| 1964 | chdir $curdir;
|
---|
| 1965 |
|
---|
| 1966 | # ************** END RUDIMENTARY VERSION OF DOWNLOADING LANGUAGE FILES FOR GS3 ************* #
|
---|
| 1967 |
|
---|
| 1968 |
|
---|
| 1969 | my $xml_response = &create_xml_response_for_chunks_requiring_work($translation_file_key, $download_target_filepath, scalar(keys(%source_files_key_to_text_mapping)),
|
---|
[24627] | 1970 | \@target_files_keys_requiring_translation, \@target_files_keys_requiring_updating,
|
---|
| 1971 | $num_chunks_to_return, \%source_files_key_to_text_mapping, \%target_files_key_to_text_mapping,
|
---|
| 1972 | \%source_files_key_to_last_update_date_mapping, \%target_files_key_to_last_update_date_mapping);
|
---|
| 1973 |
|
---|
[18460] | 1974 | return $xml_response;
|
---|
| 1975 | }
|
---|
| 1976 |
|
---|
[30581] | 1977 | # helper function
|
---|
| 1978 | # gets the listing of gs3 files for a gs3 interface module (gs3interface, gs3colcfg)
|
---|
| 1979 | # formatted correctly to go into a zip file
|
---|
| 1980 | sub get_gs3_zip_file_listing
|
---|
| 1981 | {
|
---|
| 1982 | my $target_language_code = shift(@_);
|
---|
| 1983 | my $sourcedir = shift(@_);
|
---|
| 1984 | my $files_array = shift(@_); # reference to an array of the interfaces files for the gs3 module
|
---|
| 1985 |
|
---|
| 1986 | my $filelisting = "";
|
---|
| 1987 | foreach my $interface_file (@$files_array) {
|
---|
| 1988 |
|
---|
| 1989 | my $source_filepath = &FileUtils::filenameConcatenate($sourcedir, $interface_file.".properties");
|
---|
| 1990 | my $target_filepath = &FileUtils::filenameConcatenate($sourcedir, $interface_file."_".$target_language_code.".properties");
|
---|
| 1991 |
|
---|
| 1992 | $filelisting = "$filelisting $source_filepath";
|
---|
| 1993 | if(&FileUtils::fileExists($target_filepath)) {
|
---|
| 1994 | $filelisting = "$filelisting $target_filepath";
|
---|
| 1995 | }
|
---|
| 1996 | }
|
---|
| 1997 |
|
---|
| 1998 | return $filelisting;
|
---|
| 1999 | }
|
---|
| 2000 |
|
---|
[25249] | 2001 | sub get_uptodate_chunks_for_gs3
|
---|
| 2002 | {
|
---|
| 2003 | # The code of the target language (ensure it is lowercase)
|
---|
| 2004 | my $target_language_code = lc(shift(@_));
|
---|
| 2005 | # The key of the file to translate (ensure it is lowercase)
|
---|
| 2006 | my $translation_file_key = lc(shift(@_));
|
---|
| 2007 | # The number of chunks to return (defaults to one if not specified)
|
---|
| 2008 | my $num_chunks_to_return = shift(@_) || "1";
|
---|
| 2009 |
|
---|
| 2010 | # Check that the necessary arguments were supplied
|
---|
| 2011 | if (!$target_language_code || !$translation_file_key) {
|
---|
| 2012 | &throw_fatal_error("Missing command argument.");
|
---|
| 2013 | }
|
---|
| 2014 |
|
---|
| 2015 | my %source_files_key_to_text_mapping = ();
|
---|
| 2016 | my %target_files_key_to_text_mapping = ();
|
---|
| 2017 | my %source_files_key_to_last_update_date_mapping = ();
|
---|
| 2018 | my %target_files_key_to_last_update_date_mapping = ();
|
---|
| 2019 |
|
---|
[30581] | 2020 | &build_gs3_configuration($translation_file_key, $target_language_code, \%source_files_key_to_text_mapping, \%target_files_key_to_text_mapping,
|
---|
[25249] | 2021 | \%source_files_key_to_last_update_date_mapping, \%target_files_key_to_last_update_date_mapping);
|
---|
| 2022 |
|
---|
[18460] | 2023 |
|
---|
[25249] | 2024 | # Chunks needing updating are those in the target file that have been more recently edited in the source file
|
---|
| 2025 | # All others are uptodate (which implies that they have certainly been translated at some point and would not be empty)
|
---|
| 2026 | my @uptodate_target_file_keys = ();
|
---|
| 2027 | foreach my $chunk_key (keys(%source_files_key_to_last_update_date_mapping)) {
|
---|
| 2028 | my $source_chunk_last_update_date = $source_files_key_to_last_update_date_mapping{$chunk_key};
|
---|
| 2029 | my $target_chunk_last_update_date = $target_files_key_to_last_update_date_mapping{$chunk_key};
|
---|
| 2030 |
|
---|
| 2031 | # print "key: $chunk_key\nsource date : $source_chunk_last_update_date\ntarget date : $target_chunk_last_update_date\nafter? ". &is_date_after($source_chunk_last_update_date, $target_chunk_last_update_date) . "\n\n";
|
---|
| 2032 |
|
---|
| 2033 | if (defined($target_chunk_last_update_date) && !&is_date_after($source_chunk_last_update_date, $target_chunk_last_update_date)) {
|
---|
| 2034 | # &log_message("Chunk with key $chunk_key needs updating.");
|
---|
| 2035 | push(@uptodate_target_file_keys, $chunk_key);
|
---|
| 2036 | }
|
---|
| 2037 | }
|
---|
[18460] | 2038 |
|
---|
[25249] | 2039 | my $xml_response = &create_xml_response_for_uptodate_chunks($translation_file_key, "", \@uptodate_target_file_keys, \%source_files_key_to_text_mapping, \%target_files_key_to_text_mapping, \%source_files_key_to_last_update_date_mapping, \%target_files_key_to_last_update_date_mapping);
|
---|
| 2040 |
|
---|
| 2041 | return $xml_response;
|
---|
| 2042 | }
|
---|
| 2043 |
|
---|
| 2044 |
|
---|
[18460] | 2045 | sub build_gs3_configuration
|
---|
| 2046 | {
|
---|
[30581] | 2047 | my ($translation_file_key, $target_language_code, $source_files_key_to_text_mapping, $target_files_key_to_text_mapping,
|
---|
[30548] | 2048 | $source_files_key_to_gti_comment_or_last_updated_mapping, $target_files_key_to_gti_comment_or_last_updated_mapping, $get_gti_comments_not_last_updated) = @_;
|
---|
[18460] | 2049 |
|
---|
[28755] | 2050 | my $source_file_directory = "greenstone3"; # my $source_file_directory = &util::filename_cat("WEB-INF","classes");
|
---|
[30581] | 2051 | my $files_array = \@gs3_interface_files;
|
---|
| 2052 |
|
---|
| 2053 | if($translation_file_key eq "gs3colcfg") {
|
---|
| 2054 | $source_file_directory = "gs3-collection-configs";
|
---|
| 2055 | $files_array = \@gs3_col_cfg_files;
|
---|
| 2056 | }
|
---|
[18460] | 2057 | my $translation_file_type = "resource_bundle";
|
---|
[24627] | 2058 |
|
---|
[30581] | 2059 | foreach my $interface_file_key (@$files_array) {
|
---|
[24627] | 2060 |
|
---|
| 2061 | &log_message("Greenstone 3 interface file: " . $interface_file_key);
|
---|
| 2062 |
|
---|
| 2063 | # Parse the source language and target language files
|
---|
| 2064 | my $source_file = &util::filename_cat($source_file_directory, $interface_file_key.".properties");
|
---|
| 2065 | my @source_file_lines = &read_file_lines(&util::filename_cat($gsdl_root_directory, $source_file));
|
---|
| 2066 | my %source_file_key_to_line_mapping = &build_key_to_line_mapping(\@source_file_lines, $translation_file_type);
|
---|
| 2067 | my %source_file_key_to_text_mapping = &build_key_to_text_mapping(\@source_file_lines, \%source_file_key_to_line_mapping, $translation_file_type);
|
---|
[30548] | 2068 | #my %source_file_key_to_gti_comment_mapping = &build_key_to_gti_comment_mapping(\@source_file_lines, \%source_file_key_to_line_mapping, $translation_file_type);
|
---|
[24627] | 2069 |
|
---|
[30548] | 2070 | my %source_file_key_to_gti_comment_or_last_updated_mapping;
|
---|
| 2071 | if($get_gti_comments_not_last_updated) {
|
---|
| 2072 | %source_file_key_to_gti_comment_or_last_updated_mapping = &build_key_to_gti_comment_mapping(\@source_file_lines, \%source_file_key_to_line_mapping, $translation_file_type);
|
---|
| 2073 | } else {
|
---|
| 2074 | %source_file_key_to_gti_comment_or_last_updated_mapping = &build_key_to_last_update_date_mapping($source_file, \@source_file_lines, \%source_file_key_to_line_mapping, $translation_file_type);
|
---|
| 2075 | }
|
---|
| 2076 |
|
---|
[24627] | 2077 | my $target_file = &util::filename_cat($source_file_directory, $interface_file_key."_".$target_language_code.".properties");
|
---|
| 2078 | my @target_file_lines = &read_file_lines(&util::filename_cat($gsdl_root_directory, $target_file));
|
---|
| 2079 | my %target_file_key_to_line_mapping = &build_key_to_line_mapping(\@target_file_lines, $translation_file_type);
|
---|
| 2080 | my %target_file_key_to_text_mapping = &build_key_to_text_mapping(\@target_file_lines, \%target_file_key_to_line_mapping, $translation_file_type);
|
---|
[30548] | 2081 | #my %target_file_key_to_gti_comment_mapping = &build_key_to_gti_comment_mapping(\@target_file_lines, \%target_file_key_to_line_mapping, $translation_file_type);
|
---|
[24627] | 2082 |
|
---|
[30548] | 2083 | my %target_file_key_to_gti_comment_or_last_updated_mapping;
|
---|
| 2084 | if($get_gti_comments_not_last_updated) {
|
---|
| 2085 | %target_file_key_to_gti_comment_or_last_updated_mapping = &build_key_to_gti_comment_mapping(\@target_file_lines, \%target_file_key_to_line_mapping, $translation_file_type);
|
---|
| 2086 | } else {
|
---|
| 2087 | %target_file_key_to_gti_comment_or_last_updated_mapping = &build_key_to_last_update_date_mapping($target_file, \@target_file_lines, \%target_file_key_to_line_mapping, $translation_file_type);
|
---|
| 2088 | }
|
---|
[24627] | 2089 |
|
---|
[30548] | 2090 |
|
---|
[24627] | 2091 | # Filter out any automatically translated chunks
|
---|
| 2092 | foreach my $chunk_key (keys(%source_file_key_to_line_mapping)) {
|
---|
| 2093 | if (&is_chunk_automatically_translated($chunk_key, $translation_file_type)) {
|
---|
| 2094 | delete $source_file_key_to_line_mapping{$chunk_key};
|
---|
| 2095 | delete $target_file_key_to_line_mapping{$chunk_key};
|
---|
| 2096 | }
|
---|
| 2097 | }
|
---|
| 2098 |
|
---|
| 2099 | &log_message("Number of source chunks: " . scalar(keys(%source_file_key_to_text_mapping)));
|
---|
| 2100 | &log_message("Number of target chunks: " . scalar(keys(%target_file_key_to_text_mapping)));
|
---|
| 2101 |
|
---|
| 2102 | foreach my $chunk_key (keys(%source_file_key_to_text_mapping)) {
|
---|
| 2103 | my $global_chunk_key = "$interface_file_key.$chunk_key";
|
---|
| 2104 | $source_files_key_to_text_mapping->{$global_chunk_key} = $source_file_key_to_text_mapping{$chunk_key};
|
---|
[30548] | 2105 | $source_files_key_to_gti_comment_or_last_updated_mapping->{$global_chunk_key} = $source_file_key_to_gti_comment_or_last_updated_mapping{$chunk_key};
|
---|
[24627] | 2106 |
|
---|
| 2107 | if (defined $target_file_key_to_text_mapping{$chunk_key}) {
|
---|
| 2108 | $target_files_key_to_text_mapping->{$global_chunk_key} = $target_file_key_to_text_mapping{$chunk_key};
|
---|
[30548] | 2109 | $target_files_key_to_gti_comment_or_last_updated_mapping->{$global_chunk_key} = $target_file_key_to_gti_comment_or_last_updated_mapping{$chunk_key};
|
---|
[24627] | 2110 | }
|
---|
[29456] | 2111 | }
|
---|
[18460] | 2112 | }
|
---|
| 2113 | }
|
---|
| 2114 |
|
---|
| 2115 |
|
---|
| 2116 | sub write_translated_gs3interface
|
---|
| 2117 | {
|
---|
[30581] | 2118 | my $translation_file_key = shift(@_);
|
---|
[18460] | 2119 | my $source_file_key_to_text_mapping = shift(@_);
|
---|
| 2120 | my $target_file_key_to_text_mapping = shift(@_);
|
---|
| 2121 | my $target_file_key_to_gti_comment_mapping = shift(@_);
|
---|
| 2122 | my $target_language_code = shift(@_);
|
---|
| 2123 |
|
---|
| 2124 | my @sorted_chunk_keys = sort (keys(%$source_file_key_to_text_mapping));
|
---|
[24627] | 2125 |
|
---|
[18460] | 2126 | my %translated_interface_file_keys = ();
|
---|
| 2127 | foreach my $chunk_key (keys(%$target_file_key_to_text_mapping)) {
|
---|
[24627] | 2128 | $chunk_key =~ /^([^\.]+)?\.(.*)$/;
|
---|
| 2129 | if (!defined $translated_interface_file_keys{$1}) {
|
---|
| 2130 | &log_message("Updated interface file: " . $1);
|
---|
| 2131 | $translated_interface_file_keys{$1}="";
|
---|
| 2132 | }
|
---|
[18460] | 2133 | }
|
---|
| 2134 | &log_message("Updated interface files: " . scalar(keys(%translated_interface_file_keys)));
|
---|
| 2135 |
|
---|
| 2136 | my $source_file_directory = "greenstone3";
|
---|
[30581] | 2137 | $source_file_directory = "gs3-collection-configs" if $translation_file_key eq "gs3colcfg";
|
---|
| 2138 |
|
---|
[18460] | 2139 | foreach my $interface_file_key (keys(%translated_interface_file_keys)) {
|
---|
[24627] | 2140 |
|
---|
| 2141 | # Build a mapping from chunk key to source file line, and from source file line to chunk key
|
---|
| 2142 | my $source_file = &util::filename_cat($source_file_directory, "$interface_file_key.properties");
|
---|
| 2143 | my @source_file_lines = &read_file_lines(&util::filename_cat($gsdl_root_directory, $source_file));
|
---|
| 2144 | my %source_file_key_to_line_mapping = &build_key_to_line_mapping_for_resource_bundle(@source_file_lines);
|
---|
| 2145 | my %source_file_line_to_key_mapping = ();
|
---|
| 2146 | foreach my $chunk_key (keys(%source_file_key_to_line_mapping)) {
|
---|
| 2147 | $source_file_line_to_key_mapping{$source_file_key_to_line_mapping{$chunk_key}} = $chunk_key;
|
---|
[18460] | 2148 | }
|
---|
[24627] | 2149 |
|
---|
| 2150 | # Write the new target file
|
---|
| 2151 | my $target_file = &util::filename_cat($source_file_directory, $interface_file_key . "_" . $target_language_code . ".properties");
|
---|
| 2152 | my $target_file_path = &util::filename_cat($gsdl_root_directory, $target_file);
|
---|
| 2153 | if (!open(TARGET_FILE, ">$target_file_path")) {
|
---|
| 2154 | &throw_fatal_error("Could not write target file $target_file_path.");
|
---|
| 2155 | }
|
---|
| 2156 |
|
---|
| 2157 | # Model the new target file on the source file, with the target file translations
|
---|
| 2158 | my $source_file_line_number = 0;
|
---|
| 2159 | foreach my $line_key (sort sort_by_line (keys(%source_file_line_to_key_mapping))) {
|
---|
| 2160 | # Fill in the gaps before this chunk starts
|
---|
| 2161 | my $source_file_chunk_starting_line_number = (split(/-/, $line_key))[0];
|
---|
| 2162 | my $source_file_chunk_finishing_line_number = (split(/-/, $line_key))[1];
|
---|
| 2163 | while ($source_file_line_number < $source_file_chunk_starting_line_number) {
|
---|
| 2164 | print TARGET_FILE $source_file_lines[$source_file_line_number];
|
---|
| 2165 | $source_file_line_number++;
|
---|
| 2166 | }
|
---|
| 2167 | $source_file_line_number = $source_file_chunk_finishing_line_number + 1;
|
---|
| 2168 |
|
---|
| 2169 | my $chunk_key = $source_file_line_to_key_mapping{$line_key};
|
---|
| 2170 | my $global_chunk_key = "$interface_file_key.$chunk_key";
|
---|
| 2171 | my $source_file_chunk_text = $source_file_key_to_text_mapping->{$global_chunk_key};
|
---|
| 2172 | my $target_file_chunk_text = $target_file_key_to_text_mapping->{$global_chunk_key} || "";
|
---|
| 2173 |
|
---|
| 2174 | # If no translation exists for this chunk, show this, and move on
|
---|
| 2175 | if ($source_file_chunk_text ne "" && $target_file_chunk_text eq "") {
|
---|
| 2176 | print TARGET_FILE "# -- Missing translation: $chunk_key\n";
|
---|
| 2177 | next;
|
---|
| 2178 | }
|
---|
| 2179 |
|
---|
| 2180 | print TARGET_FILE "$chunk_key:$target_file_chunk_text";
|
---|
| 2181 | if ($target_file_key_to_gti_comment_mapping->{$global_chunk_key}) {
|
---|
| 2182 | print TARGET_FILE " # " . $target_file_key_to_gti_comment_mapping->{$global_chunk_key};
|
---|
| 2183 | }
|
---|
| 2184 | print TARGET_FILE "\n";
|
---|
| 2185 | }
|
---|
| 2186 |
|
---|
| 2187 | close(TARGET_FILE);
|
---|
[18460] | 2188 | }
|
---|
| 2189 | }
|
---|
| 2190 |
|
---|
[10019] | 2191 | &main(@ARGV);
|
---|