[ic] In order to overcome a problem we had with the -m option of the
localize script
John Matecsa
interchange-users@icdevgroup.org
Mon Jan 13 20:16:01 2003
In order to overcome a problem we had with the -m option of the localize script, we did the following.
A relevant bug report is available at: http://www.icdevgroup.org/bugs/show_bug.cgi?id=479
Instructions
1. Generate a localization file using the interchange tool by:
localize -l en_US [-d fr_FR ...] page.html > translation.txt
2. Replace the default translations with your own where required
3. Feed this data into my utility
./locale-merge -i page.html -o page.html -t translation.txt
Known Bugs
If you specify a default locale that /does not/ have translations for
all keys, the output file will have blanks (you should not run into this
if you make sure the -l option on the interchange tool corresponds to
the -d option with my tool, or if you omit -d altogether)
#!/usr/bin/perl
###############################################################
# Localization Helper for Interchange
#
# Initial development by Aaron Kemp <kempa@picassofish.com>
###############################################################
use strict;
use Getopt::Std;
my $data;
my %translations;
my $default_locale = undef;
my %options;
getopts('d:t:i:o:', \%options);
if (!($options{'t'})) {
print "Interchange Localization Helper v1.0\nCopyright (C) 2002 Picassofish\n\n";
print "Usage:\n\t$0 -t <translation_file> [-i <input_file>] [-d <default_locale>] [-o <output_file>]";
print "\n\n\t-i\tSpecifies the input file (default: stdin)";
print "\n\n\t-o\tSpecifies the output file (default: stdout)";
print "\n\n\t-i\tSpecifies the translation file";
print "\n\n\t-d\tSpecifies the default locale (defaults to the\n\t\tfirst found in the translation file)\n\n";
exit;
}
# Load data from stdin or a file
if (defined($options{'i'})) {
open(INPUT, $options{'i'}) || die ("Unable to open input file '".$options{'i'}."'\n");;
$data = join('', <INPUT>);
close(INPUT);
} else {
$data = join('', <STDIN>);
}
# Load translation table
open(TRANSLATIONS, $options{'t'}) || die ("Unable to open translations file '".$options{'t'}."'\n");
my $locale = undef;
my @locale_data;
my $marker;
$default_locale = $options{'d'} if (defined($options{'d'}));
while (<TRANSLATIONS>) {
if ($locale eq undef) {
next unless m/^\s*locale\s+([a-zA-Z_]+).*<<\s*(\w+)/i;
$locale = $1;
$marker = $2;
$default_locale = $locale if ($default_locale eq undef);
@locale_data = ();
} else {
next if (m/^\s*#/);
if (m/^\s*$marker\s*$/) {
while (@locale_data) {
$translations{$locale}{pop(@locale_data)} = pop(@locale_data);
}
$locale = undef;
next;
}
chomp;
if (m/\s*\"(.*)\"/) {
push(@locale_data, $1);
} elsif (m/\s*undef\s*/) {
push(@locale_data, undef);
}
}
}
sub translate($) {
my ($key) = @_;
my $translated = (defined(${$translations{$default_locale}}{$key})?${$translations{$default_locale}}{$key}:$key);
foreach my $locale (keys %translations) {
foreach (keys %{$translations{$locale}}) {
if ($_ eq $key) {
if (defined(${$translations{$locale}}{$key})) {
if (${$translations{$locale}}{$key} ne ${$translations{$default_locale}}{$key}) {
$translated .= "[$locale]".${$translations{$locale}}{$key}."[/$locale]";
}
}
}
}
}
return ($translated);
}
# Convert [L] to [LC]
$data =~ s!\[L\]([^\[]+)\[/L\]!\[LC\]$1\[/LC\]!gm;
# Fill in [LC]
$data =~ s!\[LC.*?\]([^\[]+)\[.*?/LC\]!"\[LC\]".translate($1)."\[/LC\]"!gme;
# Output to file or stdout
if (defined($options{'o'})) {
open(OUTPUT, ">".$options{'o'});
print OUTPUT $data;
close(OUTPUT);
} else {
print $data;
}
Hopefully this will help somebody out there!
--
John Matecsa <matecsaj@picassofish.com>
Picasso Fish Corporation