美文网首页ARN-MJ
筛选本地化字符串批量替换翻译

筛选本地化字符串批量替换翻译

作者: T_aa | 来源:发表于2019-05-13 13:59 被阅读0次

    用python从项目工程中找出含中文的字符串

    定义宏 LS(s)

    #define LS(s)     NSLocalizedString(s, nil)
    

    定义python目录配置文件 PathConfig.py

    # -*- coding: utf-8 -*-
    
    import os
    
    # 文件路径
    # rub_filepath = '../RubbishCodeDemo'
    rub_filepath = '../../Storybook/Storybook'
    
    localstring_csv_filepath = 'localstring.csv'
    

    创建文件 regularReplace.py
    通过他找到项目中的所有的含中文字符串
    并生成一个 localstring.csv 的语言对应配置表

    第一行是 csv 读取的时候 需要取出的key信息

    ['localstringKey','1','2','3',..]
    

    第一列统一中文key 对应的key:'localstringKey'
    第二列 对应的原来中文内容 对应的key:'1'
    第三 第四 可以后自己添加对应语言的翻译 对应的key:'2','3','4',.....

    
    # -*- coding: utf-8 -*-
    import os
    import time
    import random
    import PathConfig
    import re
    import csv
    
    
    # 正则查找替换中文字符串
    
    # 排重
    csvKeyList = []
    csvList = []
    
    def csvWrite():
        # csv文件读写
        csvFile = open(PathConfig.localstring_csv_filepath, 'w')
        csv_writer = csv.writer(csvFile)
    
        csv_writer.writerow(['localstringKey','1'])
        csv_writer.writerow(['语言','中文'])
        
        for csvDic in csvList:
            key = csvDic['key']
            value = csvDic['value']
            csv_writer.writerow([key,value])
    
        csvFile.close()
    
    
    def replaceStr(content, oldstr, newstr):
    
        replacements = {oldstr:newstr}
        for src, target in replacements.iteritems():
                content = content.replace(src, target)
    
        return content
    
    
    
    def traverse(file_dir):
        
    
        fs = os.listdir(file_dir)
        for dir in fs:
            if dir.startswith('.'):
                #过滤隐藏
                continue
    
            tmp_path = os.path.join(file_dir, dir)
            if not os.path.isdir(tmp_path):
    
                if not dir.endswith('.h') and not dir.endswith('.m'):
                    continue
    
                with open(tmp_path) as f:
                    file_str = f.read()
                    
                    content = file_str.decode('utf-8')
                    pat = u'(@"[^"]*[\u4E00-\u9FA5]+[^"\n]*?")'
                    results = re.findall(pat,content)
                    for s in results:
                        sn_str = s.encode('utf-8')
                        new_sn_str = 'LS(%s)'%(sn_str)
                        # 字符串替换
                        file_str = file_str.replace(new_sn_str,sn_str)
                        file_str = file_str.replace(sn_str,new_sn_str)
    
                        sn_s = sn_str[2:-1]
    
                        if sn_s not in csvKeyList:
                            csvKeyList.append(sn_s)
                            csvList.append({'key':sn_s,'value':sn_s})
    
                        print '"%s" = "%s";' % (sn_s,sn_s)
    
                with open(tmp_path, 'w') as f:
                    f.write(file_str)
    
            else:
                # 是文件夹,则递归调用
                traverse(tmp_path)
    
    def main():
        traverse(PathConfig.rub_filepath)
        csvWrite()
        print('Finish done')
    
    if __name__ == '__main__':
        main()
    
    
    
    
    

    创建 CSVLocalStringRead.py 读取本地出来好的本地化语言csv文件
    打印中对应语言的 key 和 value 映射
    对应复制到项目的Localizable.strings文件中

    # -*- coding: utf-8 -*-
    import os
    import csv
    import PathConfig
    import re
    
    
    def main():
        
    
        csvFile = open(PathConfig.localstring_csv_filepath, 'r')
        
        csv_list = []
    
        csv_reader = csv.DictReader(csvFile)
        rowCount = len(csv_reader.fieldnames)
                
        for row in csv_reader:
            csv_list.append(row)    
    
        csvFile.close()
    
    
    
    
        for i in range(rowCount):
            if i == 0:
                continue
    
            print '\n\n\n\n\n\n\n\n  ================================  %s \n\n\n\n\n\n' % str(i)
            
            for row in csv_list:
                
                index = str(i)
                key_s = row['localstringKey']
                value_s = row[index]
    
                oc_s = '"%s" = "%s";' % (key_s,value_s)
                print oc_s
        
        print('Finish done')
    
    if __name__ == '__main__':
        main()
    
    

    相关文章

      网友评论

        本文标题:筛选本地化字符串批量替换翻译

        本文链接:https://www.haomeiwen.com/subject/gnplaqtx.html