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