Files
oddmuse/stuff/oddtrans

108 lines
2.5 KiB
Plaintext
Raw Normal View History

2004-06-28 21:04:42 +00:00
#!/usr/bin/perl
# Based on umtrans.pl version 1.0 (April 8, 2001) by Clifford Adams.
# Extracts translation strings from wiki script and extensions.
use strict;
use warnings;
use v5.10;
use utf8;
binmode(STDOUT, ":encoding(UTF-8)");
my $help = q{
2004-06-28 21:04:42 +00:00
NAME
2004-06-28 22:07:13 +00:00
oddtrans - complement translation tables for Oddmuse
2004-06-28 21:04:42 +00:00
SYNOPSIS
2004-06-28 22:07:13 +00:00
oddtrans [OPTIONS]... [FILE]...
2004-06-28 21:04:42 +00:00
DESCRIPTION
2004-06-28 22:07:13 +00:00
Read all the calls to T(), Ts(), and Tss() from all FILEs, and print
them on standard output, followed by their translation (usually the
empty string unless you use -l to load a library).
2004-06-28 21:04:42 +00:00
2004-06-28 22:07:13 +00:00
-l
2005-03-06 16:29:44 +00:00
load a library from a previous run; you can use multiple -l
2004-06-28 21:04:42 +00:00
EXAMPLES
oddtrans -l german-utf8.pl wiki.pl modules/*.pl > new-german-utf8.pl
2005-03-06 16:29:44 +00:00
};
our %Translate = ();
2005-03-06 16:29:44 +00:00
my $arg = shift;
2005-03-06 16:29:44 +00:00
while ($arg =~ /^-l/) {
my $file;
$file = substr($arg, 3) if length($arg) > 2;
2005-03-06 16:29:44 +00:00
$file = shift unless $file;
die $help unless -f $file;
my %backup = %Translate;
header_info_extract($file); # keep the header information of the translation files
2005-10-07 23:21:14 +00:00
do $file or die "Cannot do $file";
foreach my $key (keys %Translate) {
2005-03-06 16:29:44 +00:00
$backup{$key} = $Translate{$key};
}
%Translate = %backup;
$arg = shift;
2004-06-28 21:04:42 +00:00
}
unshift(@ARGV, $arg); # shove the last one back because it is not -l!
2004-06-28 21:04:42 +00:00
print "our \%Translate = grep(!/^#/, split(/\\n/,<<'END_OF_TRANSLATION'));\n";
undef $/; # slurp
foreach my $file (@ARGV) {
open(my $fh, "<:encoding(UTF-8)", $file) or die "Cannot open $file: $!";
$_ = <$fh>;
# join split strings
s/'\s*\.\s*'//g;
s/"\s*\.\s*"//g;
# extract calls to T, Ts and Tss
while(/Ts?s?\(\'([^']+)/g) { trans($file, $1); }
while(/Ts?s?\(\"([^"]+)/g) { trans($file, $1); }
2014-08-24 08:44:31 +02:00
}
print "#\nEND_OF_TRANSLATION\n";
2014-08-24 08:44:31 +02:00
2004-06-28 22:07:13 +00:00
my %seen = ();
my %read = ();
2004-06-28 22:07:13 +00:00
2004-06-28 21:04:42 +00:00
sub trans {
my ($file, $string) = @_;
2004-06-28 21:04:42 +00:00
my ($result);
$result = '';
$result = $Translate{$string} if (defined($Translate{$string}));
return ' ' if ($seen{$string});
marker($file) unless $read{$file};
2004-06-28 21:04:42 +00:00
$seen{$string} = 1;
print $string . "\n" . $result . "\n";
return ' ';
}
sub marker {
my $file = shift;
$read{$file} = 1;
# place marker
print "#" x 80, "\n";
print "# $file\n";
print "#" x 80, "\n";
}
2014-08-24 08:44:31 +02:00
my $header = 0;
2004-06-28 21:04:42 +00:00
sub header_info_extract {
2014-08-24 08:44:31 +02:00
return if $header++;
my $file = shift;
open(FILE, "<:encoding(utf8)", $file) or die "Can't open $file because: $!";
2014-08-24 08:44:31 +02:00
foreach (<FILE>) {
last if (/^our %Translate = /);
2014-08-24 08:44:31 +02:00
print;
}
close FILE;
}
sub AddModuleDescription {
# Do nothin; this function is just there such that the translation
# files can be run.
}