line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Command::generate::lexicont; |
2
|
3
|
|
|
3
|
|
2217
|
use 5.008005; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
97
|
|
3
|
3
|
|
|
3
|
|
12
|
use strict; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
94
|
|
4
|
3
|
|
|
3
|
|
15
|
use warnings; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
113
|
|
5
|
3
|
|
|
3
|
|
1514
|
use Mojo::Base 'Mojolicious::Command'; |
|
3
|
|
|
|
|
21836
|
|
|
3
|
|
|
|
|
28
|
|
6
|
3
|
|
|
3
|
|
306729
|
use Lingua::Translate; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
use Config::PL; |
8
|
|
|
|
|
|
|
use Carp; |
9
|
|
|
|
|
|
|
use Encode qw/decode/; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
__PACKAGE__->attr(description => <<'EOF'); |
12
|
|
|
|
|
|
|
Generate lexicon file translations. |
13
|
|
|
|
|
|
|
EOF |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
__PACKAGE__->attr(usage => <<"EOF"); |
16
|
|
|
|
|
|
|
usage: APPLICATION generate lexicont src_lang dest_lang ... |
17
|
|
|
|
|
|
|
EOF |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
__PACKAGE__->attr('conf_file'); |
20
|
|
|
|
|
|
|
__PACKAGE__->attr('conf'); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
our $VERSION = "0.03"; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub run { |
26
|
|
|
|
|
|
|
my $self = shift; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
my $arg_num = @_; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
if ($arg_num < 2){ |
31
|
|
|
|
|
|
|
croak $self->usage; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
my $src_lang = shift; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
my $app; |
37
|
|
|
|
|
|
|
if (ref $self->app eq 'CODE'){ |
38
|
|
|
|
|
|
|
$app = $self->app->(); |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
else{ |
41
|
|
|
|
|
|
|
$app = $self->app; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
my $app_class = ref $app; |
44
|
|
|
|
|
|
|
$app_class =~ s{::}{/}g; |
45
|
|
|
|
|
|
|
my $app_klass = ref $app; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
my $verbose; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
my @dest_langs = @_; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
my $src_file = $app->home->rel_file("lib/$app_class/I18N/${src_lang}.pm"); |
52
|
|
|
|
|
|
|
my $org_file = $app->home->rel_file("lib/$app_class/I18N/org.pm"); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
if ( ! -e $org_file && ! -e $src_file) { |
55
|
|
|
|
|
|
|
croak <
|
56
|
|
|
|
|
|
|
Src lexicon not found $src_file |
57
|
|
|
|
|
|
|
NOTFOUND |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
my %srclex = (); |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
if (-e $src_file){ |
63
|
|
|
|
|
|
|
%srclex = eval { |
64
|
|
|
|
|
|
|
require "$app_class/I18N/${src_lang}.pm"; |
65
|
|
|
|
|
|
|
no strict 'refs'; |
66
|
|
|
|
|
|
|
%{*{"${app_klass}::I18N::${src_lang}::Lexicon"}}; |
67
|
|
|
|
|
|
|
}; |
68
|
|
|
|
|
|
|
if ($@){ |
69
|
|
|
|
|
|
|
croak( "error $@" ); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
my $conf_file = $self->conf_file || "lexicont.conf"; |
74
|
|
|
|
|
|
|
my $conf; |
75
|
|
|
|
|
|
|
eval{ |
76
|
|
|
|
|
|
|
$conf = config_do $conf_file; |
77
|
|
|
|
|
|
|
}; |
78
|
|
|
|
|
|
|
if ($@){ |
79
|
|
|
|
|
|
|
croak "Config file cannot read $@ ($conf_file)" |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
$self->conf($conf); |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
if (-e $org_file) { |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
my %orglex = eval { |
87
|
|
|
|
|
|
|
require "$app_class/I18N/org.pm"; |
88
|
|
|
|
|
|
|
no strict 'refs'; |
89
|
|
|
|
|
|
|
%{*{"${app_klass}::I18N::org::Lexicon"}}; |
90
|
|
|
|
|
|
|
}; |
91
|
|
|
|
|
|
|
if ($@){ |
92
|
|
|
|
|
|
|
croak( "error $@" ); |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
my %changes = (); |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
for my $key (%orglex){ |
98
|
|
|
|
|
|
|
if ( defined $srclex{$key} && ($orglex{$key} ne $srclex{$key})){ |
99
|
|
|
|
|
|
|
$changes{$key} = 1; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
for my $dest_lang (@dest_langs){ |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
my $dest_file = $app->home->rel_file("lib/$app_class/I18N/${dest_lang}.pm"); |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
my %destlex = (); |
108
|
|
|
|
|
|
|
if ( -e $dest_file){ |
109
|
|
|
|
|
|
|
%destlex = eval { |
110
|
|
|
|
|
|
|
require "$app_class/I18N/${dest_lang}.pm"; |
111
|
|
|
|
|
|
|
no strict 'refs'; |
112
|
|
|
|
|
|
|
%{*{"${app_klass}::I18N::${dest_lang}::Lexicon"}}; |
113
|
|
|
|
|
|
|
}; |
114
|
|
|
|
|
|
|
if ($@){ |
115
|
|
|
|
|
|
|
croak( "error $@" ); |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
my %lexicon = (); |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
for my $key (keys %orglex){ |
122
|
|
|
|
|
|
|
if ( ! defined $srclex{$key} || (defined $changes{$key} && $changes{$key} == 1)){ |
123
|
|
|
|
|
|
|
$lexicon{$key} = $self->translate( $src_lang, $dest_lang, $orglex{$key}); |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
else{ |
126
|
|
|
|
|
|
|
$lexicon{$key} = $destlex{$key}; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
# Output lexem |
131
|
|
|
|
|
|
|
$self->render_to_file('package', $dest_file, $app_klass, $dest_lang, |
132
|
|
|
|
|
|
|
\%lexicon); |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
if ( defined $conf->{json} && $conf->{json} == 1 ){ |
135
|
|
|
|
|
|
|
my $dest_json = $app->home->rel_file("public/${dest_lang}.json"); |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
# Output json |
138
|
|
|
|
|
|
|
$self->render_to_file('json', $dest_json, $app_klass, $dest_lang, |
139
|
|
|
|
|
|
|
\%lexicon); |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
my %utf8_orglex = map { $_ => (utf8::is_utf8 ($orglex{$_})) ? $orglex{$_} : decode("utf8", $orglex{$_})} keys %orglex; |
145
|
|
|
|
|
|
|
my $src_file = $app->home->rel_file("lib/$app_class/I18N/${src_lang}.pm"); |
146
|
|
|
|
|
|
|
$self->render_to_file('package', $src_file, $app_klass, $src_lang, |
147
|
|
|
|
|
|
|
\%utf8_orglex); |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
if ( defined $conf->{json} && $conf->{json} == 1 ){ |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
my $dest_json = $app->home->rel_file("public/${src_lang}.json"); |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
# Output json |
154
|
|
|
|
|
|
|
$self->render_to_file('json', $dest_json, $app_klass, $src_lang, |
155
|
|
|
|
|
|
|
\%utf8_orglex); |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
else{ |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
for my $dest_lang (@dest_langs){ |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
my $dest_file = $app->home->rel_file("lib/$app_class/I18N/${dest_lang}.pm"); |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
my %lexicon = map { $_ => $self->translate( $src_lang, $dest_lang, $srclex{$_}) } keys %srclex; |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
# Output lexem |
169
|
|
|
|
|
|
|
$self->render_to_file('package', $dest_file, $app_klass, $dest_lang, |
170
|
|
|
|
|
|
|
\%lexicon); |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
if ( defined $conf->{json} && $conf->{json} == 1 ){ |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
my $dest_json = $app->home->rel_file("public/${dest_lang}.json"); |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
# Output json |
177
|
|
|
|
|
|
|
$self->render_to_file('json', $dest_json, $app_klass, $dest_lang, |
178
|
|
|
|
|
|
|
\%lexicon); |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
if ( defined $conf->{json} && $conf->{json} == 1 ){ |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
my $dest_json = $app->home->rel_file("public/${src_lang}.json"); |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
my %utf8_srclex = map { $_ => (utf8::is_utf8 ($srclex{$_})) ? $srclex{$_} : decode("utf8", $srclex{$_})} keys %srclex; |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
# Output json |
190
|
|
|
|
|
|
|
$self->render_to_file('json', $dest_json, $app_klass, $src_lang, |
191
|
|
|
|
|
|
|
\%utf8_srclex); |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
} |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
} |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
} |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
sub translate{ |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
my $self = shift; |
202
|
|
|
|
|
|
|
my $src = shift; |
203
|
|
|
|
|
|
|
my $dest = shift; |
204
|
|
|
|
|
|
|
my $text = shift; |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
my $xl8r; |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
eval{ |
209
|
|
|
|
|
|
|
$xl8r = Lingua::Translate->new(%{$self->conf->{lingua_translate}}, src => $src, dest => $dest); |
210
|
|
|
|
|
|
|
}; |
211
|
|
|
|
|
|
|
if ($@){ |
212
|
|
|
|
|
|
|
croak "Lingua::Translate create error $@"; |
213
|
|
|
|
|
|
|
} |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
my $trans_text = ''; |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
eval{ |
218
|
|
|
|
|
|
|
$trans_text = $xl8r->translate($text); |
219
|
|
|
|
|
|
|
if (defined $self->conf->{sleep}){ |
220
|
|
|
|
|
|
|
sleep( $self->conf->{sleep} ); |
221
|
|
|
|
|
|
|
} |
222
|
|
|
|
|
|
|
}; |
223
|
|
|
|
|
|
|
if ($@){ |
224
|
|
|
|
|
|
|
warn ("Cannot translate $@"); |
225
|
|
|
|
|
|
|
} |
226
|
|
|
|
|
|
|
return $trans_text; |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
} |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
1; |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
__DATA__ |