-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmake-translation
executable file
·220 lines (186 loc) · 6.38 KB
/
make-translation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/env perl
# This script generates internationalization for specified languages.
# Translations are generated using Google Translate. It can generate
# both a Perl script file (e.g., italian.pm) and update the static
# translation configuration (html/static-translations.txt) with new entries
# for a language (e.g., add 'it' entries). New static translations are
# generated into a file new-translations.txt which should be copied back
# to static-translations.txt once the updates are confirmed (and perhaps
# modified by hand).
my $home=$ENV{"HOME"};
# This needs to be updated to a file containg a valid authorization key
my $credfile="$home/civs/axiomatic-set-373716-3a0f9821df99.json";
# Location of the gcloud script
my $gcloud="$home/google-cloud-sdk/bin/gcloud";
use lib ".";
use lib "$home/civs/cgi-bin";
use lib '/Library/WebServer/CGI-Executables/civs';
use strict;
use URI::Encode;
use base_language;
use JSON;
use translations;
use IO::Handle;
use IO::File;
my $language = $ARGV[0];
my $pmfile = $ARGV[1];
if (!defined($language) || !defined($pmfile)) {
print STDERR "Usage: make-translation <language-code> <pm file>\n";
print STDERR " Passing a empty string for the .pm file skips that step\n";
exit 1
}
my $uri = URI::Encode->new( { encode_reserved => 0 } );
my $token=`GOOGLE_APPLICATION_CREDENTIALS=$credfile $gcloud auth application-default print-access-token`;
my $enckey = $uri->encode($token);
my $command='curl -s -X POST
-H "Content-Type: application/json"
-H "Authorization: Bearer '.$token.'"
--data "{
\'q\': <INPUT_TEXT>,
\'source\': \'en\',
\'target\': \''.$language.'\',
\'format\': \'text\'
}"
https://translation.googleapis.com/language/translate/v2';
$command =~ s/\n/ /g;
$command =~ s/ */ /g;
my $json = JSON->new->allow_nonref;
sub shell_quote {
(my $s) = @_;
$s =~ s/\\/\\\\/g;
$s =~ s/"/\\"/g;
$s =~ s/\$/\\\$/g;
$s
}
sub translate {
(my $text) = @_;
my $cmd = $command;
$text = $json->encode($text);
# print "JSON encoded: ", $text, "\n";
$text = shell_quote($text);
# print "shell encoded: ", $text, "\n";
$cmd =~ s/<INPUT_TEXT>/$text/;
# print "COMMAND = $cmd\n";
my $result = `$cmd`;
# print "RESULT = $result\n";
my $hash = $json->decode($result);
my @translations = @{$hash->{data}->{translations}};
return $translations[0]->{translatedText};
}
if ($pmfile ne "") {
print STDERR "Generating $pmfile...\n";
open(LANG, "<$home/civs/cgi-bin/base_language.pm");
sysopen (my $pmh, "$pmfile", O_WRONLY | O_CREAT | O_EXCL)
or die "Can't create new file $pmfile";
my $cur = <LANG>;
while ($cur) {
if ($cur =~ m/### BEGIN TRANSLATIONS ###/) { last }
$cur = <LANG>;
}
print $pmh $_;
while ($cur) {
if ($cur =~ m/^sub [A-Za-z][A-Za-z0-9_]* {/) {
my $header = $cur;
print STDERR "$header";
my $decl = "";
$cur = <LANG>;
if ($cur =~ m/^ +my \(.*\) = \@_;/) {
$decl = $cur;
$cur = <LANG>;
}
my $body = "";
my ($translated, $quote, $unquote);
while ($cur) {
if ($cur =~ m/^}/) {
if ($body =~ m/^\s*'/) {
$body =~ s/^\s*'//;
$body =~ s/';?\s*\Z//;
$body =~ s/\n\n/<PARAGRAPH>/g;
$body =~ s/\n/ /g;
$body =~ s/ */ /g;
$body =~ s/<PARAGRAPH>/\n/g;
# print "single-quoted: " , $body, "\n";
$quote = " '";
$unquote = "'";
$translated = translate $body;
} elsif ($body =~ m/^\s*"/) {
$body =~ s/^\s*"//;
$body =~ s/";?\s*\Z//;
$body =~ s/\n\n/<PARAGRAPH>/g;
$body =~ s/\n/ /g;
$body =~ s/ */ /g;
$body =~ s/<PARAGRAPH>/\n/g;
$quote = ' "';
$unquote = '"';
$translated = translate $body;
} else {
$quote = $unquote = '';
$translated = $body . '# UNTRANSLATED';
}
print $pmh $header, $decl, $quote, $translated, $unquote, "\n}\n";
last
}
$body .= $cur;
$cur = <LANG>;
}
} else {
print $pmh $cur;
}
$cur = <LANG>;
}
close (LANG);
} # end pmfile output
else {
print "Skipping generation of .pm file (empty filename provided)\n";
}
my $trans_data;
{
local $/;
open (TRANSLATIONS, "<html/static-translations.txt") || die "can't find translations";
$trans_data = <TRANSLATIONS>;
close (TRANSLATIONS);
}
sub ParseAllTranslations {
my ($input) = @_;
my $pos = 0;
my @result;
my %translations;
while (1) {
(my $key, my $fields, my $npos) = &ParseEntry($input, $pos);
last unless $key;
$pos = $npos;
# my $translation = $fields->{$language_code} || $fields->{'en'} || 'unknown';
$translations{$key} = $fields;
}
if ($pos < length $input) {
print STDERR "Unexpected input at $pos: ", substr($input, pos, 15), "...\n";
}
return \%translations;
}
my %translations = %{&ParseAllTranslations($trans_data)};
my %new_translation;
my $translation;
my $filename = "new-translations.txt";
my $outputfile = IO::File->new("$filename", O_WRONLY | O_CREAT | O_EXCL, 0666) or
die ("Cannot create $filename: $!");
foreach my $key (keys %translations) {
print STDERR "$key...";
my $fields = $translations{$key};
my $text = $fields->{en};
$text =~ s/\n\n/<PARAGRAPH>/g;
$text =~ s/\n/ /g;
$text =~ s/ */ /g;
$text =~ s/<PARAGRAPH>/\n/g;
if (!$translations{$key}->{$language}) {
$translation = translate $text;
print STDERR "translated\n";
} else {
print STDERR "already defined\n";
}
# $translation = $text;
# print STDERR $key, ": ", $translation, "\n";
$new_translation{$key} = $translation;
}
&EditTranslationFile($outputfile, \%new_translation, $language);
&ParseAllTranslations($trans_data);
undef $outputfile;