line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Table::Trans; |
2
|
4
|
|
|
4
|
|
143258
|
use warnings; |
|
4
|
|
|
|
|
27
|
|
|
4
|
|
|
|
|
137
|
|
3
|
4
|
|
|
4
|
|
21
|
use strict; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
93
|
|
4
|
4
|
|
|
4
|
|
21
|
use Carp; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
299
|
|
5
|
4
|
|
|
4
|
|
1299
|
use utf8; |
|
4
|
|
|
|
|
31
|
|
|
4
|
|
|
|
|
25
|
|
6
|
|
|
|
|
|
|
require Exporter; |
7
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
8
|
|
|
|
|
|
|
our @EXPORT_OK = qw/ |
9
|
|
|
|
|
|
|
add_trans |
10
|
|
|
|
|
|
|
get_lang_name |
11
|
|
|
|
|
|
|
get_lang_trans |
12
|
|
|
|
|
|
|
get_single_trans |
13
|
|
|
|
|
|
|
read_trans |
14
|
|
|
|
|
|
|
trans_to_json_file |
15
|
|
|
|
|
|
|
write_trans |
16
|
|
|
|
|
|
|
/; |
17
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( |
18
|
|
|
|
|
|
|
all => \@EXPORT_OK, |
19
|
|
|
|
|
|
|
); |
20
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
21
|
|
|
|
|
|
|
|
22
|
4
|
|
|
4
|
|
2427
|
use Table::Readable '0.05', qw!read_table read_table_hash!; |
|
4
|
|
|
|
|
6262
|
|
|
4
|
|
|
|
|
622
|
|
23
|
4
|
|
|
4
|
|
1875
|
use JSON::Create 'write_json'; |
|
4
|
|
|
|
|
5195
|
|
|
4
|
|
|
|
|
232
|
|
24
|
4
|
|
|
4
|
|
1829
|
use JSON::Parse; # Used for test only in fact. |
|
4
|
|
|
|
|
5077
|
|
|
4
|
|
|
|
|
4789
|
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
my %lang2name; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub add_trans |
29
|
|
|
|
|
|
|
{ |
30
|
0
|
|
|
0
|
1
|
0
|
my ($trans, $file) = @_; |
31
|
0
|
|
|
|
|
0
|
my $trans2 = read_trans ($file); |
32
|
0
|
|
|
|
|
0
|
for my $id (keys %$trans2) { |
33
|
0
|
0
|
|
|
|
0
|
if ($trans->{$id}) { |
34
|
0
|
|
|
|
|
0
|
warn "$file: $id is duplicated.\n"; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
else { |
37
|
0
|
|
|
|
|
0
|
$trans->{$id} = $trans2->{$id}; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub get_single_trans |
43
|
|
|
|
|
|
|
{ |
44
|
0
|
|
|
0
|
1
|
0
|
my ($trans, $id, $lang) = @_; |
45
|
0
|
0
|
|
|
|
0
|
if (! $trans->{$id}) { |
46
|
0
|
|
|
|
|
0
|
croak "Unknown id '$id'"; |
47
|
|
|
|
|
|
|
} |
48
|
0
|
0
|
|
|
|
0
|
if (! $trans->{$id}->{$lang}) { |
49
|
0
|
|
|
|
|
0
|
carp "Id '$id' has no translation in $lang"; |
50
|
|
|
|
|
|
|
} |
51
|
0
|
|
|
|
|
0
|
return $trans->{$id}->{$lang}; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub get_lang_trans |
57
|
|
|
|
|
|
|
{ |
58
|
0
|
|
|
0
|
1
|
0
|
my ($trans, $vars, $lang, $verbose) = @_; |
59
|
0
|
|
|
|
|
0
|
my $varstrans = {}; |
60
|
0
|
|
|
|
|
0
|
for my $id (keys %{$trans}) { |
|
0
|
|
|
|
|
0
|
|
61
|
0
|
0
|
|
|
|
0
|
if ($verbose) { |
62
|
0
|
|
|
|
|
0
|
print "$id, $trans->{$id}{$lang}\n"; |
63
|
|
|
|
|
|
|
} |
64
|
0
|
|
|
|
|
0
|
my $value; |
65
|
0
|
0
|
|
|
|
0
|
if ($trans->{$id}{all}) { |
66
|
0
|
|
|
|
|
0
|
$value = $trans->{$id}{all}; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
else { |
69
|
0
|
|
|
|
|
0
|
$value = $trans->{$id}{$lang}; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
# The following test checks whether $value is defined because |
72
|
|
|
|
|
|
|
# an empty string may be a valid translation (for example if |
73
|
|
|
|
|
|
|
# something does not need to be translated). |
74
|
0
|
0
|
|
|
|
0
|
if (! defined $value) { |
75
|
0
|
0
|
|
|
|
0
|
if ($verbose) { |
76
|
0
|
|
|
|
|
0
|
warn "No translation for $id for language $lang: substituting English."; |
77
|
|
|
|
|
|
|
} |
78
|
0
|
|
|
|
|
0
|
$value = $trans->{$id}->{en}; |
79
|
|
|
|
|
|
|
} |
80
|
0
|
|
|
|
|
0
|
$varstrans->{$id} = $value; |
81
|
|
|
|
|
|
|
} |
82
|
0
|
|
|
|
|
0
|
$vars->{trans} = $varstrans; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub get_lang_name |
89
|
|
|
|
|
|
|
{ |
90
|
1
|
|
|
1
|
1
|
87
|
my ($lang) = @_; |
91
|
1
|
50
|
|
|
|
6
|
if (scalar (keys %lang2name) == 0) { |
92
|
1
|
|
|
|
|
3
|
my $l2nfile = __FILE__; |
93
|
1
|
|
|
|
|
5
|
$l2nfile =~ s!Trans\.pm!l2n.txt!; |
94
|
1
|
|
|
|
|
9
|
my @langs = read_table ($l2nfile); |
95
|
1
|
|
|
|
|
28528
|
for my $lang (@langs) { |
96
|
270
|
|
|
|
|
732
|
$lang2name{$lang->{lang}} = $lang->{name}; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
} |
99
|
1
|
|
|
|
|
5
|
my $name = $lang2name{$lang}; |
100
|
1
|
50
|
|
|
|
37
|
if (! $name) { |
101
|
0
|
|
|
|
|
0
|
$name = $lang; |
102
|
|
|
|
|
|
|
} |
103
|
1
|
|
|
|
|
10
|
return $name; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub read_trans |
108
|
|
|
|
|
|
|
{ |
109
|
2
|
|
|
2
|
1
|
12759
|
my ($input_file, %options) = @_; |
110
|
2
|
|
|
|
|
16
|
my ($trans, $order) = read_table_hash ($input_file, 'id', %options); |
111
|
2
|
|
|
|
|
12848
|
x_link ($trans, $order); |
112
|
2
|
50
|
|
|
|
8
|
if (wantarray ()) { |
113
|
0
|
|
|
|
|
0
|
return ($trans, $order); |
114
|
|
|
|
|
|
|
} |
115
|
2
|
|
|
|
|
8
|
return $trans; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub trans_to_json_file |
119
|
|
|
|
|
|
|
{ |
120
|
1
|
|
|
1
|
1
|
121
|
my ($trans_file, $json_file) = @_; |
121
|
1
|
|
|
|
|
5
|
my $trans = read_trans ($trans_file); |
122
|
1
|
|
|
|
|
10
|
write_json ($json_file, $trans, indent => 1, sort => 1); |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
sub write_trans |
126
|
|
|
|
|
|
|
{ |
127
|
0
|
|
|
0
|
1
|
0
|
my ($trans, $lang_ref, $file_name, $id_order_ref) = @_; |
128
|
0
|
0
|
|
|
|
0
|
if (ref $lang_ref ne 'ARRAY') { |
129
|
0
|
|
|
|
|
0
|
croak "write_trans requires an array reference of languages to print as its second argument."; |
130
|
|
|
|
|
|
|
} |
131
|
0
|
0
|
|
|
|
0
|
open my $output, '>:encoding(utf8)', $file_name or die $!; |
132
|
0
|
|
|
|
|
0
|
my @id_order; |
133
|
0
|
0
|
|
|
|
0
|
if ($id_order_ref) { |
134
|
0
|
|
|
|
|
0
|
@id_order = @{$id_order_ref}; |
|
0
|
|
|
|
|
0
|
|
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
else { |
137
|
0
|
|
|
|
|
0
|
warn "No order supplied.\n"; |
138
|
0
|
|
|
|
|
0
|
@id_order = keys %$trans; |
139
|
|
|
|
|
|
|
} |
140
|
0
|
|
|
|
|
0
|
for my $id (@id_order) { |
141
|
0
|
|
|
|
|
0
|
print $output "id: $id\n"; |
142
|
0
|
|
|
|
|
0
|
for my $lang (@$lang_ref) { |
143
|
0
|
|
|
|
|
0
|
my $t = $trans->{$id}->{$lang}; |
144
|
0
|
0
|
|
|
|
0
|
if (! $t) { |
145
|
0
|
|
|
|
|
0
|
$t = $trans->{$id}->{en}; |
146
|
|
|
|
|
|
|
} |
147
|
0
|
0
|
|
|
|
0
|
if (! $t) { |
148
|
0
|
|
|
|
|
0
|
croak "Translation $id does not have an English translation."; |
149
|
|
|
|
|
|
|
} |
150
|
0
|
|
|
|
|
0
|
$t =~ s/\s+$//; |
151
|
0
|
|
|
|
|
0
|
print $output "%%$lang:\n$t\n%%\n"; |
152
|
|
|
|
|
|
|
} |
153
|
0
|
|
|
|
|
0
|
print $output "\n"; |
154
|
|
|
|
|
|
|
} |
155
|
0
|
|
|
|
|
0
|
close $output; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
my $x_lang_re = qr/\{\{(\w+)\}\}/; |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
sub x_link |
161
|
|
|
|
|
|
|
{ |
162
|
2
|
|
|
2
|
0
|
7
|
my ($trans_ref, $order) = @_; |
163
|
|
|
|
|
|
|
# X-trans links to copy text from one bit of the translation to another. |
164
|
2
|
|
|
|
|
7
|
for my $id (@$order) { |
165
|
4
|
|
|
|
|
9
|
my $trans = $trans_ref->{$id}; |
166
|
|
|
|
|
|
|
|
167
|
4
|
|
|
|
|
13
|
for my $lang (keys %$trans) { |
168
|
|
|
|
|
|
|
# Check the links go somewhere |
169
|
14
|
|
|
|
|
71
|
while ($trans->{$lang} =~ /$x_lang_re/g) { |
170
|
1
|
|
|
|
|
15
|
my $w = $1; |
171
|
1
|
|
|
|
|
3
|
my $t = $trans_ref->{$w}{all}; |
172
|
1
|
50
|
|
|
|
4
|
if (! $t) { |
173
|
1
|
|
|
|
|
3
|
$t = $trans_ref->{$w}{$lang}; |
174
|
|
|
|
|
|
|
} |
175
|
1
|
50
|
|
|
|
4
|
if (! $t) { |
176
|
0
|
|
|
|
|
0
|
die "Bad X-trans {{$w}} in $id for language id '$lang'.\n"; |
177
|
|
|
|
|
|
|
} |
178
|
1
|
|
|
|
|
20
|
$trans->{$lang} =~ s/\{\{$w\}\}/$t/g; |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
1; |