line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::I18N::Command::Auto; |
2
|
1
|
|
|
1
|
|
1349
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
36
|
|
3
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
32
|
|
4
|
1
|
|
|
1
|
|
5
|
use Encode; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
109
|
|
5
|
1
|
|
|
1
|
|
5
|
use Cwd; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
70
|
|
6
|
1
|
|
|
1
|
|
6
|
use App::I18N::Config; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
29
|
|
7
|
1
|
|
|
1
|
|
5
|
use App::I18N::Logger; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
24
|
|
8
|
1
|
|
|
1
|
|
10
|
use File::Basename; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
67
|
|
9
|
1
|
|
|
1
|
|
6
|
use File::Path qw(mkpath); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
50
|
|
10
|
1
|
|
|
1
|
|
6
|
use File::Find::Rule; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
11
|
|
11
|
1
|
|
|
1
|
|
1377
|
use REST::Google::Translate; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
use base qw(App::I18N::Command); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub options { |
17
|
|
|
|
|
|
|
( |
18
|
|
|
|
|
|
|
'f|from=s' => 'from', |
19
|
|
|
|
|
|
|
't|to=s' => 'to', |
20
|
|
|
|
|
|
|
'backend=s' => 'backend', |
21
|
|
|
|
|
|
|
'locale' => 'locale', |
22
|
|
|
|
|
|
|
'verbose' => 'verbose', |
23
|
|
|
|
|
|
|
'msgstr' => 'msgstr', # translate from existing msgstr instead of translating from msgid. |
24
|
|
|
|
|
|
|
'overwrite' => 'overwrite', # overwrite existing msgstr |
25
|
|
|
|
|
|
|
'p|prompt' => 'prompt', |
26
|
|
|
|
|
|
|
) |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub run { |
32
|
|
|
|
|
|
|
my ( $self , $lang ) = @_; |
33
|
|
|
|
|
|
|
my $logger = $self->logger(); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
unless( $lang ) { |
36
|
|
|
|
|
|
|
return $logger->error( "Language name is required." ); |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
unless( $self->{from} ) { |
40
|
|
|
|
|
|
|
return $logger->error( "--from [Language] is required. please specify your language to translate." ); |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# XXX: check this option |
45
|
|
|
|
|
|
|
$self->{backend} ||= 'rest-google'; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
my $podir = $self->{podir}; |
48
|
|
|
|
|
|
|
$podir = App::I18N->guess_podir( $self ) unless $podir; |
49
|
|
|
|
|
|
|
$self->{mo} = 1 if $self->{locale}; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
mkpath [ $podir ]; |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
my $pot_name = App::I18N->pot_name; |
54
|
|
|
|
|
|
|
my $potfile = File::Spec->catfile( $podir, $pot_name . ".pot") ; |
55
|
|
|
|
|
|
|
if( ! -e $potfile ) { |
56
|
|
|
|
|
|
|
$logger->info( "$potfile not found." ); |
57
|
|
|
|
|
|
|
return; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
my $from_lang = $self->{from}; |
61
|
|
|
|
|
|
|
my $to_lang = $self->{to} || $lang; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
my $pofile; |
64
|
|
|
|
|
|
|
if( $self->{locale} ) { |
65
|
|
|
|
|
|
|
$pofile = File::Spec->join( $podir , $lang , 'LC_MESSAGES' , $pot_name . ".po" ); |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
else { |
68
|
|
|
|
|
|
|
$pofile = File::Spec->join( $podir , $lang . ".po" ); |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
my $ext = Locale::Maketext::Extract->new; |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
$logger->info( "Reading po file: $pofile" ); |
74
|
|
|
|
|
|
|
$ext->read_po($pofile); |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
my $from_lang_s = $from_lang; |
77
|
|
|
|
|
|
|
my $to_lang_s = $to_lang; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# ($from_lang_s) = ( $from_lang =~ m{^([a-z]+)(_\w+)?} ); |
80
|
|
|
|
|
|
|
# ($to_lang_s) = ( $to_lang =~ m{^([a-z]+)(_\w+)?} ); |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
$logger->info( "Translating: $from_lang_s => $to_lang_s" ); |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
$logger->info( "Initialing REST::Google" ); |
85
|
|
|
|
|
|
|
REST::Google::Translate->http_referer('http://google.com'); |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
binmode STDERR, ":utf8"; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
NEXT_MSGID: |
91
|
|
|
|
|
|
|
for my $i ($ext->msgids()) { |
92
|
|
|
|
|
|
|
my $msgid = $i; |
93
|
|
|
|
|
|
|
my $msgstr = $ext->msgstr( $i ); |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
# skip if no msgstr and no overwrite. |
96
|
|
|
|
|
|
|
next if $msgstr && ! $self->{overwrite} && ! $self->{msgstr}; |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
# translate from msgstr |
99
|
|
|
|
|
|
|
$i = $msgstr if $msgstr && $self->{msgstr}; |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
$logger->info( "********" ); |
102
|
|
|
|
|
|
|
$logger->info( " msgid: $msgid"); |
103
|
|
|
|
|
|
|
$logger->info( " msgstr: $msgstr" ) if $msgstr; |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
$logger->info( " tranlating from msgstr" ) if $self->{msgstr}; |
106
|
|
|
|
|
|
|
$logger->info( " tranlating from msgid" ) if ! $self->{msgstr}; |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
my $retry = 1; |
109
|
|
|
|
|
|
|
while($retry--) { |
110
|
|
|
|
|
|
|
my $res; |
111
|
|
|
|
|
|
|
eval { |
112
|
|
|
|
|
|
|
$res = REST::Google::Translate->new( |
113
|
|
|
|
|
|
|
q => $i, |
114
|
|
|
|
|
|
|
langpair => $from_lang_s . '|' . $to_lang_s ); |
115
|
|
|
|
|
|
|
}; |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
if( $@ ) { |
118
|
|
|
|
|
|
|
# XXX: let it retry for 3 times |
119
|
|
|
|
|
|
|
$retry = 2; |
120
|
|
|
|
|
|
|
$logger->error( "REST API ERROR: $@ , $!" ); |
121
|
|
|
|
|
|
|
$logger->info( "Retrying ..." ); |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
if ($res->responseStatus == 200) { |
125
|
|
|
|
|
|
|
my $translated = $res->responseData->translatedText; |
126
|
|
|
|
|
|
|
$logger->info( "translated: " . encode_utf8( $translated ) ); |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
if( ($msgstr && $self->{overwrite}) |
129
|
|
|
|
|
|
|
|| ! $msgstr ) { |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
if( $self->{prompt} ) { |
132
|
|
|
|
|
|
|
my $ans = $self->prompt( "Apply this ? (Y/n)" ); |
133
|
|
|
|
|
|
|
next NEXT_MSGID if $ans =~ /^\s*n\s*$/i; |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
if ( $ans !~ /^\s*y\s*$/i ) { |
136
|
|
|
|
|
|
|
print STDERR qq|Applying "$ans"\n|; |
137
|
|
|
|
|
|
|
# it's user typed msgstr |
138
|
|
|
|
|
|
|
$ext->set_msgstr( $i, $ans ); |
139
|
|
|
|
|
|
|
next NEXT_MSGID; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
print STDERR qq|Applying "$translated"\n|; |
143
|
|
|
|
|
|
|
$ext->set_msgstr($i, encode_utf8( $translated ) ); |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
else { |
147
|
|
|
|
|
|
|
$ext->set_msgstr($i, undef) if $self->{overwrite}; |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
$logger->info( "Writing po file to $pofile" ); |
154
|
|
|
|
|
|
|
$ext->write_po($pofile); |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
if( $self->{mo} ) { |
157
|
|
|
|
|
|
|
my $mofile = $pofile; |
158
|
|
|
|
|
|
|
$mofile =~ s{\.po$}{.mo}; |
159
|
|
|
|
|
|
|
$logger->info( "Updating MO file: $mofile" ); |
160
|
|
|
|
|
|
|
system(qq{msgfmt -v $pofile -o $mofile}); |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
$logger->info( "Done" ); |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
1; |
170
|
|
|
|
|
|
|
__END__ |