格式化安卓字符串资源

格式化安卓字符串资源 每种语言按照默认的行数对应起来 每个资源必须单独一行 只支持String 不支持数组

使用方法:

1.放在res同目录下
2.在translate_file_name中append需要格式化的文件名

代码如下 格式及其不规范

# coding=utf-8
import fileinput
import os
import re
from xml.etree.ElementTree import ElementTree

RES = "res\\"
DEFAULT_LANGUAGE = "values"


# 通过xml格式读取默认的语言格式
def read_xml(in_path):
    try:
        tree = ElementTree()
        tree.parse(in_path)
        return tree
    except BaseException:
        return None


# 获取其他语言对应的字典
def get_dict_from_other_language(file_path):
    other_language_dict = {}
    is_start_parser_string = False
    head_content = ""
    for line in fileinput.input(file_path):
        _pattern = r"name=\"" + "\w*\""
        pattern = re.compile(_pattern)
        result = pattern.findall(line)
        if len(result) >= 1:
            if not is_start_parser_string:
                is_start_parser_string = True
            name = result[0][6:-1]
            other_language_dict[name] = line
        else:
            if not is_start_parser_string:
                head_content += line
    fileinput.close()
    return head_content, other_language_dict


# 获取其他语言对应的字符串
def get_string_for_other_language(other_language_dict, tree_default, head_content):
    result = head_content
    for node in tree_default.getroot():
        node_name = node.attrib['name']
        if node_name in other_language_dict:
            result += other_language_dict[node_name]
        else:
            result += ("    <!-- " + node_name + " not translate -->\n")
    result += "</resources>"
    return result


# 写文件
def write_file(content, path):
    fp = open(path, mode="w")
    try:
        fp.write(content)
    except BaseException:
        return None
    finally:
        fp.close()


# 校验语言格式是否匹配
def check_other_language(default_language_path, other_language_path):
    default_language_dict = {}
    default_index = 0
    for line in fileinput.input(default_language_path):
        _pattern = r"name=\"" + "\w*\""
        pattern = re.compile(_pattern)
        result = pattern.findall(line)
        if len(result) >= 1:
            name = result[0][6:-2]
            default_language_dict[default_index] = name
            default_index += 1
    fileinput.close()

    other_language_dict = {}
    other_index = 0
    for line in fileinput.input(other_language_path):
        _pattern = r"name=\"" + "\w*\""
        pattern = re.compile(_pattern)
        result = pattern.findall(line)
        if len(result) >= 1:
            name = result[0][6:-1]
            other_language_dict[other_index] = name
            other_index += 1
    fileinput.close()

    default_len = len(default_language_dict)
    other_len = len(other_language_dict)
    if default_len != other_len:
        print other_language_path + " error!!!"
        return
    for index in range(default_len):
        if cmp(default_language_dict[index], other_language_dict[index]) != 0:
            print default_language_dict[index], other_language_dict[
                index], other_language_path + " error!!!"
            return
    print other_language_path, "succeed"
    return


# 格式化安卓字符串资源 每种语言按照默认的行数对应起来  每个资源必须单独一行
# 只支持String 不支持数组
translate_file_name = []
translate_file_name.append("strings.xml")


def format_android_string():
    # 1. 通过xml格式读取默认语言的xml文件
    # 2. 通过读文件方式读取其他语言的xml文件(通过正则表达式提取name属性/通过xml节点方式获取name属性) 生成name属性和该行的map数据对象
    # 3. 根据name属性 从map数据对象中获取该行
    # 4. 将读出来的该行 拼接到已经有的资源字符串后
    # 5. 遍历完全部 生成文件
    for fileName in translate_file_name:
        default_language_path = RES + DEFAULT_LANGUAGE + "\\" + fileName
        tree_default = read_xml(default_language_path)
        if tree_default is None: continue
        other_language_dir = os.listdir(RES)
        for otherLanguageFile in other_language_dir:
            if otherLanguageFile.startswith(
                    "values") and not otherLanguageFile == DEFAULT_LANGUAGE:
                other_language_path = RES + otherLanguageFile + "\\" + fileName
                head_content, other_language_dict = get_dict_from_other_language(
                    other_language_path)
                string_result = get_string_for_other_language(other_language_dict,
                                                              tree_default, head_content)
                write_file(string_result, other_language_path)
                check_other_language(default_language_path, other_language_path)


if __name__ == "__main__":
    format_android_string()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值