2 # Convert words in lang/en files from UK to US spelling.
3 # Create lang/en_GB files containing words that are different.
5 if ($ARGV[0] eq "--svn" || $ARGV[0] eq "-svn" ||
6 $ARGV[0] eq "--git" || $ARGV[0] eq "-git") {
11 chdir("/usr/local/webadmin");
18 foreach $d (readdir(DIR)) {
19 push(@modules, $d) if (-r "$d/module.info");
25 open(MAPPING, "english-mappings.txt") ||
26 die "Failed to open english-mappings.txt";
30 my ($us, $uk) = split(/\t+/, $_);
32 push(@us_mappings, [ $us, $uk ]);
36 @uk_mappings = map { [ $_->[1], $_->[0] ] } @us_mappings;
37 print STDERR "Found ",scalar(@uk_mappings)," mappings\n";
39 # Do all the given modules
41 foreach $m (@modules) {
42 print STDERR "Doing module $m\n";
43 push(@rv, &fix_english_file("$m/lang/en", "$m/lang/en_GB", 1));
44 push(@rv, &fix_english_file("$m/config.info",
45 "$m/config.info.en_GB", 1));
46 opendir(HELP, "$m/help");
47 foreach $h (readdir(HELP)) {
48 if ($h =~ /^([^\.]+)\.html$/) {
49 push(@rv, &fix_english_file("$m/help/$h",
50 "$m/help/$1.en_GB.html", 0));
56 # Print and commit the files
60 ($dir, $rest) = split(/\//, $f, 2);
61 system("cd $dir ; git add $rest ; git commit -m '$svn' $rest ; git push");
67 local ($us, $uk, $linefmt) = @_;
68 return ( ) if (!-r $us);
71 # Webmin = separated line file
73 # First fix up any UK spellings in the US file
75 &read_file($us, \%uslines);
77 foreach my $k (keys %uslines) {
79 $usv = &convert_to_us($v);
86 &write_file($us, \%uslines);
90 # Then create a UK file with only lines that need changing
92 &read_file($uk, \%uklines);
94 foreach my $k (keys %uslines) {
96 $ukv = &convert_to_uk($v);
97 if ($ukv ne $v && $uklines{$k} ne $ukv) {
103 &write_file($uk, \%uklines);
110 # First fix up any UK spellings in the US file
111 local $ustext = &read_file_contents($us);
112 $usv = &convert_to_us($ustext);
113 if ($usv ne $ustext) {
114 &write_file_contents($us, $usv);
118 # Then create a UK file
119 $uktext = &read_file_contents($uk);
120 $ukv = &convert_to_uk($usv);
121 if ($uktext ne $ukv && $ukv ne $usv) {
122 &write_file_contents($uk, $ukv);
132 return &convert_mapping($str, \@uk_mappings);
138 return &convert_mapping($str, \@us_mappings);
143 local ($str, $fromto) = @_;
144 foreach my $w (@$fromto) {
145 my ($from, $to) = @$w;
146 $str =~ s/(\s|^)\Q$from\E(\s|$)/$1$to$2/g;
147 $from = ucfirst($from);
149 $str =~ s/(\s|^)\Q$from\E(\s|$)/$1$to$2/g;
154 # read_file(file, &assoc, [&order], [lowercase])
155 # Fill an associative array with name=value pairs from a file
158 open(ARFILE, $_[0]) || return 0;
161 if (!/^#/ && /^([^=]+)=(.*)$/) {
162 $_[1]->{$_[3] ? lc($1) : $1} = $2;
163 push(@{$_[2]}, $1) if ($_[2]);
166 push(@{$_[2]}, undef) if ($_[2]);
173 # write_file(file, array)
174 # Write out the contents of an associative array as name=value lines
178 &read_file($_[0], \%old, \@order);
179 open(ARFILE, ">$_[0]");
180 foreach $k (@order) {
184 elsif (exists($_[1]->{$k})) {
185 print ARFILE $k,"=",$_[1]->{$k},"\n";
188 foreach $k (keys %{$_[1]}) {
189 print ARFILE $k,"=",$_[1]->{$k},"\n" if (!exists($old{$k}));
194 sub read_file_contents
196 open(FILE, $_[0]) || return undef;
203 sub write_file_contents
205 open(FILE, ">$_[0]") || return undef;