| 1 | package App::Bernard::Magic::Gnome; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use LWP::UserAgent; | ||
| 7 | use File::Slurp; | ||
| 8 | |||
| 9 | use App::Bernard::Magic::Single; | ||
| 10 | |||
| 11 | sub new { | ||
| 12 | return bless {}; | ||
| 13 | } | ||
| 14 | |||
| 15 | sub fetch { | ||
| 16 | my ($self, $url, $target) = @_; | ||
| 17 | |||
| 18 | my $ua = LWP::UserAgent->new; | ||
| 19 | |||
| 20 | my $req = HTTP::Request->new(GET => $url); | ||
| 21 | my $res = $ua->request($req); | ||
| 22 | |||
| 23 | die "HTTP error: " . $res->status_line. "\n" | ||
| 24 | unless $res->is_success; | ||
| 25 | |||
| 26 | write_file ($target, $res->content); | ||
| 27 | } | ||
| 28 | |||
| 29 | sub process_file { | ||
| 30 | my ($self, $filename, $settings) = @_; | ||
| 31 | |||
| 32 | print "Processing file: $filename\n"; | ||
| 33 | |||
| 34 | # Can't rely on file headers to work out | ||
| 35 | # the package name (at the moment). | ||
| 36 | |||
| 37 | my @elements = split(m!/!, $filename); | ||
| 38 | |||
| 39 | pop @elements; # lose "en@shaw.po" | ||
| 40 | my $po_type = pop @elements; # usually "po", but could be "po-properties" | ||
| 41 | my $pkg = pop @elements; | ||
| 42 | |||
| 43 | my $extra = ''; | ||
| 44 | # Special case for GTK+: | ||
| 45 | $extra .= '-properties' if $po_type eq 'po-properties'; | ||
| 46 | |||
| 47 | my $pot_url = "http://l10n.gnome.org/POT/$pkg.master/$pkg$extra.master.pot"; | ||
| 48 | my $pot_local = $filename . rand() . '.pot'; | ||
| 49 | |||
| 50 | print "Fetch: $pot_url\n"; | ||
| 51 | $self->fetch($pot_url, $pot_local); | ||
| 52 | |||
| 53 | print "Merging: "; | ||
| 54 | system("msgmerge -U $filename $pot_local"); | ||
| 55 | |||
| 56 | unlink $pot_local; | ||
| 57 | |||
| 58 | print "Transliterating: "; | ||
| 59 | |||
| 60 | $settings->{'inplace'} = 1; | ||
| 61 | $settings->{'check'} = 1; | ||
| 62 | $settings->{'input'} = $filename; | ||
| 63 | $settings->{'output'} = $filename.rand().'.po'; | ||
| 64 | |||
| 65 | my $single = App::Bernard::Magic::Single->new(); | ||
| 66 | $single->handle($settings); | ||
| 67 | |||
| 68 | print "done.\n"; | ||
| 69 | } | ||
| 70 | |||
| 71 | sub handle { | ||
| 72 | |||
| 73 | my ($self, $settings) = @_; | ||
| 74 | |||
| 75 | # Make sure "GNOME" is spelt correctly | ||
| 76 | $settings->{'defines'}->{'gnome'} = | ||
| 77 | chr(0xB7).chr(0x1045C).chr(0x1046F).chr(0x10474).chr(0x10465) | ||
| 78 | unless defined $settings->{'defines'}->{'gnome'}; | ||
| 79 | |||
| 80 | # Is this a directory, or a single file? | ||
| 81 | |||
| 82 | if (-d $settings->{'input'}) { | ||
| 83 | |||
| 84 | # A directory. | ||
| 85 | |||
| 86 | for my $pkg (glob($settings->{'input'}.'/*')) { | ||
| 87 | |||
| 88 | for my $suffix ('/po/en@shaw.po', | ||
| 89 | '/po-properties/en@shaw.po') { | ||
| 90 | if (-e $pkg.$suffix) { | ||
| 91 | $self->process_file( | ||
| 92 | $pkg.$suffix, | ||
| 93 | $settings); | ||
| 94 | } | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | } else { | ||
| 99 | |||
| 100 | # Just one file. | ||
| 101 | |||
| 102 | $self->process_file( | ||
| 103 | $settings->{'input'}, | ||
| 104 | $settings); | ||
| 105 | } | ||
| 106 | } | ||
| 107 | |||
| 108 | 1; |