Changeset 4164
- Timestamp:
- 2003-04-16T14:34:10+12:00 (21 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gsdl/bin/script/translator.pl
r4135 r4164 65 65 $targetlang =~ tr/A-Z/a-z/; 66 66 67 # If the source language is not English, it may not be up to date68 if ($sourcelang !~ m/english/) {69 print STDERR "Warning: Source language is not English. You should check that" .70 " the source language is up to date before beginning.\n";71 }72 73 67 my $gsdldir = "$ENV{'GSDLHOME'}"; 74 68 my $macrosdir = util::filename_cat($gsdldir, "macros"); 75 76 # Check that the source language macro files exist, and parse them 69 my $langdir = util::filename_cat($gsdldir, "tmp", "lang"); 70 71 # Make sure this is a continuation of a previously initialised translation process 72 $translationdir = util::filename_cat($langdir, "$sourcelang-$targetlang"); 73 if (! -e $translationdir) { 74 print STDERR "Error: Translation has not been initialised (exiting).\n"; 75 return; 76 } 77 78 # Parse the source language macro files 77 79 my $sourcedmname1 = &get_macrofile_name($sourcelang, ""); 78 80 my $sourcehash1 = &parse_macrofile($macrosdir, $sourcedmname1); … … 88 90 my $sourcehash = &combine_hashes($sourcehash1, $sourcehash2); 89 91 90 # Check if this is a new translation process or the continuation of an old one 91 $translationdir = util::filename_cat($gsdldir, "tmp", "lang", "$sourcelang-$targetlang"); 92 # Open the existing translation and update databases 92 93 $translationdb = util::filename_cat($translationdir, "translation.db"); 93 94 $updatedb = util::filename_cat($translationdir, "update.db"); 94 95 95 if (! -e $translationdir) { 96 # Create a directory to store the data for the new translation, make world-writeable 97 my $currentmask = umask; 98 umask(0000); 99 if (! mkdir($translationdir, 0777)) { 100 umask($currentmask); 101 die "Error: Couldn't create directory $translationdir.\n"; 102 } 103 umask($currentmask); 104 105 # Start a new database for the translation information 106 tie(%translationdata, "GDBM_File", $translationdb, GDBM_WRCREAT, 0640); 107 108 # Work out what the target language code should be, and store it in the database 109 my $maincfgfile = util::filename_cat($gsdldir, "etc", "main.cfg"); 110 my $languagecodes = &get_language_codes(); 111 my $targetcode = &get_language_code($targetlang, $maincfgfile, $languagecodes); 112 $translationdata{"*target_lang_code*"} = $targetcode; 113 114 # Start a new update database 115 tie(%updatedata, "GDBM_File", $updatedb, GDBM_WRCREAT, 0640); 116 } 117 else { 118 # This translation is a continuation of an old one, so open the database files for it 119 tie(%translationdata, "GDBM_File", $translationdb, 1, 0640); 120 tie(%updatedata, "GDBM_File", $updatedb, 1, 0640); 121 } 122 123 # Create the target language macro files if they don't exist 96 # This translation is a continuation of an old one, so open the database files for it 97 tie(%translationdata, "GDBM_File", $translationdb, 1, 0640); 98 tie(%updatedata, "GDBM_File", $updatedb, 1, 0640); 99 100 # Parse the target language macro files 124 101 my $targetdmname1 = &get_macrofile_name($targetlang, ""); 125 my $targetdmfile1 = util::filename_cat($macrosdir, $targetdmname1); 126 if (! -e $targetdmfile1) { 127 my $sourcedmfile1 = util::filename_cat($macrosdir, $sourcedmname1); 128 &create_empty_macrofile($sourcedmfile1, $targetdmfile1); 129 } 102 my $targethash1 = &parse_macrofile($macrosdir, $targetdmname1); 130 103 my $targetdmname2 = &get_macrofile_name($targetlang, "2"); 131 my $targetdmfile2 = util::filename_cat($macrosdir, $targetdmname2);132 if (! -e $targetdmfile2) {133 my $sourcedmfile2 = util::filename_cat($macrosdir, $sourcedmname2);134 &create_empty_macrofile($sourcedmfile2, $targetdmfile2);135 }136 137 # Parse the target language macro files138 my $targethash1 = &parse_macrofile($macrosdir, $targetdmname1);139 104 my $targethash2 = &parse_macrofile($macrosdir, $targetdmname2); 140 105 … … 170 135 $macrofilename = $macrofilename . $suffix . ".dm"; 171 136 return $macrofilename; 172 }173 174 175 sub get_language_code176 {177 my ($language, $maincfgfile, $languagecodes) = @_;178 179 # Check if the language is in the main.cfg file180 open(MAIN_CFG_IN, "<$maincfgfile") or die "Error: Could not open $maincfgfile.\n";181 my $lastlangline;182 while (<MAIN_CFG_IN>) {183 $line = $_;184 chomp($line);185 # print "Line: $line\n";186 if ($line =~ m/^Language\s+/i) {187 my @args = split(/ +/,$line);188 my ($lang_abbr) = ($args[1] =~ m/^shortname=(.*)$/);189 $lang_abbr =~ tr/A-Z/a-z/;190 my ($lang_long) = ($args[2] =~ m/^longname=(.*)$/);191 $lang_long =~ tr/A-Z/a-z/;192 193 # Is this the language we are translating into?194 if ($lang_long eq $language) {195 return $lang_abbr;196 }197 198 $lastlangline = $line;199 }200 }201 close MAIN_CFG_IN;202 203 # Try to find it using the ISO 639 language codes204 my $langcode;205 if ($languagecodes->{$language}) {206 $langcode = $languagecodes->{$language};207 }208 # Otherwise we just have to make something up209 else {210 $langcode = &make_up_language_code($language, $languagecodes);211 }212 213 # Add the new language code into the main.cfg file, and make sure it is world writeable214 my $currentmask = umask;215 umask(0000);216 if (! open(MAIN_CFG_OUT,">$maincfgfile.new")) {217 umask($currentmask);218 die "Error: Could not create $maincfgfile.new: $!\n";219 }220 umask($currentmask);221 222 open MAIN_CFG_IN, "<$maincfgfile" or die "Error: Could not open $maincfgfile.\n";223 my $inmacros = "false";224 while (<MAIN_CFG_IN>) {225 $line = $_;226 chomp($line);227 print MAIN_CFG_OUT $line;228 if ($line =~ m/^macrofiles /) {229 $inmacros = "true";230 }231 if ($inmacros eq "true" && $line !~ m/\\$/) {232 print MAIN_CFG_OUT " \\\n";233 print MAIN_CFG_OUT " $language" . ".dm $language" . "2.dm";234 $inmacros = "false";235 }236 print MAIN_CFG_OUT "\n";237 238 if ($line eq $lastlangline) {239 # Change language into title case240 my $langtitlecase = $language;241 substr($langtitlecase, 0, 1) =~ tr/a-z/A-Z/;242 print MAIN_CFG_OUT ("Language shortname=$langcode longname=$langtitlecase ",243 "default_encoding=utf-8\n");244 }245 }246 close MAIN_CFG_IN;247 close MAIN_CFG_OUT;248 249 # Delete the old main.cfg and replace it with the new one250 unlink($maincfgfile);251 rename("$maincfgfile.new", $maincfgfile);252 253 return $langcode;254 }255 256 257 sub make_up_language_code258 {259 my ($language, $languagecodes) = @_;260 261 # !! TO FINISH !!262 print STDERR "Making up language code...\n";263 $langcode = $& if ($language =~ m/\w\w/);264 $language =~ s/\A.//;265 266 return $langcode;267 }268 269 270 sub create_empty_macrofile271 {272 my ($sourcedmfile, $targetdmfile) = @_;273 274 open(SOURCE_DM_FILE_IN, "<$sourcedmfile") or die "Error: Could not open file.\n";275 open(TARGET_DM_FILE_OUT, ">$targetdmfile") or die "Error: Could not write file.\n";276 277 # Reads in contents of macro file, line by line278 while (<SOURCE_DM_FILE_IN>) {279 # Check if a new package is being defined280 if (s/^package //) {281 $packagename = $_;282 chomp($packagename);283 284 # It is, so write an empty package section to the target macro file285 print TARGET_DM_FILE_OUT "package $packagename\n";286 print TARGET_DM_FILE_OUT "# text macros\n";287 print TARGET_DM_FILE_OUT "# icons\n";288 }289 }290 291 close SOURCE_DM_FILE_IN;292 close TARGET_DM_FILE_OUT;293 137 } 294 138 … … 403 247 if ($translationdata{$sourcekey}) { 404 248 if ($translationdata{$sourcekey} ne $sourcehash->{$sourcekey}) { 405 # print "Source macro $sourcekey has changed value.\n";406 249 my $targetkey = &get_macroname_equivalent($sourcelang, $targetlang, 407 250 $sourcekey); … … 416 259 } 417 260 418 # Clear out the database261 # Clear out the translation database 419 262 my $targetcode = $translationdata{"*target_lang_code*"}; 420 263 untie %translationdata; … … 439 282 my $targetkey = &get_macroname_equivalent($sourcelang, $targetlang, $sourcekey); 440 283 if (!$translationdata{$targetkey}) { 441 # print "Macro $sourcekey needs translating.\n";442 # print "Text to translate: $sourcehash->{$sourcekey}\n";443 284 $needstranslating{$sourcekey}[0] = $sourcehash->{$sourcekey}; 444 285 $needstranslating{$sourcekey}[1] = ""; … … 446 287 } 447 288 448 # ...and those in the listof macros to update289 # ...and those in the database of macros to update 449 290 foreach $targetkey (sort keys(%updatedata)) { 450 291 my $sourcekey = &get_macroname_equivalent($targetlang, $sourcelang, $targetkey); … … 567 408 my $text = $formhash->{$key}[0]; 568 409 my $default = $formhash->{$key}[1]; 410 # print "Text: $text Default: $default\n"; 569 411 570 412 # whole lot of formatting on strings … … 592 434 $default =~ s/(\n\Z)//; 593 435 594 # $keynamecount = 1;595 596 436 #tells whether is a core or auxiliary macro 597 437 my $priority = &core_or_auxiliary_macro($sourcelang, $keyname); … … 616 456 617 457 print $fh ("<table cellpadding=\"10\">\n<tr>", 618 "<td><strong><center>". uc ($sourcelang) ."</center></strong></td>\n",619 "<td><strong><center>". uc ($targetlang) ."</center></strong></td>\n",458 "<td><strong><center>". uc ($sourcelang) . "</center></strong></td>\n", 459 "<td><strong><center>". uc ($targetlang) . "</center></strong></td>\n", 620 460 "</tr>\n"); 621 461 } … … 626 466 print $fh ("</table><br></center><img src=\"_httpimg_/auxiliary.gif\">", 627 467 "<table cellpadding=\"10\">\n<tr>", 628 "<td><strong><center>". uc ($sourcelang) ."</strong></center></td>\n",629 "<td><strong><center>". uc ($targetlang) ."</strong></center></td>\n",468 "<td><strong><center>". uc ($sourcelang) . "</strong></center></td>\n", 469 "<td><strong><center>". uc ($targetlang) . "</strong></center></td>\n", 630 470 "</tr>\n"); 631 471 $first = "false"; … … 641 481 $icontext =~ s/"(.*)"/$1/; 642 482 # print "Text to translate: $icontext\n"; 483 484 if ($default =~ m/\#\# (^|.)*/) { 485 if ($& =~ m/".*"/) { 486 $default = $&; 487 $default =~ s/"(.*)"/$1/; 488 } 489 } 490 # print "Default: $default\n"; 643 491 644 492 my $remainder = $text; … … 658 506 } 659 507 print $fh ("<tr><td align=center>", $textbox1, "</td><td align=center>"); 660 # $query->hidden(-name=>"$keyname" . "$keynamecount",-default=>"$`"));661 # $keynamecount++;662 508 663 509 my $textbox2; … … 675 521 -default=>"$remainder"); 676 522 print $fh "</td></tr>\n"; 677 678 # $keynamecount++;679 # print $fh ($query->hidden(-name=>"$keyname" . "$keynamecount",-default=>"$'"),680 # "</td></tr>\n");681 523 } 682 524 } … … 746 588 747 589 748 sub source_or_target_macro749 {750 my ($sourcelang, $targetlang, $key) = @_;751 752 my $macrofile = $key;753 $macrofile =~ s/^([^:]+)::(.*)/$1/;754 755 if ($macrofile eq ($sourcelang . ".dm") || $macrofile eq ($sourcelang . "2.dm")) {756 return "1"; # Source macro757 }758 else {759 return "2"; # Target macro760 }761 }762 763 764 590 sub core_or_auxiliary_macro 765 591 { … … 790 616 791 617 &main(@ARGV); 792 793 794 sub get_language_codes795 {796 %languagecodes = (797 'abkhazian', 'ab',798 'afar', 'aa',799 'afrikaans', 'af',800 'albanian', 'sq',801 'amharic', 'am',802 'arabic', 'ar',803 'armenian', 'hy',804 'assamese', 'as',805 'aymara', 'ay',806 'azerbaijani', 'az',807 'bashkir', 'ba',808 'basque', 'eu',809 'bengali', 'bn',810 'bangla', 'bn',811 'bhutani', 'dz',812 'bihari', 'bh',813 'bislama', 'bi',814 'breton', 'br',815 'bulgarian', 'bg',816 'burmese', 'my',817 'byelorussian', 'be',818 'belarusian', 'be',819 'cambodian', 'km',820 'catalan', 'ca',821 'chinese', 'zh',822 'corsican', 'co',823 'croatian', 'hr',824 'czech', 'cs',825 'danish', 'da',826 'dutch', 'nl',827 'english', 'en',828 'esperanto', 'eo',829 'estonian', 'et',830 'faeroese', 'fo',831 'farsi', 'fa',832 'fiji', 'fj',833 'finnish', 'fi',834 'french', 'fr',835 'frisian', 'fy',836 'galician', 'gl',837 'gaelic (scottish)', 'gd',838 'gaelic (manx)', 'gv',839 'georgian', 'ka',840 'german', 'de',841 'greek', 'el',842 'greenlandic', 'kl',843 'guarani', 'gn',844 'gujarati', 'gu',845 'hausa', 'ha',846 'hebrew', 'iw',847 'hindi', 'hi',848 'hungarian', 'hu',849 'icelandic', 'is',850 'indonesian', 'id', # Should be 'in' for backward compatibility851 'interlingua', 'ia',852 'interlingue', 'ie',853 'inuktitut', 'iu',854 'inupiak', 'ik',855 'irish', 'ga',856 'italian', 'it',857 'japanese', 'ja',858 'javanese', 'jv',859 'kannada', 'kn',860 'kashmiri', 'ks',861 'kazakh', 'kk',862 'kinyarwanda', 'rw',863 'ruanda', 'rw',864 'kirghiz', 'ky',865 'kirundi', 'rn',866 'rundi', 'rn',867 'korean', 'ko',868 'kurdish', 'ku',869 'laothian', 'lo',870 'latin', 'la',871 'latvian', 'lv',872 'lettish', 'lv',873 'limburgish', 'li',874 'limburger', 'li',875 'lingala', 'ln',876 'lithuanian', 'lt',877 'macedonian', 'mk',878 'malagasy', 'mg',879 'malay', 'ms',880 'malayalam', 'ml',881 'maltese', 'mt',882 'maori', 'mi',883 'marathi', 'mr',884 'moldavian', 'mo',885 'mongolian', 'mn',886 'nauru', 'na',887 'nepali', 'ne',888 'norwegian', 'no',889 'occitan', 'oc',890 'oriya', 'or',891 'oromo', 'om',892 'afan', 'om',893 'galla', 'om',894 'pashto', 'ps',895 'pushto', 'ps',896 'polish', 'pl',897 'portuguese', 'pt',898 'punjabi', 'pa',899 'quechua', 'qu',900 'rhaeto-romance', 'rm',901 'romanian', 'ro',902 'russian', 'ru',903 'samoan', 'sm',904 'sangro', 'sg',905 'sanskrit', 'sa',906 'serbian', 'sr',907 'serbo-croatian', 'sh',908 'sesotho', 'st',909 'setswana', 'tn',910 'shona', 'sn',911 'sindhi', 'sd',912 'sinhalese', 'si',913 'siswati', 'ss',914 'slovak', 'sk',915 'slovenian', 'sl',916 'somali', 'so',917 'spanish', 'es',918 'sundanese', 'su',919 'swahili', 'sw',920 'kiswahili', 'sw',921 'swedish', 'sv',922 'tagalog', 'tl',923 'tajik', 'tg',924 'tamil', 'ta',925 'tatar', 'tt',926 'telugu', 'te',927 'thai', 'th',928 'tibetan', 'bo',929 'tigrinya', 'ti',930 'tonga', 'to',931 'tsonga', 'ts',932 'turkish', 'tr',933 'turkmen', 'tk',934 'twi', 'tw',935 'uighur', 'ug',936 'ukrainian', 'uk',937 'urdu', 'ur',938 'uzbek', 'uz',939 'vietnamese', 'vi',940 'volapÃŒk', 'vo',941 'welsh', 'cy',942 'wolof', 'wo',943 'xhosa', 'xh',944 'yiddish', 'ji',945 'yoruba', 'yo',946 'zulu', 'zu'947 );948 949 return (\%languagecodes);950 }
Note:
See TracChangeset
for help on using the changeset viewer.