root/trunk/build/make-l10n @ 3531

Revision 3531, 4.5 kB (checked in by fumiakiy, 9 months ago)

Merged sockfish to trunk. "svn merge -r3114:3527 http://code.sixapart.com/svn/movabletype/branches/sockfish/ ."

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
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
9use strict;
10
11if (grep { /--help/ } @ARGV) {
12    print <<USAGE;
13Mode d\'emploi:
14
15    make-l10n <root directory>
16
17<root directory> defaults to typepad.
18USAGE
19exit(1);
20}
21
22my $wc = 0;
23my $root_dir = ($ARGV[0] || 'typepad');
24my $languagename = ($ARGV[1] || 'fr');
25
26my $lib_dir = $root_dir . '/lib';
27
28use lib './lib';
29
30
31print <<"PACKAGENAME";
32package MT::L10N::$languagename;
33PACKAGENAME
34
35my $yr = (localtime(time))[5] + 1900;
36
37my $parent_class = $languagename eq 'en_us' ? 'MT::L10N' : 'MT::L10N::en_us';
38print <<"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
45use strict;
46use $parent_class;
47use vars qw( \@ISA \%Lexicon );
48\@ISA = qw( $parent_class );
49
50sub encoding { 'utf-8' }
51
52## The following is the translation table.
53
54\%Lexicon = (
55HEADER
56
57%MT::L10N::en_us::Lexicon = ();  ## Suppress warning.
58require MT::L10N::en_us;
59eval ("require MT::L10N::$languagename");
60use File::Find;
61
62find \&wanted, $root_dir;
63
64print "\n    ## Other phrases, with English translations.\n";
65for my $phrase (keys %MT::L10N::en_us::Lexicon) {
66    print encode_phrase($phrase, eval ('$MT::L10N::'.$languagename.'::Lexicon{$phrase}'));
67}
68
69
70print "\n    ## Error messages, strings in the app code.\n";
71find \&wanted_app, $root_dir;
72
73print <<FOOTER;
74);
75
76
771;
78
79## New words: $wc
80FOOTER
81
82my %SeenPhrase;
83sub 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
105sub 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
136sub 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
159sub 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}
Note: See TracBrowser for help on using the browser.