update-desktop-files/translate.pl

166 lines
3.2 KiB
Perl

$LANGUAGE=$ARGV[0];
$FILE=$ARGV[1];
use File::Basename;
$LANGUAGE =~ s,/*$,,;
$lang = basename($LANGUAGE);
$lang =~ s/translations_//;
$file = basename($FILE);
my %origs;
my %trans;
my $group = '';
my %added = ();
opendir(DOMS, "$LANGUAGE/en_US/LC_MESSAGES/") || die "opendir $LANGUAGE";
@mos = grep { /\.mo$/ } readdir(DOMS);
close DOMS;
sub flush_translation()
{
my $list = $added{$group};
foreach $tag ('Name', 'Comment', 'GenericName')
{
next if (!defined $trans{$tag});
$list = "$tag,$lang;$list";
}
if ($list) {
print "X-SuSE-added=$list\n";
}
foreach $tag ('Name', 'Comment', 'GenericName')
{
next if (!defined $trans{$tag});
#print "#SUSE-Addition\n";
print "$tag\[$lang\]=$trans{$tag}\n";
print STDERR "adding '$tag\[$lang\]=$trans{$tag}' to $file\n";
delete $trans{$tag};
}
}
my $flagged = 0;
open(FILE, "$FILE");
while ( <FILE> ) {
# new group
if ($_ =~ /^\[/ )
{
my $line = $_;
chomp $line;
$group = $line;
}
if ($_ =~ /^X-SuSE-added=/) {
if (defined $added{$group}) {
print STDERR "X-SuSE-added twice in $group\n";
exit(1);
}
chomp $_;
$_ =~ s,^X-SuSE-added=,,;
$added{$group} = $_;
next;
}
}
close(FILE);
open(FILE, "$FILE");
$group = '';
while ( <FILE> )
{
$flagged = 1 if ($_ =~ /X-SuSE-translate=true/ && $group eq '[Desktop Entry]');
# new group
if ($_ =~ /^\[/ )
{
flush_translation();
my $line = $_;
chomp $line;
if (!$flagged && $group eq '[Desktop Entry]') {
print "X-SuSE-translate=true\n";
$flagged = 1;
}
$group = $line;
%origs = ();
%trans = ();
print $_;
next;
}
foreach $tag ('Name', 'Comment', 'GenericName')
{
if ($_ =~ m/^$tag=/ ) {
$text = $_;
chomp($text);
$text =~ s/$tag=//;
$origs{$tag} = $text;
$text = "$tag($file): $text";
foreach $domain (@mos)
{
$domain=basename($domain, ".mo");
$ENV{"TEXTDOMAINDIR"} = $LANGUAGE;
$ENV{"LC_ALL"} = "en_US.UTF-8";
open(GETTEXT, "-|", "gettext", "-d", "$domain", "$text");
$transe=<GETTEXT>;
close(GETTEXT);
next if ($transe eq $text);
next if ($transe eq $origs{$tag});
$trans{$tag} = $transe;
}
next;
}
if ( $_ =~ /^$tag\[$lang\]/)
{
if (! defined $origs{$tag}) {
print STDERR "ERROR: $tag is translated before original. Not supported - patch desktop file $FILE\n";
exit(1);
}
$text = $_;
chomp($text);
$text =~ s/^$tag\[$lang\]=//;
# no reason to act
$ourtrans = $origs{$tag};
$ourtrans = $trans{$tag} if (defined $trans{$tag});
if ($text eq $ourtrans)
{
delete $trans{$tag};
#print "# SUSE Copy\n";
next;
}
print "#SUSE-Overwrite $_";
# if not defined, we prefer the orig
if (defined $trans{$tag})
{
print STDERR "adding '$tag\[$lang\]=$ourtrans' to $file\n";
$_ = "$tag\[$lang\]=$ourtrans\n";
delete $trans{$tag};
} else {
print STDERR "removing '$tag\[$lang\]=$text' from $file\n";
$_ = '';
}
next;
}
}
next if ($_ =~ /^X-SuSE-added=/);
print $_ if ($_);
}
flush_translation();
if (!$flagged) {
print "X-SuSE-translate=true\n";
}