oddtrans 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/perl
  2. # Based on umtrans.pl version 1.0 (April 8, 2001) by Clifford Adams.
  3. # Extracts translation strings from wiki script and extensions.
  4. use strict;
  5. use warnings;
  6. use v5.10;
  7. use utf8;
  8. binmode(STDOUT, ":encoding(UTF-8)");
  9. my $help = q{
  10. NAME
  11. oddtrans - complement translation tables for Oddmuse
  12. SYNOPSIS
  13. oddtrans [OPTIONS]... [FILE]...
  14. DESCRIPTION
  15. Read all the calls to T(), Ts(), and Tss() from all FILEs, and print
  16. them on standard output, followed by their translation (usually the
  17. empty string unless you use -l to load a library).
  18. -l
  19. load a library from a previous run; you can use multiple -l
  20. EXAMPLES
  21. oddtrans -l german-utf8.pl wiki.pl modules/*.pl > new-german-utf8.pl
  22. };
  23. our %Translate = ();
  24. my $arg = shift;
  25. while ($arg =~ /^-l/) {
  26. my $file;
  27. $file = substr($arg, 3) if length($arg) > 2;
  28. $file = shift unless $file;
  29. die $help unless -f $file;
  30. my %backup = %Translate;
  31. header_info_extract($file); # keep the header information of the translation files
  32. do $file or die "Cannot do $file";
  33. foreach my $key (keys %Translate) {
  34. $backup{$key} = $Translate{$key};
  35. }
  36. %Translate = %backup;
  37. $arg = shift;
  38. }
  39. unshift(@ARGV, $arg); # shove the last one back because it is not -l!
  40. print "our \%Translate = grep(!/^#/, split(/\\n/,<<'END_OF_TRANSLATION'));\n";
  41. undef $/; # slurp
  42. foreach my $file (@ARGV) {
  43. open(my $fh, "<:encoding(UTF-8)", $file) or die "Cannot open $file: $!";
  44. $_ = <$fh>;
  45. # join split strings
  46. s/'\s*\.\s*'//g;
  47. s/"\s*\.\s*"//g;
  48. # extract calls to T, Ts and Tss
  49. while(/Ts?s?\(\'([^']+)/g) { trans($file, $1); }
  50. while(/Ts?s?\(\"([^"]+)/g) { trans($file, $1); }
  51. }
  52. print "#\nEND_OF_TRANSLATION\n";
  53. my %seen = ();
  54. my %read = ();
  55. sub trans {
  56. my ($file, $string) = @_;
  57. my ($result);
  58. $result = '';
  59. $result = $Translate{$string} if (defined($Translate{$string}));
  60. return ' ' if ($seen{$string});
  61. marker($file) unless $read{$file};
  62. $seen{$string} = 1;
  63. print $string . "\n" . $result . "\n";
  64. return ' ';
  65. }
  66. sub marker {
  67. my $file = shift;
  68. $read{$file} = 1;
  69. # place marker
  70. print "#" x 80, "\n";
  71. print "# $file\n";
  72. print "#" x 80, "\n";
  73. }
  74. my $header = 0;
  75. sub header_info_extract {
  76. return if $header++;
  77. my $file = shift;
  78. open(FILE, "<:encoding(utf8)", $file) or die "Can't open $file because: $!";
  79. foreach (<FILE>) {
  80. last if (/^our %Translate = /);
  81. print;
  82. }
  83. close FILE;
  84. }
  85. sub AddModuleDescription {
  86. # Do nothin; this function is just there such that the translation
  87. # files can be run.
  88. }