1 | ###########################################################################
|
---|
2 | #
|
---|
3 | # translator.pm --
|
---|
4 | # A component of the Greenstone digital library software
|
---|
5 | # from the New Zealand Digital Library Project at the
|
---|
6 | # University of Waikato, New Zealand.
|
---|
7 | #
|
---|
8 | # Copyright (C) 1999 New Zealand Digital Library Project
|
---|
9 | #
|
---|
10 | # This program is free software; you can redistribute it and/or modify
|
---|
11 | # it under the terms of the GNU General Public License as published by
|
---|
12 | # the Free Software Foundation; either version 2 of the License, or
|
---|
13 | # (at your option) any later version.
|
---|
14 | #
|
---|
15 | # This program is distributed in the hope that it will be useful,
|
---|
16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
18 | # GNU General Public License for more details.
|
---|
19 | #
|
---|
20 | # You should have received a copy of the GNU General Public License
|
---|
21 | # along with this program; if not, write to the Free Software
|
---|
22 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
23 | #
|
---|
24 | ###########################################################################
|
---|
25 |
|
---|
26 |
|
---|
27 | # Common functions used by the different parts of the translator
|
---|
28 | package translator;
|
---|
29 |
|
---|
30 |
|
---|
31 | use util;
|
---|
32 | use GDBM_File;
|
---|
33 |
|
---|
34 |
|
---|
35 | my $gsdldir = "$ENV{'GSDLHOME'}";
|
---|
36 | my $maincfgfile = &util::filename_cat($gsdldir, "etc", "main.cfg");
|
---|
37 | my $translatordir = &util::filename_cat($gsdldir, "translator");
|
---|
38 | my $translatorcfgfile = &util::filename_cat($gsdldir, "etc", "translator.cfg");
|
---|
39 |
|
---|
40 | my $cvsroot = ":pserver:cvs_anon\@cvs.scms.waikato.ac.nz:2402/usr/local/global-cvs/gsdl-src";
|
---|
41 |
|
---|
42 |
|
---|
43 | sub get_translator_dir
|
---|
44 | {
|
---|
45 | return $translatordir;
|
---|
46 | }
|
---|
47 |
|
---|
48 |
|
---|
49 | sub get_cvs_root
|
---|
50 | {
|
---|
51 | return $cvsroot;
|
---|
52 | }
|
---|
53 |
|
---|
54 |
|
---|
55 | sub read_languages_from_main_cfg_file
|
---|
56 | {
|
---|
57 | my %code2namehash = ();
|
---|
58 |
|
---|
59 | # Read the main.cfg file
|
---|
60 | if (!open(MAIN_CFG_FILE, "<$maincfgfile")) {
|
---|
61 | print STDERR "Error: Could not open $maincfgfile.\n";
|
---|
62 | die "\n";
|
---|
63 | }
|
---|
64 | while (<MAIN_CFG_FILE>) {
|
---|
65 | my $line = $_;
|
---|
66 | chomp($line);
|
---|
67 |
|
---|
68 | # Parse the lines containing a language definition
|
---|
69 | if ($line =~ m/^Language\s+/i) {
|
---|
70 | my ($langcode) = ($line =~ /shortname=(\S+)/i);
|
---|
71 | my ($langname) = ($line =~ /longname=(\"[^\"]+\"|\S+)/i);
|
---|
72 |
|
---|
73 | # Casefold the language code, and remove any quotes around the language name
|
---|
74 | $langcode =~ tr/A-Z/a-z/;
|
---|
75 | $langname =~ s/^\"(.+)\"$/$1/;
|
---|
76 |
|
---|
77 | $code2namehash{$langcode} = $langname;
|
---|
78 | }
|
---|
79 | }
|
---|
80 | close(MAIN_CFG_FILE);
|
---|
81 |
|
---|
82 | return %code2namehash;
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 | sub read_translation_files_from_config_file
|
---|
87 | {
|
---|
88 | my %name2deschash = ();
|
---|
89 |
|
---|
90 | # Read the translator config file
|
---|
91 | if (!open(CONFIG_FILE, "<$translatorcfgfile")) {
|
---|
92 | print STDERR "Error: Could not open $translatorcfgfile.\n";
|
---|
93 | die "\n";
|
---|
94 | }
|
---|
95 | while (<CONFIG_FILE>) {
|
---|
96 | my $line = $_;
|
---|
97 | chomp($line);
|
---|
98 |
|
---|
99 | # Check if a file is being defined
|
---|
100 | if ($line =~ m/^File\s+/i) {
|
---|
101 | my ($fileid) = ($line =~ /id=(\S+)/i);
|
---|
102 | my ($filedesc) = ($line =~ /description=(\S+)/i);
|
---|
103 | $name2deschash{$fileid} = $filedesc;
|
---|
104 | }
|
---|
105 | }
|
---|
106 | close(CONFIG_FILE);
|
---|
107 |
|
---|
108 | return %name2deschash;
|
---|
109 | }
|
---|
110 |
|
---|
111 |
|
---|
112 | sub read_file_data_from_config_file
|
---|
113 | {
|
---|
114 | my ($sourceabbr, $targetabbr, $file) = @_;
|
---|
115 |
|
---|
116 | # Read the translator config file
|
---|
117 | if (!open(CONFIG_FILE, "<$translatorcfgfile")) {
|
---|
118 | print STDERR "Error: Could not open $translatorcfgfile.\n";
|
---|
119 | die "\n";
|
---|
120 | }
|
---|
121 | while (<CONFIG_FILE>) {
|
---|
122 | my $line = $_;
|
---|
123 | chomp($line);
|
---|
124 |
|
---|
125 | # Check if a file is being defined
|
---|
126 | if ($line =~ m/^File\s+/i) {
|
---|
127 | my @lineparts = split(/\s+/, $line);
|
---|
128 | my ($fileid) = ($lineparts[1] =~ m/^id=(.*)$/);
|
---|
129 |
|
---|
130 | # If this is the file we want, return its details
|
---|
131 | if ($fileid eq $file) {
|
---|
132 | my ($filedesc) = ($lineparts[2] =~ m/^description=(.*)$/);
|
---|
133 | my ($sourcefile) = ($lineparts[3] =~ m/^sourcefile=(.*)$/);
|
---|
134 | my ($targetfile) = ($lineparts[4] =~ m/^targetfile=(.*)$/);
|
---|
135 | my ($filetype) = ($lineparts[5] =~ m/^type=(.*)$/);
|
---|
136 |
|
---|
137 | # Determine sourcebase and targetbase, using the filetype
|
---|
138 | my $sourcebase = &get_file_base($sourceabbr, $filetype);
|
---|
139 | my $targetbase = &get_file_base($targetabbr, $filetype);
|
---|
140 |
|
---|
141 | # Resolve instances of {sourceabbr}, {sourcebase}, {targetabbr}, {targetbase}
|
---|
142 | $sourcefile =~ s/\{sourceabbr\}/$sourceabbr/g;
|
---|
143 | $sourcefile =~ s/\{sourcebase\}/$sourcebase/g;
|
---|
144 | $targetfile =~ s/\{targetabbr\}/$targetabbr/g;
|
---|
145 | $targetfile =~ s/\{targetbase\}/$targetbase/g;
|
---|
146 |
|
---|
147 | close(CONFIG_FILE);
|
---|
148 | return ($sourcefile, $targetfile, $filetype);
|
---|
149 | }
|
---|
150 | }
|
---|
151 | }
|
---|
152 | close(CONFIG_FILE);
|
---|
153 |
|
---|
154 | return ();
|
---|
155 | }
|
---|
156 |
|
---|
157 |
|
---|
158 | sub get_translation_file_path
|
---|
159 | {
|
---|
160 | my ($translationdir, $relativefilepath) = @_;
|
---|
161 |
|
---|
162 | # Extract the file name from the relative file path
|
---|
163 | $relativefilepath =~ /([^\\\/]+)$/;
|
---|
164 | return &util::filename_cat($translationdir, $1);
|
---|
165 | }
|
---|
166 |
|
---|
167 |
|
---|
168 | sub get_file_base
|
---|
169 | {
|
---|
170 | my ($langabbr, $filetype) = @_;
|
---|
171 |
|
---|
172 | if ($filetype =~ /^macrofile/) {
|
---|
173 | return &get_macrofile_base($langabbr);
|
---|
174 | }
|
---|
175 | if ($filetype =~ /^resourcebundle/ || $filetype =~ /^myresourcebundle/) {
|
---|
176 | return &get_resource_bundle_base($langabbr);
|
---|
177 | }
|
---|
178 |
|
---|
179 | print STDERR "Error: Unknown file type $filetype.\n";
|
---|
180 | die "\n";
|
---|
181 | }
|
---|
182 |
|
---|
183 |
|
---|
184 | sub process_file
|
---|
185 | {
|
---|
186 | my ($filepath, $filetype) = @_;
|
---|
187 |
|
---|
188 | # Build a mapping from key to lines
|
---|
189 | my %linehash = &build_key_to_line_mapping($filepath, $filetype);
|
---|
190 |
|
---|
191 | # Build a mapping from chunk key to chunk text, and return
|
---|
192 | return &build_key_to_text_mapping($filepath, $filetype, %linehash);
|
---|
193 | }
|
---|
194 |
|
---|
195 |
|
---|
196 | sub build_key_to_line_mapping
|
---|
197 | {
|
---|
198 | my ($filepath, $filetype) = @_;
|
---|
199 |
|
---|
200 | if ($filetype =~ /^macrofile/) {
|
---|
201 | return &build_key_to_line_mapping_for_macrofile($filepath);
|
---|
202 | }
|
---|
203 | if ($filetype =~ /^resourcebundle/) {
|
---|
204 | return &build_key_to_line_mapping_for_resource_bundle($filepath);
|
---|
205 | }
|
---|
206 | if ($filetype =~ /^myresourcebundle/) {
|
---|
207 | return &build_key_to_line_mapping_for_my_resource_bundle($filepath);
|
---|
208 | }
|
---|
209 |
|
---|
210 | print STDERR "Error: Unknown file type $filetype.\n";
|
---|
211 | die "\n";
|
---|
212 | }
|
---|
213 |
|
---|
214 |
|
---|
215 | sub build_key_to_text_mapping
|
---|
216 | {
|
---|
217 | my ($filepath, $filetype, %linehash) = @_;
|
---|
218 |
|
---|
219 | if (!open(FILE_IN, "<$filepath")) {
|
---|
220 | print STDERR "Error: Could not open file $filepath.\n";
|
---|
221 | die "\n";
|
---|
222 | }
|
---|
223 | my @lines = <FILE_IN>;
|
---|
224 | close FILE_IN;
|
---|
225 |
|
---|
226 | my %chunks = ();
|
---|
227 |
|
---|
228 | foreach $key (keys(%linehash)) {
|
---|
229 | # Chunk lines in linehash start at 0
|
---|
230 | my $chunkstart = (split(/-/, $linehash{$key}))[0];
|
---|
231 | my $chunkfinish = (split(/-/, $linehash{$key}))[1];
|
---|
232 |
|
---|
233 | my $chunktext = $lines[$chunkstart];
|
---|
234 | for ($l = ($chunkstart + 1); $l <= $chunkfinish; $l++) {
|
---|
235 | $chunktext .= $lines[$l];
|
---|
236 | }
|
---|
237 |
|
---|
238 | # Map from chunk key to (formatted) text
|
---|
239 | $chunks{$key} = &import_chunk($chunktext, $filetype);
|
---|
240 | }
|
---|
241 |
|
---|
242 | return %chunks;
|
---|
243 | }
|
---|
244 |
|
---|
245 |
|
---|
246 | sub import_chunk
|
---|
247 | {
|
---|
248 | my ($chunktext, $filetype) = @_;
|
---|
249 |
|
---|
250 | if ($filetype =~ /^macrofile/) {
|
---|
251 | return &import_chunk_from_macrofile($chunktext);
|
---|
252 | }
|
---|
253 | if ($filetype =~ /^resourcebundle/) {
|
---|
254 | return &import_chunk_from_resource_bundle($chunktext);
|
---|
255 | }
|
---|
256 | if ($filetype =~ /^myresourcebundle/) {
|
---|
257 | return &import_chunk_from_my_resource_bundle($chunktext);
|
---|
258 | }
|
---|
259 |
|
---|
260 | print STDERR "Error: Unknown file type $filetype.\n";
|
---|
261 | die "\n";
|
---|
262 | }
|
---|
263 |
|
---|
264 |
|
---|
265 | sub refresh_translation
|
---|
266 | {
|
---|
267 | my ($translationdir, $sourcefile, $filetype) = @_;
|
---|
268 |
|
---|
269 |
|
---|
270 | # -----------------------------------------
|
---|
271 | # Process the source language file
|
---|
272 | # -----------------------------------------
|
---|
273 |
|
---|
274 | # Parse the backup source language file, building a mapping from chunk key to chunk text
|
---|
275 | my $sourcepath = &get_translation_file_path($translationdir, $sourcefile);
|
---|
276 | my %backuphash = &process_file($sourcepath, $filetype);
|
---|
277 | print STDOUT "Number of backup chunks: " . scalar(keys(%backuphash)) . "\n";
|
---|
278 |
|
---|
279 | # Remove the (possibly out of date) source file
|
---|
280 | my $hotsourcepath = &util::filename_cat($gsdldir, $sourcefile);
|
---|
281 | # &util::rm($hotsourcepath);
|
---|
282 |
|
---|
283 | # Get the latest version of the source file from CVS
|
---|
284 | # my $sourceupdate = `cd $gsdldir; cvs -d $cvsroot update $sourcefile`;
|
---|
285 | # if ($sourceupdate !~ /^U /) {
|
---|
286 | # print STDERR "Error: Could not update source file.\n";
|
---|
287 | # die "\n";
|
---|
288 | # }
|
---|
289 |
|
---|
290 | # Check that the source file now exists
|
---|
291 | if (!-e $hotsourcepath) {
|
---|
292 | print STDERR "Error: Source file $hotsourcepath does not exist.\n";
|
---|
293 | die "\n";
|
---|
294 | }
|
---|
295 |
|
---|
296 | # Copy the new version of the source file to become the backup file
|
---|
297 | &util::cp($hotsourcepath, $sourcepath);
|
---|
298 |
|
---|
299 | # Parse the new source language file, building a mapping from chunk key to chunk text
|
---|
300 | my %sourcehash = &process_file($sourcepath, $filetype);
|
---|
301 | print STDOUT "Number of source chunks: " . scalar(keys(%sourcehash)) . "\n";
|
---|
302 |
|
---|
303 |
|
---|
304 | # -----------------------------------------
|
---|
305 | # Rebuild the translation database
|
---|
306 | # -----------------------------------------
|
---|
307 |
|
---|
308 | # Open the translation database for this file
|
---|
309 | my $translationdb = &util::filename_cat($translationdir, "translation.db");
|
---|
310 | my %translationdata = ();
|
---|
311 | tie(%translationdata, "GDBM_File", $translationdb, GDBM_WRCREAT, 0666);
|
---|
312 |
|
---|
313 | # Remove translation data for any chunks that no longer exist
|
---|
314 | foreach $key (keys(%translationdata)) {
|
---|
315 | my $sourcekey = $key;
|
---|
316 | if ($sourcekey =~ /\*$/) {
|
---|
317 | chop($sourcekey);
|
---|
318 | }
|
---|
319 |
|
---|
320 | # If the chunk doesn't exist in the new source file, remove it from the database
|
---|
321 | if (!defined($sourcehash{$sourcekey})) {
|
---|
322 | print STDOUT "Chunk with key $key no longer exists!\n";
|
---|
323 | delete $translationdata{$key};
|
---|
324 | }
|
---|
325 | }
|
---|
326 |
|
---|
327 | # Update the translation database to reflect the new source file
|
---|
328 | foreach $key (keys(%sourcehash)) {
|
---|
329 | # Add translation data for any new chunks
|
---|
330 | if (!defined($backuphash{$key})) {
|
---|
331 | print STDOUT "Chunk with key $key is new!\n";
|
---|
332 | $translationdata{$key} = "";
|
---|
333 | }
|
---|
334 |
|
---|
335 | # Source chunks that have changed since last time must have their translations updated
|
---|
336 | if ($backuphash{$key} && $backuphash{$key} ne $sourcehash{$key}) {
|
---|
337 | print STDOUT "Chunk with key $key needs updating!\n";
|
---|
338 | $translationdata{$key . "*"} = "";
|
---|
339 | }
|
---|
340 | }
|
---|
341 |
|
---|
342 | # Close the old translation database
|
---|
343 | untie(%translationdata);
|
---|
344 | }
|
---|
345 |
|
---|
346 |
|
---|
347 | # ==========================================================================================
|
---|
348 |
|
---|
349 | sub get_macrofile_base
|
---|
350 | {
|
---|
351 | my ($langcode) = @_;
|
---|
352 |
|
---|
353 | # List special cases here (macrofile names differ from language longnames)
|
---|
354 | my %specialcases = ('es', 'spanish',
|
---|
355 | 'fr', 'french',
|
---|
356 | 'id', 'indo',
|
---|
357 | 'kz', 'kazakh',
|
---|
358 | 'pt-br', 'port-br',
|
---|
359 | 'pt-pt', 'port-pt',
|
---|
360 | 'ru', 'russian',
|
---|
361 | 'zh-tr', 'chinese-trad',
|
---|
362 | 'zh', 'chinese' );
|
---|
363 |
|
---|
364 | # Special cases override main.cfg entries
|
---|
365 | if ($specialcases{$langcode}) {
|
---|
366 | return $specialcases{$langcode};
|
---|
367 | }
|
---|
368 |
|
---|
369 | # Use the language name defined in the main.cfg file as the macrofile name
|
---|
370 | my %langhash = &read_languages_from_main_cfg_file();
|
---|
371 | my $langname = $langhash{$langcode} || die "Error: Language to base mapping undefined!\n";
|
---|
372 | $langname =~ tr/A-Z/a-z/;
|
---|
373 | return $langname;
|
---|
374 | }
|
---|
375 |
|
---|
376 |
|
---|
377 | sub build_key_to_line_mapping_for_macrofile
|
---|
378 | {
|
---|
379 | my ($filepath) = @_;
|
---|
380 |
|
---|
381 | # Open file for reading
|
---|
382 | if (!open(FILE_IN, "<$filepath")) {
|
---|
383 | print STDERR "Error: Could not open macrofile $filepath.\n";
|
---|
384 | die "\n";
|
---|
385 | }
|
---|
386 | my @lines = <FILE_IN>;
|
---|
387 | close FILE_IN;
|
---|
388 |
|
---|
389 | # Initialise some my variables
|
---|
390 | my $currpackage;
|
---|
391 | my %linehash = ();
|
---|
392 |
|
---|
393 | # Process the contents of the file, line by line
|
---|
394 | for ($i = 0; $i < scalar(@lines); $i++) {
|
---|
395 | my $line = $lines[$i];
|
---|
396 | $line =~ s/(\s*)$//; # Remove any nasty whitespace, carriage returns etc.
|
---|
397 |
|
---|
398 | # Check if a new package is being defined
|
---|
399 | if ($line =~ m/^package\s+(.+)/) {
|
---|
400 | $currpackage = $1;
|
---|
401 | }
|
---|
402 |
|
---|
403 | # Line contains a macro name
|
---|
404 | elsif ($line =~ m/^(_\w+_)/) {
|
---|
405 | my $macroname = $1;
|
---|
406 | $line =~ s/\s*([^\\]\#.+)?$//; # Remove any comments and nasty whitespace
|
---|
407 |
|
---|
408 | # While there is still text of the macro to go...
|
---|
409 | my $startline = $i;
|
---|
410 | while ($line !~ /\}$/) {
|
---|
411 | $i++;
|
---|
412 | $line = $lines[$i];
|
---|
413 | $line =~ s/(\s*)$//; # Remove any nasty whitespace, carriage returns etc.
|
---|
414 | }
|
---|
415 |
|
---|
416 | # The key consists of the package name and the macro name
|
---|
417 | my $key = $currpackage . "::" . $macroname;
|
---|
418 | # Map from key to line
|
---|
419 | $linehash{$key} = $startline . "-" . $i;
|
---|
420 | }
|
---|
421 |
|
---|
422 | # Icon: line in format ## "sometext" ## macro ## macroname ##
|
---|
423 | elsif ($line =~ m/^\#\# (.*)/) {
|
---|
424 | my $macroname = $1;
|
---|
425 |
|
---|
426 | # If the macro text contains a new line it will run over two lines
|
---|
427 | my $startline = $i;
|
---|
428 | unless ($line =~ m/^\#\# .*\#\#/) {
|
---|
429 | $i++;
|
---|
430 | $line = $lines[$i];
|
---|
431 | $macroname .= $line;
|
---|
432 | }
|
---|
433 |
|
---|
434 | # Split the image macro header on ##
|
---|
435 | my @names = split(/\s*\#\#\s*/, $macroname);
|
---|
436 | # Overwrite macroname with macroname from ## ... ## ... ## HERE ###
|
---|
437 | $macroname = $names[(scalar @names) - 1];
|
---|
438 |
|
---|
439 | # Read the rest of the text associated with the image macro
|
---|
440 | while (defined($line) && $line !~ /^\s*$/) {
|
---|
441 | $i++;
|
---|
442 | $line = $lines[$i];
|
---|
443 | }
|
---|
444 |
|
---|
445 | # The key consists of package name and macro name
|
---|
446 | my $key = $currpackage . "::" . $macroname;
|
---|
447 | # Map from key to line
|
---|
448 | $linehash{$key} = $startline . "-" . ($i - 1);
|
---|
449 | }
|
---|
450 | }
|
---|
451 |
|
---|
452 | return %linehash;
|
---|
453 | }
|
---|
454 |
|
---|
455 |
|
---|
456 | sub import_chunk_from_macrofile
|
---|
457 | {
|
---|
458 | my ($chunktext) = @_;
|
---|
459 |
|
---|
460 | # Is this an icon macro??
|
---|
461 | if ($chunktext =~ /^\#\# (.*)/) {
|
---|
462 | # Extract image macro text
|
---|
463 | $chunktext =~ /^\#\#\s+([^\#]+)\s+\#\#/;
|
---|
464 | $chunktext = $1;
|
---|
465 |
|
---|
466 | # Remove enclosing quotes
|
---|
467 | $chunktext =~ s/^\"//;
|
---|
468 | $chunktext =~ s/\"$//;
|
---|
469 | }
|
---|
470 |
|
---|
471 | # No, so it must be a text macro
|
---|
472 | else {
|
---|
473 | # Remove macro key
|
---|
474 | $chunktext =~ s/^_([^_]+)_(\s*)//;
|
---|
475 |
|
---|
476 | # Remove language specifier
|
---|
477 | $chunktext =~ s/^\[l=.*\](\s*)//;
|
---|
478 |
|
---|
479 | # Remove braces enclosing text
|
---|
480 | $chunktext =~ s/^{(\s*)((.|\n)*)}(\s*)(\#.+\s*)?/$2/;
|
---|
481 | }
|
---|
482 |
|
---|
483 | return $chunktext;
|
---|
484 | }
|
---|
485 |
|
---|
486 |
|
---|
487 | # ------------------------------------------------------------------------------------------
|
---|
488 |
|
---|
489 | sub get_resource_bundle_base
|
---|
490 | {
|
---|
491 | my ($langabbr) = @_;
|
---|
492 |
|
---|
493 | return "";
|
---|
494 | }
|
---|
495 |
|
---|
496 |
|
---|
497 | sub build_key_to_line_mapping_for_resource_bundle
|
---|
498 | {
|
---|
499 | my ($filepath) = @_;
|
---|
500 |
|
---|
501 | # Open file for reading
|
---|
502 | if (!open(FILE_IN, "<$filepath")) {
|
---|
503 | print STDERR "Error: Could not open resource bundle $filepath.\n";
|
---|
504 | die "\n";
|
---|
505 | }
|
---|
506 | my @lines = <FILE_IN>;
|
---|
507 | close FILE_IN;
|
---|
508 |
|
---|
509 | # Initialise some my variables
|
---|
510 | my %linehash = ();
|
---|
511 |
|
---|
512 | # Process the contents of the file, line by line
|
---|
513 | for ($i = 0; $i < scalar(@lines); $i++) {
|
---|
514 | my $line = $lines[$i];
|
---|
515 | $line =~ s/(\s*)$//; # Remove any nasty whitespace, carriage returns etc.
|
---|
516 |
|
---|
517 | # Line contains a dictionary string
|
---|
518 | if ($line =~ /^((\w|\.)+):(.*)$/) {
|
---|
519 | my $key = $1;
|
---|
520 |
|
---|
521 | # Map from key to line
|
---|
522 | $linehash{$key} = $i . "-" . $i;
|
---|
523 | }
|
---|
524 | }
|
---|
525 |
|
---|
526 | return %linehash;
|
---|
527 | }
|
---|
528 |
|
---|
529 |
|
---|
530 | sub import_chunk_from_resource_bundle
|
---|
531 | {
|
---|
532 | my ($chunktext) = @_;
|
---|
533 |
|
---|
534 | # Simple: just remove string key
|
---|
535 | $chunktext =~ s/^((\w|\.)+)://;
|
---|
536 |
|
---|
537 | return $chunktext;
|
---|
538 | }
|
---|
539 |
|
---|
540 |
|
---|
541 |
|
---|
542 | # ------------------------------------------------------------------------------------------
|
---|
543 |
|
---|
544 | sub build_key_to_line_mapping_for_my_resource_bundle
|
---|
545 | {
|
---|
546 | my ($filepath) = @_;
|
---|
547 |
|
---|
548 | # Open file for reading
|
---|
549 | if (!open(FILE_IN, "<$filepath")) {
|
---|
550 | print STDERR "Error: Could not open resource bundle $filepath.\n";
|
---|
551 | die "\n";
|
---|
552 | }
|
---|
553 | my @lines = <FILE_IN>;
|
---|
554 | close FILE_IN;
|
---|
555 |
|
---|
556 | # Initialise some my variables
|
---|
557 | my %linehash = ();
|
---|
558 |
|
---|
559 | # Process the contents of the file, line by line
|
---|
560 | for ($i = 0; $i < scalar(@lines); $i++) {
|
---|
561 | my $line = $lines[$i];
|
---|
562 | $line =~ s/(\s*)$//; # Remove any nasty whitespace, carriage returns etc.
|
---|
563 |
|
---|
564 | # Line contains a dictionary string
|
---|
565 | if ($line =~ /^((\w|\.|\-)+):\"(.*)$/) {
|
---|
566 | my $key = $1;
|
---|
567 | my $text = $3;
|
---|
568 |
|
---|
569 | # While there is still text of the chunk to go...
|
---|
570 | my $startline = $i;
|
---|
571 | while ($text !~ /\"$/ || $text =~ /\\\"$/) {
|
---|
572 | $i++;
|
---|
573 | $text = $lines[$i];
|
---|
574 | }
|
---|
575 |
|
---|
576 | $linehash{$key} = $startline . "-" . $i;
|
---|
577 | }
|
---|
578 | }
|
---|
579 |
|
---|
580 | return %linehash;
|
---|
581 | }
|
---|
582 |
|
---|
583 |
|
---|
584 | sub import_chunk_from_my_resource_bundle
|
---|
585 | {
|
---|
586 | my ($chunktext) = @_;
|
---|
587 |
|
---|
588 | # Remove chunk key and quotes
|
---|
589 | $chunktext =~ s/^((\w|\.|\-)+)://;
|
---|
590 | $chunktext =~ s/^\"//;
|
---|
591 | $chunktext =~ s/\"$//;
|
---|
592 |
|
---|
593 | return $chunktext;
|
---|
594 | }
|
---|
595 |
|
---|
596 |
|
---|
597 | 1;
|
---|