Python 脚本拉取多语言网络数据写入iOS工程

场景描述:为了国际化市场 app需要支持20种国际语言,在工程中要使用多语言文本时,只需要输入对应key值,工程根据当前语言设置自动去语言文件中选择对应的语言文本。
此脚本一键拉取多语言平台上的全部国家的语言配置json数据(键值形式,一个键对应20个国家语言),通过脚本写入iOS工程,每次跑脚本都会全量覆盖原有语言文本。

上代码

# @Author yangzai
# 如果老key有改动 使用此脚本更新

import requests, json, os, sys, io, re

# 拉取最新多语言配置数据
def get_data(app_client, pagesize, pageno):
    base_url = "http://octopus.duolainc.com/api/language/item/list"
    url = base_url + "?pageSize=" + pagesize + "&app=" + app_client + "&pageNo=" + pageno
    response = requests.get(url)
    dic = json.loads(response.text)
    error_code = dic["errorCode"]
    if error_code != 0:
        print("下载失败 错误码errorCode=" + str(error_code))
        exit()
    else:
        value = dic["value"]
        lan_data = value["data"]
    return lan_data

def regex_replace(string):
    string = string.replace('{{app_name}}', 'Fordeal')
    string = string.replace('"', '\\"')
    # string = string.replace('%', '%%')
    value = re.sub('{{p.?}}', '%@', string, 0)
    return value


#移除老的多语言文件 重新创建空文件
def remove_old_files(translations):
    distLanDir = os.getcwd() + "/ECAME/Supporting Files/"
    for key in translations:
        url = ""
        dir_url = ""
        if key == "zh":
            url = distLanDir + key + "-Hans.lproj/Localizable.strings"
            dir_url = distLanDir + key + "-Hans.lproj"
        else:
            url = distLanDir + key + ".lproj/Localizable.strings"
            dir_url = distLanDir + key + ".lproj"

            # 先把以前的删掉
        if os.path.exists(url):
            os.remove(url)

        # 创建新的
        # 无文件夹时先创建文件夹 再创建文件
        if not os.path.isdir(dir_url):
            os.makedirs(dir_url)
        f = open(url, "a")
        f.close()

#写入老key
def write_to_string_pre(lan_data_pre):
    # 先删除原有文件 然后新建从头到尾添加key value
    have_removed_old_files = False
    distLanDir = os.getcwd() + "/ECAME/Supporting Files/"
    for item in lan_data_pre:
        translations = item["translations"]
        item_key = item["itemKey"]
        if translations is None:
            print(item_key + "没有值")
        else:
            if have_removed_old_files is False:
                have_removed_old_files = True
                remove_old_files(translations) #移除老的多语言文件 重新创建空文件

            #写入
            for key in translations:
                value = regex_replace(translations[key])
                url = ""
                if key == "zh":
                    url = distLanDir + key + "-Hans.lproj/Localizable.strings"
                else:
                    url = distLanDir + key + ".lproj/Localizable.strings"
                f = open(url, "a")
                f.write('"' + item_key + '"' + ' = "' + value + '";' + "\n")
                f.close()

#写入新key
def write_to_string(lan_data):
    distLanDir = os.getcwd() + "/ECAME/Supporting Files/"
    for item in lan_data:
        translations = item["translations"]
        item_key = item["itemKey"]
        if translations is None:
            print(item_key + "没有值")
        else:
            #写入
            for key in translations:
                value = regex_replace(translations[key])
                url = ""
                if key == "zh":
                    url = distLanDir + key + "-Hans.lproj/Localizable.strings"
                else:
                    url = distLanDir + key + ".lproj/Localizable.strings"
                f = open(url, "a")
                f.write('"' + item_key + '"' + ' = "' + value + '";' + "\n")
                f.close()

app = "client"
pageSize = "100"
pageNo = "1"
lan_data = get_data(app, pageSize, pageNo) #新配置 与android共用

app_pre = "iospre"
pageSize_pre = "1000"
pageNo_pre = "1"
lan_data_pre = get_data(app_pre, pageSize_pre, pageNo_pre) #老配置 ios 老key

if lan_data_pre is None:
    print("异常!!!iospre数据下载为空")
    if lan_data is None:
        print("异常!!!client数据下载为空")
    else:
        write_to_string(lan_data)
        print("更新完毕")
else:
    write_to_string_pre(lan_data_pre)
    if lan_data is None:
        print("异常!!!client数据下载为空")
    else:
        write_to_string(lan_data)
        print("更新完毕")


你可能感兴趣的:(Python 脚本拉取多语言网络数据写入iOS工程)