Handle hostnames with upper-case letters
[webmin.git] / gb-to-big5.pl
1 #!/usr/local/bin/perl
2 # Converts a module's zh_CN (simplified chinese) strings to zh_TW.Big5
3 # (traditional)
4
5 use Encode::HanConvert;
6
7 foreach $m (@ARGV) {
8         # Convert lang file
9         -d "$m/lang" || die "$m is not a module directory";
10         local %zh;
11         &read_file("$m/lang/zh_CN", \%zh);
12         foreach $k (keys %zh) {
13                 $zh{$k} = gb_to_big5($zh{$k});
14                 }
15         &write_file_diff("$m/lang/zh_TW.Big5", \%zh);
16
17         # Translate the module.info file
18         local %minfo;
19         &read_file("$m/module.info", \%minfo);
20         local %ominfo = %minfo;
21         if ($minfo{'desc_zh_CN'}) {
22                 $minfo{'desc_zh_TW.Big5'} = gb_to_big5($minfo{'desc_zh_CN'});
23                 &write_file_diff("$m/module.info", \%minfo);
24                 }
25
26         # Translate the config.info file
27         local %cinfo;
28         if (&read_file("$m/config.info.zh_CN", \%cinfo)) {
29                 local %ocinfo = %cinfo;
30                 foreach $k (keys %cinfo) {
31                         $cinfo{$k} = gb_to_big5($cinfo{$k});
32                         }
33                 &write_file_diff("$m/config.info.zh_TW.Big5", \%cinfo);
34                 }
35
36         # Translate any help files
37         opendir(DIR, "$m/help");
38         foreach $h (readdir(DIR)) {
39                 if ($h =~ /(\S+)\.zh_CN\.html$/) {
40                         open(IN, "$m/help/$h");
41                         open(OUT, ">$m/help/$1.zh_TW.Big5.html");
42                         while(<IN>) {
43                                 print OUT gb_to_big5($_);
44                                 }
45                         close(OUT);
46                         close(IN);
47                         }
48                 }
49         closedir(DIR);
50         }
51
52 # read_file(file, &assoc, [&order], [lowercase])
53 # Fill an associative array with name=value pairs from a file
54 sub read_file
55 {
56 open(ARFILE, $_[0]) || return 0;
57 while(<ARFILE>) {
58         s/\r|\n//g;
59         if (!/^#/ && /^([^=]+)=(.*)$/) {
60                 $_[1]->{$_[3] ? lc($1) : $1} = $2;
61                 push(@{$_[2]}, $1) if ($_[2]);
62                 }
63         }
64 close(ARFILE);
65 return 1;
66 }
67  
68 # write_file_diff(file, array)
69 # Write out the contents of an associative array as name=value lines
70 sub write_file_diff
71 {
72 local(%old, @order);
73 &read_file($_[0], \%old, \@order);
74 return if (!&diff(\%old, $_[1]));
75 open(ARFILE, ">$_[0]");
76 foreach $k (@order) {
77         print ARFILE $k,"=",$_[1]->{$k},"\n" if (exists($_[1]->{$k}));
78         }
79 foreach $k (keys %{$_[1]}) {
80         print ARFILE $k,"=",$_[1]->{$k},"\n" if (!exists($old{$k}));
81         }
82 close(ARFILE);
83 print "Wrote $_[0]\n";
84 }
85
86 sub diff
87 {
88 if (scalar(keys %{$_[0]}) != scalar(keys %{$_[1]})) {
89         return 1;
90         }
91 foreach $k (keys %{$_[0]}) {
92         if ($_[0]->{$k} ne $_[1]->{$k}) {
93                 return 1;
94                 }
95         }
96 return 0;
97 }