用户名 密码 记住我 还未注册?

文章管理 - xoops语言自动编码转换的工具

文章管理 :: Xoops 模块 :: 模块其他

xoops语言自动编码转换的工具


http://xoops.org.cn/modules/newbb/viewtopic.php?topic_id=7759
原作者:yetist
近来在学习Python,为些特写了此工具用来对xoops的语言文件自动进行编码转换,目前能自动判断schinese和schineseutf目录,如果只有其中一个,那么会自动生成另一种编码。即对gb2312和utf8自动查找并转换。
运行效果:
如果你的某个目录只有一种语言:如简体中文(gb2312)schinese,那么它会自动转换并生成另一个utf编码的目录schineseutf
如果你的某个目录只有utf编码的目录schineseutf,那么它会自动转换并生成另一个简体中文编码(gb2312)的目录schinese.
如果某个目录中这两个目录都存在,它会显示all,并处理下一个。
如果某个目录中没有这两个目录,它会显示None,并处理下一个(如:class/xoopseditor/koivi/language目录不会补处理)。
运行条件:linux+python2.4及以上,2.3中没有cp936编码。
运行方法:请将以下代码下载并保存在xoops_root目录下autoconver.py,然后更改属性为可执行:chmod u+x autoconver.py
运行:./autoconver.py
本人只进行过一部分测试,没有进行全面测试,所以请大家在使用过程中遇到什么问题及时提出,本人再做改进。

提醒一点:用此工具完成转换之后,请手动修改一下xoops_root/language/xxxxxx/global.php文件中的编码设置,程序在此没有处理。改天有时间再完善。

附源代码:
如果你有兴趣,请完善并重新发布它,呵呵。

#!/usr/bin/env python2.4
# -*- encoding: utf8 -*-

# Filename: autoconver.py
# Discripion: 
# Author(s): yetist
# Version: 0.2

import sys
import os
import time

def find_lang_dir
():
     for 
rootdirsfiles in os.walkos.getcwd()):
        for 
dir in dirs:
            
path os.path.joinrootdir )
            
curdirname=root.split("/")[-1:][0]
            if 
curdirname=="language" :
                
process_lang(root)
                
time.sleep(1)


def process_lang(dir=None):
    print 
"process dir: %s" dir
    schinese
=False
    schineseutf
=False
    
for i in os.listdir(dir):
        if 
=="schinese":
            
schinese=True
        
if =="schineseutf":
            
schineseutf=True

    
if schinese == True and schineseutf == True:
        print 
"all"
        
return
    if 
schinese == False and schineseutf == True:
        print 
"translate schineseutf to schinese"
        
convert_dir_to_dir(os.path.join(dir,"schineseutf"),os.path.join(dir,"schinese"),"zh_utf","zh_cn")
        return
    if 
schinese == True and schineseutf == False:
        print 
"translate schinese to eschineseutf"
        
convert_dir_to_dir(os.path.join(dir,"schinese"),os.path.join(dir,"schineseutf"),"zh_cn","zh_utf")
        return
    if 
os.path.isdir(os.path.join(dir,i)):
        print 
">>>find other language in this dir: %s" i
    
print "None"

def convert_dir_to_dir(srcdir,dstdir,fcode,tocode):  
    
time.sleep(1)
    if 
not os.path.exists(dstdir):
        
os.mkdir(dstdir)
    for 
rootdirsfiles in os.walk(srcdir):
        for 
dir in dirs:
            
ndir=os.path.join(root,dir)
            
dirpath=ndir.replace(srcdir,"")[1:]
            
ndstdir os.path.join(dstdir,dirpath)
            if 
not os.path.exists(ndstdir):
                
os.mkdir(ndstdir)
    for 
rootdirsfiles in os.walk(srcdir):
        for 
file in files:
            
srcfile os.path.joinrootfile )
            
dpath=srcfile.replace(srcdir,"")[1:]
            
dstfile os.path.join(dstdir,dpath)
            print 
">>>>>>>nconvert start [from %s ]:%s" % (fcode,srcfile)
            print 
"<<<<<<<nconvert end.  [to   %s ]:%s" % (tocode,dstfile)
            
time.sleep(1)
            
file_trans(srcfile,dstfile,fcode,tocode)

def file_trans(srcfile,dstfile,fcode="zh_cn",tocode="zh_utf"):
    
transcode={"zh_cn":"cp936","zh_tw":"big5","zh_utf":"utf-8"}
    
fp=open(srcfile,"r")
    
str=fp.read()
    
fp.close()
    
obj=unicode(str,transcode.get(fcode)).encode(transcode.get(tocode))
    
fp=open(dstfile,"w+")
    
fp.write(obj)
    
fp.close()


if 
__name__ == "__main__":
    
find_lang_dir()
<< 好用的模組TadNews 新聞+電子報模組 1.2.7 版 XOOPS 和 Discuz 论坛简易用户同步代码 >>
跟踪网址
  • 文章地址: http://xoops.org.cn/modules/article/view.article.php/c18/76
  • 跟踪地址: http://xoops.org.cn/modules/article/trackback.php/76
API: 工具箱 短消息 Email PDF 书签 打印 | RSS | RDF | ATOM
Copyright© shawnf69 & XOOPS CHINA
网友个人意见,不代表本站立场。对于发言内容,由发表者自负责任。
发表者 树状展开
msryat
发表日期: 11月05日 22:30  更新: 11月05日 22:30
中级会员
注册日期: 11月05日
来自: ali
发表总数: 63
 评论: xoops语言自动编码转换的工具