解决webalizer 汉化后图表中乱码的问题

代码如下(webalizer_patch.pl):

  1. #!/usr/bin/perl
  2. ###############################################################################
  3. # webalizer_patch.pl
  4. # 原作:http://mail.tses.tcc.edu.tw/nuke/sections.php?op=viewarticle&artid=139
  5. # 修改:陶然 <taoran@taoran.net>
  6. # 功能: 解决HTML汉化后图表中乱码的问题
  7. # 方法:图表中乱码改用英文显示就正常了
  8. #       需要修改: graphs.c output.c webalizer_lang.h lang.h
  9. #
  10. # tar zxf webalizer-2.01-10-src.tgz
  11. # cd webalizer-2.01-10
  12. # ./configure --with-language=simplified_chinese
  13. # perl /path/of/webalizer_patch.pl ./ 
  14. # make
  15. # make install
  16. #
  17. ###############################################################################
  18. $file_graph    = "$ARGV[0]/graphs.c";
  19. if(!(-e $file_graph))   {
  20.     print "error open  $file_graph 
  21. ";
  22.     print "Usage:
  23.   perl $0  DIR
  24. ";
  25. #DIR是源文件目录,比如 ./
  26.     exit;
  27. }
  28.  
  29. #把图表中的月份和注释改成英文
  30. @old_var    = ("msg_h_hits",
  31.         "msg_h_pages",
  32.         "msg_h_visits",
  33.         "msg_h_files",
  34.         "msg_h_sites",
  35.         "msg_h_xfer",
  36.         "s_month");
  37.  
  38. @new_var    = ("msg_gh_hits",
  39.         "msg_gh_pages",
  40.         "msg_gh_visits",
  41.         "msg_gh_files",
  42.         "msg_gh_sites",
  43.         "msg_gh_xfer",
  44.         "s_gmonth");
  45.  
  46. open(IN,"<$file_graph");
  47. @lines    = <IN>;
  48. $size    = @lines;
  49. close(IN);
  50.  
  51. `mv $file_graph $file_graph.bak`;
  52. open(OUT,">$file_graph");
  53. for($i=0;$i<$size;$i++)   {
  54.     for($k=0;$k<@old_var;$k++)   {
  55.         if($lines[$i] =~ s/$old_var[$k]/$new_var[$k]/g)   {
  56.             print "$i    $old_var[$k] -> $new_var[$k]
  57. ";
  58.             print OUT "/* --- Modified --- */
  59. ";
  60.         }
  61.     }
  62.     print OUT $lines[$i];
  63. }
  64. close(OUT);
  65.  
  66. #把图表中的标题改成英文显示
  67. $file_output    = "$ARGV[0]/output.c";
  68. open(IN,"<$file_output");
  69. @lines    = <IN>;
  70. $size    = @lines;
  71. close(IN);
  72. `mv $file_output $file_output.bak`;
  73. open(OUT,">$file_output");
  74. for($i=0;$i<$size;$i++)   {
  75.     if($lines[$i] =~ s/msg_hmth_du,l_month/"Daily Usage for",s_gmonth/xg) {
  76.         print "$i    patched
  77. ";
  78.         print OUT "/* --- Modified --- */
  79. ";
  80.     } elsif($lines[$i] =~ s/msg_hmth_hu,l_month/"Hourly Usage for",s_gmonth/xg) {
  81.         print "$i    patched
  82. ";
  83.         print OUT "/* --- Modified --- */
  84. ";
  85.     } elsif($lines[$i] =~ s/msg_ctry_use,l_month/"Usage by Country for",s_gmonth/xg) {
  86.         print "$i    patched
  87. ";
  88.         print OUT "/* --- Modified --- */
  89. ";
  90.     } elsif($lines[$i] =~ s/msg_main_us/"Usage summary for"/xg) {
  91.         print "$i    patched
  92. ";
  93.         print OUT "/* --- Modified --- */
  94. ";
  95.     }
  96.     print  OUT $lines[$i];
  97. }
  98. close(OUT);
  99.  
  100. #加入新的函数给前面显示的程序用
  101.  
  102. print "
  103. patch    $ARGV[0]/webalizer_lang.h
  104. ";
  105.  
  106. $file_lang    = "$ARGV[0]/webalizer_lang.h";
  107. open(OUT,">>$file_lang");
  108. print OUT qq~
  109. /* --- start Add  ---*/
  110. char *msg_gh_hits  = "Hits";
  111. char *msg_gh_pages = "Pages";
  112. char *msg_gh_visits= "Visits";
  113. char *msg_gh_files = "Files";
  114. char *msg_gh_sites = "Sites";
  115. char *msg_gh_xfer  = "KBytes";
  116.  
  117. char *s_gmonth[12]={ "Jan", "Feb", "Mar",
  118.                     "Apr", "May", "Jun",
  119.                     "Jul", "Aug", "Sep",
  120.                     "Oct", "Nov", "Dec"};
  121.  
  122. /* -------------End------------- */
  123. ~;
  124.  
  125. #补充声明这些新加的函数
  126. print "
  127. patch    $ARGV[0]/lang.h
  128. ";
  129.  
  130. $file_lang    = "$ARGV[0]/lang.h";
  131. open(OUT,">>$file_lang");
  132. print OUT qq~
  133. /* --- start Add  ---*/
  134. extern char *msg_gh_hits  ;
  135. extern char *msg_gh_pages ;
  136. extern char *msg_gh_visits;
  137. extern char *msg_gh_files ;
  138. extern char *msg_gh_sites ;
  139. extern char *msg_gh_xfer  ;
  140. extern char *s_gmonth[12];
  141. /* -------------End------------- */
  142. ~;
  143.  
  144. print "Done ^_^
  145. ";
  146.  
  147. #############################################################################
Tags: linux, perl, webalizer, 乱码

相关日志

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

还没有评论。

发表评论

(必填)

(必填)


*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Comment moderation is enabled. Your comment may take some time to appear.