-
Notifications
You must be signed in to change notification settings - Fork 28
/
subst_text.pl
132 lines (116 loc) · 3.53 KB
/
subst_text.pl
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
#!/usr/bin/env perl
sub Usage {
print STDERR "Usage: subst_text <translation_file> <language_code> <source_file> <dest_file>\n";
exit 1;
}
sub Error {
print STDERR (join ' ', @_), "\n";
exit 1;
}
if ($#ARGV != 3) {
&Usage;
}
my ($translation_file, $language_code, $source_file, $dest_file) = @ARGV;
undef $/;
open(TRANSLATIONS, $translation_file) || &Error("Could not open file: $translation_file");
my $trans_data = <TRANSLATIONS>;
close(TRANSLATIONS);
sub ParseKey {
my ($input, $pos) = @_;
my ($skip, $key) = substr($input, $pos) =~ m/\A(\s*([a-zA-Z][a-zA-Z_0-9]*))/;
return unless $key;
$pos += length $skip;
my ($skip2, $sep) = (substr($input, $pos) =~ /^( *(\{|:))/);
if (!$sep) {
print STDERR "At key $key, expected : or { at position $pos: ", substr($input, $pos, 10), "...\n";
return;
}
$pos += length $skip2;
if ($sep eq '{') {
my ($skip3) = (substr($input, $pos) =~ /(\s*\n)/);
$pos += length $skip3;
}
return ($key, $sep, $pos);
}
sub ParseField {
my ($input, $pos) = @_;
#print STDERR "calling ParseKey\n";
(my $key, $sep, $pos) = &ParseKey($input, $pos);
return unless $key;
#print STDERR "back from ParseKey\n";
if ($sep eq ':') {
my ($skip, $value) = substr($input, $pos) =~ /^(\s*(.*))\n/;
return ($key, $value, $pos + length $skip);
} else {
my $n = length $input;
my $pos2 = $pos;
my $terminator;
while ($pos2 < $n) {
($terminator) = substr($input, $pos2) =~ /\A(\n\s*\}\s*\n)/;
last if $terminator;
$pos2++;
}
my $value = substr($input, $pos, $pos2 - $pos);
$value =~ s/\\\r?\n\s*//g;
$value =~ s/\\ / /g;
$pos2 += length $terminator;
return ($key, $value, $pos2);
}
}
sub SkipComments {
my ($input, $pos) = @_;
while (1) {
my ($skip) = substr($input, $pos) =~ /\A(\s*#.*\n)/;
my $n = length $skip || 0;
return $pos unless $n;
$pos += $n
}
return $pos;
}
sub ParseEntry {
my ($input, $pos) = @_;
$pos = &SkipComments($input, $pos);
(my $key, $sep, $pos) = &ParseKey($input, $pos);
return unless $key;
my %fields;
while (1) {
(my $field, my $value, my $npos) = &ParseField($input, $pos);
last unless ($field);
$pos = $npos;
$fields{$field} = $value;
}
my ($skip) = substr($input,$pos) =~ /^(\s*\}\s*\n)/;
if (!$skip) {
print STDERR "Expected } at position $pos: ", substr($input,
$pos, 10), "\n";
}
return ($key, \%fields, $pos + length($skip));
}
sub ParseTranslations {
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} = $translation;
}
if ($pos < length $input) {
print STDERR "Unexpected input at $pos: ", substr($input, pos, 15), "...\n";
}
return \%translations;
}
my %translations = %{&ParseTranslations($trans_data, $language_code)};
open(SOURCE, $source_file) || &Error("Can't open $source_file $source_file");
my $source = <SOURCE>;
close(SOURCE);
foreach my $name (keys %translations) {
my $text = $translations{$name};
$source =~ s[\@$name\@][$text]g;
}
open(DEST, ">$dest_file") || Error "Could not open $dest_file for writing";
print DEST $source;
close(DEST);