forked from webmin/webmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfix-english.pl
executable file
·209 lines (191 loc) · 4.25 KB
/
fix-english.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
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
#!/usr/local/bin/perl
# Convert words in lang/en files from UK to US spelling.
# Create lang/en_GB files containing words that are different.
if ($ARGV[0] eq "--svn" || $ARGV[0] eq "-svn" ||
$ARGV[0] eq "--git" || $ARGV[0] eq "-git") {
shift(@ARGV);
$svn = shift(@ARGV);
}
chdir("/usr/local/webadmin");
if (@ARGV) {
@modules = @ARGV;
}
else {
@modules = ( "." );
opendir(DIR, ".");
foreach $d (readdir(DIR)) {
push(@modules, $d) if (-r "$d/module.info");
}
closedir(DIR);
}
# Get the words
open(MAPPING, "english-mappings.txt") ||
die "Failed to open english-mappings.txt";
while(<MAPPING>) {
s/\r|\n//g;
s/#.*$//;
my ($us, $uk) = split(/\t+/, $_);
if ($us && $uk) {
push(@us_mappings, [ $us, $uk ]);
}
}
close(MAPPING);
@uk_mappings = map { [ $_->[1], $_->[0] ] } @us_mappings;
print STDERR "Found ",scalar(@uk_mappings)," mappings\n";
# Do all the given modules
@rv = ( );
foreach $m (@modules) {
print STDERR "Doing module $m\n";
push(@rv, &fix_english_file("$m/lang/en", "$m/lang/en_GB", 1));
push(@rv, &fix_english_file("$m/config.info",
"$m/config.info.en_GB", 1));
opendir(HELP, "$m/help");
foreach $h (readdir(HELP)) {
if ($h =~ /^([^\.]+)\.html$/) {
push(@rv, &fix_english_file("$m/help/$h",
"$m/help/$1.en_GB.html", 0));
}
}
closedir(HELP);
}
# Print and commit the files
foreach $f (@rv) {
print $f,"\n";
if ($svn) {
($dir, $rest) = split(/\//, $f, 2);
system("cd $dir ; git add $rest ; git commit -m '$svn' $rest ; git push");
}
}
sub fix_english_file
{
local ($us, $uk, $linefmt) = @_;
return ( ) if (!-r $us);
local @rv;
if ($linefmt) {
# Webmin = separated line file
# First fix up any UK spellings in the US file
local %uslines;
&read_file($us, \%uslines);
local $changed_us;
foreach my $k (keys %uslines) {
$v = $uslines{$k};
$usv = &convert_to_us($v);
if ($usv ne $v) {
$uslines{$k} = $usv;
$changed_us++;
}
}
if ($changed_us) {
&write_file($us, \%uslines);
push(@rv, $us);
}
# Then create a UK file with only lines that need changing
local %uklines;
&read_file($uk, \%uklines);
local $changed_uk;
foreach my $k (keys %uslines) {
$v = $uslines{$k};
$ukv = &convert_to_uk($v);
if ($ukv ne $v && $uklines{$k} ne $ukv) {
$uklines{$k} = $ukv;
$changed_uk++;
}
}
if ($changed_uk) {
&write_file($uk, \%uklines);
push(@rv, $uk);
}
}
else {
# Big blob of text
# First fix up any UK spellings in the US file
local $ustext = &read_file_contents($us);
$usv = &convert_to_us($ustext);
if ($usv ne $ustext) {
&write_file_contents($us, $usv);
push(@rv, $us);
}
# Then create a UK file
$uktext = &read_file_contents($uk);
$ukv = &convert_to_uk($usv);
if ($uktext ne $ukv && $ukv ne $usv) {
&write_file_contents($uk, $ukv);
push(@rv, $uk);
}
}
return @rv;
}
sub convert_to_us
{
local ($str) = @_;
return &convert_mapping($str, \@uk_mappings);
}
sub convert_to_uk
{
local ($str) = @_;
return &convert_mapping($str, \@us_mappings);
}
sub convert_mapping
{
local ($str, $fromto) = @_;
foreach my $w (@$fromto) {
my ($from, $to) = @$w;
$str =~ s/(\s|^)\Q$from\E(\s|$)/$1$to$2/g;
$from = ucfirst($from);
$to = ucfirst($to);
$str =~ s/(\s|^)\Q$from\E(\s|$)/$1$to$2/g;
}
return $str;
}
# read_file(file, &assoc, [&order], [lowercase])
# Fill an associative array with name=value pairs from a file
sub read_file
{
open(ARFILE, $_[0]) || return 0;
while(<ARFILE>) {
s/\r|\n//g;
if (!/^#/ && /^([^=]+)=(.*)$/) {
$_[1]->{$_[3] ? lc($1) : $1} = $2;
push(@{$_[2]}, $1) if ($_[2]);
}
elsif (!/\S/) {
push(@{$_[2]}, undef) if ($_[2]);
}
}
close(ARFILE);
return 1;
}
# write_file(file, array)
# Write out the contents of an associative array as name=value lines
sub write_file
{
local(%old, @order);
&read_file($_[0], \%old, \@order);
open(ARFILE, ">$_[0]");
foreach $k (@order) {
if (!defined($k)) {
print ARFILE "\n";
}
elsif (exists($_[1]->{$k})) {
print ARFILE $k,"=",$_[1]->{$k},"\n";
}
}
foreach $k (keys %{$_[1]}) {
print ARFILE $k,"=",$_[1]->{$k},"\n" if (!exists($old{$k}));
}
close(ARFILE);
}
sub read_file_contents
{
open(FILE, $_[0]) || return undef;
local $/ = undef;
local $rv = <FILE>;
close(FILE);
return $rv;
}
sub write_file_contents
{
open(FILE, ">$_[0]") || return undef;
print FILE $_[1];
close(FILE);
}