| 1 | #!/usr/bin/perl -w |
|---|
| 2 | |
|---|
| 3 | # Movable Type (r) Open Source (C) 2001-2009 Six Apart, Ltd. |
|---|
| 4 | # This program is distributed under the terms of the |
|---|
| 5 | # GNU General Public License, version 2. |
|---|
| 6 | # |
|---|
| 7 | # $Id$ |
|---|
| 8 | |
|---|
| 9 | use strict; |
|---|
| 10 | |
|---|
| 11 | if (grep { /--help/ } @ARGV) { |
|---|
| 12 | print <<USAGE; |
|---|
| 13 | Mode d\'emploi: |
|---|
| 14 | |
|---|
| 15 | make-l10n <root directory> |
|---|
| 16 | |
|---|
| 17 | <root directory> defaults to typepad. |
|---|
| 18 | USAGE |
|---|
| 19 | exit(1); |
|---|
| 20 | } |
|---|
| 21 | |
|---|
| 22 | my $wc = 0; |
|---|
| 23 | my $root_dir = ($ARGV[0] || 'typepad'); |
|---|
| 24 | my $languagename = ($ARGV[1] || 'fr'); |
|---|
| 25 | |
|---|
| 26 | my $lib_dir = $root_dir . '/lib'; |
|---|
| 27 | |
|---|
| 28 | use lib './lib'; |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | print <<"PACKAGENAME"; |
|---|
| 32 | package MT::L10N::$languagename; |
|---|
| 33 | PACKAGENAME |
|---|
| 34 | |
|---|
| 35 | my $yr = (localtime(time))[5] + 1900; |
|---|
| 36 | |
|---|
| 37 | my $parent_class = $languagename eq 'en_us' ? 'MT::L10N' : 'MT::L10N::en_us'; |
|---|
| 38 | print <<"HEADER"; |
|---|
| 39 | # Copyright 2001-$yr Six Apart. This code cannot be redistributed without |
|---|
| 40 | # permission from www.sixapart.com. For more information, consult your |
|---|
| 41 | # Movable Type license. |
|---|
| 42 | # |
|---|
| 43 | # \$Id\$ |
|---|
| 44 | |
|---|
| 45 | use strict; |
|---|
| 46 | use $parent_class; |
|---|
| 47 | use vars qw( \@ISA \%Lexicon ); |
|---|
| 48 | \@ISA = qw( $parent_class ); |
|---|
| 49 | |
|---|
| 50 | sub encoding { 'utf-8' } |
|---|
| 51 | |
|---|
| 52 | ## The following is the translation table. |
|---|
| 53 | |
|---|
| 54 | \%Lexicon = ( |
|---|
| 55 | HEADER |
|---|
| 56 | |
|---|
| 57 | %MT::L10N::en_us::Lexicon = (); ## Suppress warning. |
|---|
| 58 | require MT::L10N::en_us; |
|---|
| 59 | eval ("require MT::L10N::$languagename"); |
|---|
| 60 | use File::Find; |
|---|
| 61 | |
|---|
| 62 | find \&wanted, $root_dir; |
|---|
| 63 | |
|---|
| 64 | print "\n ## Other phrases, with English translations.\n"; |
|---|
| 65 | for my $phrase (keys %MT::L10N::en_us::Lexicon) { |
|---|
| 66 | print encode_phrase($phrase, eval ('$MT::L10N::'.$languagename.'::Lexicon{$phrase}')); |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | print "\n ## Error messages, strings in the app code.\n"; |
|---|
| 71 | find \&wanted_app, $root_dir; |
|---|
| 72 | |
|---|
| 73 | print <<FOOTER; |
|---|
| 74 | ); |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | 1; |
|---|
| 78 | |
|---|
| 79 | ## New words: $wc |
|---|
| 80 | FOOTER |
|---|
| 81 | |
|---|
| 82 | my %SeenPhrase; |
|---|
| 83 | sub wanted { |
|---|
| 84 | my $file = $File::Find::name; |
|---|
| 85 | return unless $file =~ /\.(tmpl|pl|cgi\.pre)$/; |
|---|
| 86 | print "\n ## $file\n"; |
|---|
| 87 | open my $fh, $_ or die "Can't open $_: $!"; |
|---|
| 88 | local $_ = do { local $/; <$fh> }; |
|---|
| 89 | close $fh; |
|---|
| 90 | while (/<MT_TRANS ([^>]+)>/g) { |
|---|
| 91 | my($msg, %args) = ($1); |
|---|
| 92 | while ($msg =~ /(\w+)\s*=\s*(\\?["'])(.*?[^\\])\2/gs) { #" |
|---|
| 93 | # try avoiding strings ending in escape chars |
|---|
| 94 | # while ($msg =~ /(\w+)\s*=\s*(\\?["'])(.*?)\2/g) { #" |
|---|
| 95 | $args{$1} = $3; |
|---|
| 96 | } |
|---|
| 97 | next unless $args{phrase}; |
|---|
| 98 | next if $SeenPhrase{$args{phrase}}++; |
|---|
| 99 | next if $args{phrase} =~ /^_[A-Z]/; |
|---|
| 100 | $args{phrase} =~ s/\\\'/\'/sg; |
|---|
| 101 | print encode_phrase($args{phrase}, eval('$MT::L10N::'.$languagename.'::Lexicon{$args{phrase}}')); |
|---|
| 102 | } |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | sub wanted_app { |
|---|
| 106 | my $file = $File::Find::name; |
|---|
| 107 | return unless $file =~ m!/mt\.js$! || $file =~ /\.(pm|pl|cgi\.pre)$/; |
|---|
| 108 | print "\n ## $file\n"; |
|---|
| 109 | open my $fh, $_ or die "Can't open $_: $!"; |
|---|
| 110 | local $_ = do { local $/; <$fh> }; |
|---|
| 111 | close $fh; |
|---|
| 112 | if ($file =~ m/\.js/) { |
|---|
| 113 | while (/trans\('(.+?[^\\])'/gs) { # Anything between trans(' and '), including escaped quotes |
|---|
| 114 | my($phrase) = $1; |
|---|
| 115 | next if $SeenPhrase{$phrase}++; |
|---|
| 116 | next if $phrase =~ /^_[A-Z]/; |
|---|
| 117 | $phrase =~ s/\\\'/\'/sg; # Turn escaped quotes back into regular quotes, because the encode_phrase will re-encode them if needed |
|---|
| 118 | print encode_phrase($phrase,eval('$MT::L10N::'.$languagename.'::Lexicon{$phrase}')); |
|---|
| 119 | } |
|---|
| 120 | } else { |
|---|
| 121 | while (/(?:translate|errtrans|maketext|trans_error)\s*\(\s*("([^"]*?[^\\])"|'([^']*?[^\\])')/gs) { #" |
|---|
| 122 | # Try avoiding escape symbols at end of string |
|---|
| 123 | # while (/(?:translate|errtrans|maketext|trans_error)\s*\(\s*("([^"]*)"|'([^']*)')/gs) { #" |
|---|
| 124 | # Translate, errtrans... followed by open and closed brackets with either |
|---|
| 125 | # a double quote followed by non double quotes followed by a double quote |
|---|
| 126 | # or |
|---|
| 127 | # a single quote followed by non single quotes followed by a single quote |
|---|
| 128 | my($phrase) = ($2 || $3); |
|---|
| 129 | next if $SeenPhrase{$phrase}++; |
|---|
| 130 | next if $phrase =~ /^_[A-Z]/; |
|---|
| 131 | print encode_phrase($phrase,eval('$MT::L10N::'.$languagename.'::Lexicon{$phrase}')); |
|---|
| 132 | } |
|---|
| 133 | } |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | sub encode_phrase { |
|---|
| 137 | my($l, $r, $words) = @_; |
|---|
| 138 | $r ||= ''; |
|---|
| 139 | $l =~ s/'/\\'/g; |
|---|
| 140 | $r =~ s/'/\\'/g; |
|---|
| 141 | $l =~ s/\\\\/\\/g; |
|---|
| 142 | $r =~ s/\\\\/\\/g; |
|---|
| 143 | $words = wordcount($l); |
|---|
| 144 | $wc += $words if ($r eq ''); |
|---|
| 145 | if ($r eq '') { |
|---|
| 146 | sprintf qq( '%s' => '%s', # Translate - New (%s)\n), $l, $l, $words; |
|---|
| 147 | } else { |
|---|
| 148 | if ($l eq $r) { |
|---|
| 149 | sprintf qq( '%s' => '%s', # Translate - Previous (%s)\n), $l, $r, $words; |
|---|
| 150 | } else { |
|---|
| 151 | sprintf qq( '%s' => '%s',\n), $l, $r; |
|---|
| 152 | } |
|---|
| 153 | } |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | sub wordcount { |
|---|
| 160 | my $l = shift; |
|---|
| 161 | $l =~ s/[`!"$%^&*()_+={[}\];:@~#,.<>?\\\/]/ /g; #` |
|---|
| 162 | $l =~ s/\ ([ei])\ ([ge])\ / $1.$2./g; |
|---|
| 163 | my @words = split(/\W*\s+\W*/, $l); # see the Camel book p.39 |
|---|
| 164 | return ($#words + 1); |
|---|
| 165 | } |
|---|