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