translate_resources.pl
上传用户:center1979
上传日期:2022-07-26
资源大小:50633k
文件大小:4k
源码类别:

OpenGL

开发平台:

Visual C++

  1. #!/usr/bin/perl
  2. ################################################################
  3. #           Utility for i18n on Win32
  4. #
  5. #   Author: Christophe Teyssier <chris@teyssier.org>
  6. #   date: July 2006
  7. #
  8. # - Loads translations from the po files and produces translated .rc files in src/celestia/res
  9. # - Produces a dll with the translated resources for each po file in the locale/ dir
  10. # - Produces a list of unicode codepoints for each language in the current dir 
  11. #   (to generate txf font textures)
  12. # - Compiles .po files and installs catalogs under locale/
  13. #
  14. # Requirements:
  15. # - rc.exe link.exe and msgfmt.exe must be in the PATH
  16. ################################################################
  17. use Encode;
  18. use File::Basename;
  19. my $dir = dirname $0;
  20. my $resource_file = "$dir/../src/celestia/res/celestia.rc";
  21. my $output_dir = "$dir/../src/celestia/res/";
  22. opendir(DIR, $dir);
  23. my @po_files = sort (grep( /.po$/, readdir(DIR) ));
  24. closedir DIR;
  25. open RES, "< $resource_file";
  26. my $resource;
  27.     local $/;
  28.     $resource = <RES>;
  29. }
  30. close RES;
  31. # LangID and code page
  32. # http://msdn.microsoft.com/library/default.asp?url=/library/en-us/intl/nls_238z.asp
  33. my %lang = (
  34. ar => [ '401', 1256 ],
  35. bg => [ '402', 1251 ],
  36. de => [ '407', 1252 ],
  37. el => [ '408', 1253 ],
  38. en => [ '409', 1252 ],
  39. es => [ '40a', 1252 ],
  40. fr => [ '40c', 1252 ],
  41. hu => [ '40e', 1250 ],
  42. it => [ '410', 1252 ],
  43. ja => [ '411',  932 ],
  44. ko => [ '412',  949 ],
  45. lt => [ '427',  1257 ],
  46. lv => [ '426', 1257 ],
  47. nl => [ '413', 1252 ],
  48. pl => [ '415', 1250 ],
  49. pt_BR => [ '416', 1252 ],
  50. pt => [ '816', 1252 ],
  51. ro =>  [ '418', 1250 ],
  52. ru =>  [ '419', 1251 ],
  53. sv => [ '41d', 1252 ],
  54. uk => [ '422', 1251 ],
  55. zh_CN => [ '804', 936 ],
  56. );
  57. mkdir "$dir\..\locale" if ! -d "$dir\..\locale";
  58. my %codepoints; # hash holds unicode codepoints used by language
  59. foreach my $po (@po_files) {
  60.     my $strings = load_po_strings("$dir\$po");
  61.     my $res = $resource;
  62.     my $lang = basename $po;
  63.     $lang =~ s/..*//o;
  64.     mkdir "$dir\..\locale\$lang" if ! -d "$dir\..\locale\$lang";
  65.     mkdir "$dir\..\locale\$lang\LC_MESSAGES" if ! -d "$dir\..\locale\$lang\LC_MESSAGES";
  66.     while (my ($k, $v) = each %$strings) {
  67.         map { $codepoints{$lang}{$_} = 1; } map { sprintf '%04X', ord($_); } split //, Encode::decode_utf8($v);
  68.         Encode::from_to($v, 'UTF-8', "CP$lang{$lang}[1]");
  69.         map { $c{ord($_)} = 1; } split //, Encode::decode("UTF-8", $v);
  70.         $res =~ s/"Q$kE"/"$v"/g;
  71.     }
  72.     $res=~ s/Q#pragma code_page(1252)E/#pragma code_page($lang{$lang}[1])/;
  73.     open OUT, "> $output_dir/celestia_$lang.rc";
  74.     print OUT $res;
  75.     close OUT;
  76.     system qq{rc /l $lang{$lang}[0] /d "NDEBUG" /fo $dir\..\src\celestia\celestia_$lang.res /i "$dir\..\src\celestia\res" $dir\..\src\celestia\res\celestia_$lang.rc};
  77.     system qq{link /nologo /noentry /dll /machine:I386 /out:$dir\..\locale\res_$lang.dll $dir\..\src\celestia\celestia_$lang.res};
  78.     system qq{msgfmt $dir\$lang.po -o $dir\..\locale\$lang\LC_MESSAGES\celestia.mo};
  79. }
  80. opendir(DIR, "$dir\..\po2");
  81. my @po_files = sort (grep( /.po$/, readdir(DIR) ));
  82. closedir DIR;
  83. foreach my $po (@po_files) {
  84.     my $lang = basename $po;
  85.     $lang =~ s/..*//o;
  86.     my $strings = load_po_strings("$dir\..\po2\$po");
  87.     while (my ($k, $v) = each %$strings) {
  88.         map { $codepoints{$lang}{$_} = 1; } map { sprintf '%04X', ord($_); } split //, Encode::decode_utf8($v);
  89.     }
  90.     mkdir "$dir\..\locale\$lang" if ! -d "$dir\..\locale\$lang";
  91.     mkdir "$dir\..\locale\$lang\LC_MESSAGES" if ! -d "$dir\..\locale\$lang\LC_MESSAGES";
  92.     system qq{msgfmt $dir\..\po2\$lang.po -o $dir\..\locale\$lang\LC_MESSAGES\celestia_constellations.mo};
  93. }
  94. foreach my $lang (keys %codepoints) {
  95.     # list of unicode codepoints to generate font textures
  96.     my $chr;
  97.     map { $chr .= "$_ n"; } sort keys %{$codepoints{$lang}};
  98.     open CHR, "> $dir/codepoints_$lang.txt";
  99.     print CHR $chr;
  100.     close CHR;
  101. }
  102. sub load_po_strings {
  103.     my $po = shift;
  104.     my %strings;
  105.     open PO, "< $po";
  106.     my $l1;
  107.     my $l2;
  108.     my $l3;
  109.     while ($l3 = <PO>) {
  110.         # The po file is read by groups of three lines.
  111.         # we can safely ignore multiline msgids since resource files
  112.         # use only single line strings
  113.         if ($l2 =~ /^msgids+"(.*)"/) {
  114.             my $msgid = $1;
  115.             if ($l3 =~ /^msgstrs+"(.+)"/ && $l1 !~ /fuzzy/ ) {
  116.                 $strings{$msgid} = $1;
  117.             }
  118.         }
  119.         $l1 = $l2;
  120.         $l2 = $l3;
  121.     }
  122.     close PO;
  123.     return %strings;
  124. }