Python android 多国翻译提取整合工具

Posted AAA啊哈

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python android 多国翻译提取整合工具相关的知识,希望对你有一定的参考价值。

Python android 多国翻译提取整合工具

前提

使用了 BeautifulSoup, 我很懒

功能 1

提取 string.xml 资源文件的内容

<string name="app_name">App Name</string> -> App Name

功能 2

整合 翻译文件到源文件, 翻译文件每行代表一个翻译,不能有空行, 原理就是 按行替换资源文件的内容
<string name="app_name">App Name</string> + 应用名 -> <string name="app_name">应用名</string>

代码实现

from bs4 import BeautifulSoup

is_write_translate = False
source_path = "strings.xml"
source_string_path = "资源文件.txt"
translate_path = "xxxx.txt"
source_translate_path = "new_string.xml"

def write_to_file(path, text):
    with open(path, 'a', encoding='utf-8') as f:
        f.writelines(text + '\\n')

def get_xml_string_to_file():
    file = open(source_path, 'r', encoding='utf-8')
    soup = BeautifulSoup(file, "html.parser")
    soup.contains_replacement_characters = False
    string_tag = soup.find_all('string')
    for content in string_tag:
        write_to_file(source_string_path, content.string)

def write_translate_to_file():
    file = open(source_path, 'r', encoding='utf-8')
    soup = BeautifulSoup(file, "html.parser")
    soup.contains_replacement_characters = False
    string_tag = soup.find_all('string')
    write_to_file(source_translate_path, "<resources>")
    with open(translate_path, 'r', encoding='utf-8') as file_object:
        lines = file_object.readlines()
        i = 0
        line_len = len(lines)
        while i < line_len:
            s = lines[i]
            if i < line_len - 1:
                s = s[:-1]
            content = string_tag[i]
            content.string = s
            write_to_file(source_translate_path, str(content))
            i += 1
    write_to_file(source_translate_path, "</resources>")

if __name__ == '__main__':
    temp_source_path = input("程序未做判空处理,确保输入有效,请输入原始文件路径:\\n")
    if temp_source_path:
        source_path = temp_source_path
    flag = input("提取资源字符串请输入1, 整合翻译字符串请输入2: \\n")
    if flag == '1':
        is_write_translate = False
    elif flag == '2':
        is_write_translate = True
    else:
        is_write_translate = False

    if is_write_translate:
        temp_translate_path = input("请输入翻译文件路径(txt 格式,不能有空行):\\n")
        if temp_translate_path:
            translate_path = temp_translate_path
        temp_source_translate_path = input("请输入输出路径(xml):\\n")
        if temp_source_translate_path:
            source_translate_path = temp_source_translate_path
        write_translate_to_file()
    else:
        temp_source_string_path = input("请输入输出路径(txt):\\n")
        if temp_source_string_path:
            source_string_path = temp_source_string_path
        get_xml_string_to_file()

以上是关于Python android 多国翻译提取整合工具的主要内容,如果未能解决你的问题,请参考以下文章

Android国际化翻译多国String导入导出(EXCEL)实现

Android国际化翻译多国String导入导出(EXCEL)实现

求StressMyPC(电脑硬件压力测试工具) V4.11 多国语言绿色版网盘资源

Python爬虫实战之制作桌面翻译工具

求Enigma Virtual Box(单文件制作工具) V9.20 多国语言官方版网盘资源

QT实多国语言切换