美文网首页
Localizations及Iconfont使用python脚本

Localizations及Iconfont使用python脚本

作者: 歌手的剑 | 来源:发表于2017-05-03 11:31 被阅读19次

    "project - info - localizations"增加相关配置

    "project - info - localizations"

    创建.strings文件

    create .strings

    勾上需要支持的语言

    选择语言

    可以分别将两个文件夹内的.strings文件拖入到xcode中,其会自动生成带下箭头的样式:

    1 2

    使用脚本"conversion.py"抓取文案:

    import sys
    import time
    import requests
    
    def get_current_time():
        return time.strftime('%m-%d', time.localtime(time.time()))
    
    def write_time_to_file(file):
        file.write('\n//Update Time: %s\n' % get_current_time())
    
    def replace_placeholder(string):
        return string.strip().replace('{0}', '%1$@').replace('{1}', '%2$@')
    
    def replace_iconfont(string):
        return string.strip().lstrip('&#x')
    
    def new_start():
        reload(sys)
        sys.setdefaultencoding('utf-8')
    
        token = "api-miwso27zjqvymtorywghlesklpxj"
    
        request = requests.post("地址", {
            "api.token": token,
            "slug": "/product_and_design/translation/hexa_app_translation/",
        }, verify=False)
        content = request.json()["result"]["content"].split("\n")
    
        enfile = open('./en.lproj/Localizable.strings', 'w')
        zhfile = open('./zh-Hans.lproj/Localizable.strings', 'w')
        iconfile = open('./rota/Extension/String+Iconfont.swift', 'w')
    
        write_time_to_file(enfile)
        write_time_to_file(zhfile)
        write_time_to_file(iconfile)
        iconfile.write('import Foundation\n\nextension String {\n\n')
    
        content = content[2:]
        exists = {}
        for x in content:
            v = x.split("|")
            if len(v) <= 1:
                continue
            if v[2] in exists:
                continue
            exists[v[2]] = True
    
            key = v[2].strip()
    
    
    
            if key.startswith('iconfont'):
                value = '"\\u{%s}"' % v[3].strip().lstrip('&#x')
                iconfile.write('    static var %s: String {\n\
            return %s\n\
        }\n\n' % (key, value))
            else:
                envalue = '"%s"' % replace_placeholder(v[3].replace('"', '\\"'))
                chvalue = '"%s"' % replace_placeholder(v[4])
                enfile.write('"%s" = %s;\n' % (key, envalue))
                zhfile.write('"%s" = %s;\n' % (key, chvalue))
    
    
        # print "\"%s\" = \"%s\";" % (v[2].strip( ),v[4].strip( ))
        iconfile.write('}\n')
        enfile.close()
        zhfile.close()
        iconfile.close()
        print 'Success!, Here are %s keys.' % bytes(len(content))
    
    new_start()
    

    替换iconfont需要先替换项目中的iconfont.ttf文件

    相关文章

      网友评论

          本文标题:Localizations及Iconfont使用python脚本

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