#!/usr/bin/perl -w my @locales; my %locale_to_domain_to_key_to_value = (); my %key_to_domain_to_locale = (); my %value_to_key_domain_locale = (); sub getlocales { my @localedirs = ; foreach my $localedir (@localedirs) { $localedir =~ m!/usr/share/locale/(.*)/LC_MESSAGES!; my $locale = $1; $locale_to_domain_to_key_to_value{$locale} = (); push @locales, $locale; my %localespace = (); foreach my $mo (<$localedir/*.mo>) { $mo =~ m!.*/(.*?)\.mo!; my $domain = $1; $locale_to_domain_to_key_to_value{$locale}{$domain} = (); my %domain = (); my $text = `msgunfmt $mo 2> /dev/null`; my @lines = split /\n/, $text; my $line = shift @lines; do { while (defined $line && $line eq '') { $line = shift @lines; } last unless defined $line; next unless $line =~ /msgid "(.*)"/; my $key = $1; $line = shift @lines; $line =~ /msgstr "(.*)"$/; $value = $1; while (($line = shift @lines) && ($line =~ /^"(.*)"$/)) { $value .= $1; } $domain{$key} = $value; $key_to_domain_to_locale{$key} = () unless defined $key_to_domain_to_locale{$key}; $key_to_domain_to_locale{$key}{$domain} = () unless defined $key_to_domain_to_locale{$key}{$domain}; $key_to_domain_to_locale{$key}{$domain}{$locale} = $value; $locale_to_domain_to_key_to_value{$locale}{$domain}{$key} = $value; $value_to_key_domain_locale{$value} = () unless defined $value_to_key_domain_locale{$value}; $value_to_key_domain_locale{$value}{"$key$domain$locale"} = [$key, $domain, $locale]; } while (1); } } if (0) { for my $a (keys %locale_to_domain_to_key_to_value) { print "$a => {\n"; next unless defined $locale_to_domain_to_key_to_value{$a}; my %domain_to_key_to_value = %{$locale_to_domain_to_key_to_value{$a}}; for my $b (keys %domain_to_key_to_value) { print " $b => {\n"; my %key_to_value = %{$domain_to_key_to_value{$b}}; for my $c (keys %key_to_value) { print " $c => ".$key_to_value{$c}."\n"; } print " }\n"; } print "}\n"; } } } sub zquote { my $line = ''; foreach my $str (@_) { $str =~ s/([\\"])/\\$1/g; $line .= qq! "$str"!; } return $line; } getlocales(); sub getinput { my ($title, $prompt) = @_; my $input=`zenity --entry --title="$title" --text="$prompt"`; chomp $input; return $input; } sub warning { my ($message) = @_; `zenity --warning --text="$message"`; } sub choice { my ($title, $prompt, @pairs) = @_; my $options = ''; while ((scalar @pairs) > 1) { $key = shift @pairs; $value = shift @pairs; $options .= zquote($key, $value); } my $action=`zenity --list --title="$title" --text= --hide-column=1 --column= --column="$prompt" $options`; chomp $action; return $action; } sub lookup { my $value = getinput("Retrieve identifier", "Enter user interface string"); return unless $value ne ''; unless (defined $value_to_key_domain_locale{$value}) { warning("No matching string"); return; } my @tripples = (); my $id = 0; my $options = ''; foreach $key (keys %{$value_to_key_domain_locale{$value}}) { my @tripple = @{$value_to_key_domain_locale{$value}{$key}}; push @tripples, ++$id, $tripple[2], $tripple[1], $tripple[0]; $options .= zquote($id, $tripple[2], $tripple[1], $tripple[0]); } my $row = `zenity --list --title="Matching identifiers" --text="for $value" --hide-column=1 --column= --column=Locale --column=Domain --column=ID $options`; } sub view { my $key = getinput("Review identifier translations", "Enter identifier"); return unless $value ne ''; unless (defined $key_to_domain_to_locale{$key}) { warning("No such identifier"); return; } my @tripples = (); my $id = 0; my $options = ''; foreach $domain (keys %{$key_to_domain_to_locale{$key}}) { foreach $locale (keys %{$key_to_domain_to_locale{$key}{$domain}}) { my $value = $key_to_domain_to_locale{$key}{$domain}{$locale}; push @tripples, ++$id, $locale, $domain, $value; $options .= zquote($id, $locale, $domain, $value); } } my $row = `zenity --list --title="Matching strings" --text="for $key" --hide-column=1 --column= --column=Locale --column=Domain --column=Value $options`; } sub browse { my $loptions = ''; for my $locale (sort keys %locale_to_domain_to_key_to_value) { $loptions .= zquote($locale); } my $locale; while (($locale = `zenity --list --title="Select locale" --text= --column=Locale $loptions`) ne '') { chomp $locale; my %domain_to_key_to_value = %{$locale_to_domain_to_key_to_value{$locale}}; my $doptions = ''; for my $domain (keys %domain_to_key_to_value) { $doptions .= zquote($domain); } my $domain; while (($domain = `zenity --list --title="Select domain" --text="Locale ($locale)" --column=Domain $doptions`) ne '') { chomp $domain; my %key_to_value = %{$domain_to_key_to_value{$domain}}; my $koptions = ''; my @key_values = (); for my $key (keys %key_to_value) { my $value = $key_to_value{$key}; push @key_values, $key, $value; $koptions .= zquote($key, $value); } `zenity --list --title="Domain: $domain" --text="Locale: $locale" --column=ID --column=Value $koptions`; } } } sub main { my $action; do { $action=choice("Translator tool", "What would you like to do?", "lookup", "Lookup identifier", "read", "View identifier translations", "browse", "Browse translations"); for ($action) { /^lookup$/ && lookup(); /^read$/ && view(); /^browse$/ && browse(); } } while ($action ne ''); } main();