【问题标题】:When exporting XLIFF from Xcode, how to exclude dummy strings?从 Xcode 导出 XLIFF 时,如何排除虚拟字符串?
【发布时间】:2022-05-13 15:43:59
【问题描述】:

我正在使用 Xcode 的 Editor > Export For Localization... 导出 XLIFF 文件进行翻译 但是 Main.storyboard 的翻译包含许多不必要的字符串,主要是在设计时有用的占位符/虚拟变量。

如何从 XLIFF 文件中排除此类字符串?

【问题讨论】:

  • Xcode 应该支持。犯罪缺失功能。
  • XLIFF 工作流程是在 Xcode 6 中引入的,从本地化中排除视图将是 UI 中的一个复选框,这很愚蠢。

标签: xcode scripting localization


【解决方案1】:

我写了一个 script 不包括某些翻译。

它是如何工作的?
命令行:python strip_loc.py input.xliff output.xliff exclude_list.txt [-v]

用法示例:
python strip_loc.py en.xliff en-stripped.xliff exclude_words.txt -v

exclude_list.txt 是一个每行一个字符串的文件。该脚本解析该列表并创建一个禁用词词典。如果遇到源包含这些字符串之一的翻译,则从输出 xml/xliff 中删除整个翻译单元。

【讨论】:

  • 可以自动化吗?例如,从 Xcode 导出基础语言时。
【解决方案2】:

对我来说,我使用这个XLIFF Online Editor 来编辑 xliff 文件。您很容易忽略虚拟文本或您需要的任何内容。

【讨论】:

  • 我刚刚试用了在线编辑器,但它可以帮助您翻译已导出的密钥。 XLIFF 文件将被发送给翻译人员,他们不知道哪些字符串是虚拟的,哪些不是。最好完全去掉这些字符串。
  • 也许您可以使用在线编辑器,在虚拟文本的翻译中输入关键字,例如“DUMMY”,然后将其导出并发送给翻译人员。译者会忽略它们。如果您从 XLIFF 文件中完全排除 Xcode,我担心 Xcode 在导入时会遇到问题
  • 我刚刚测试了当您从 xliff 文件中删除某些字符串时会发生什么。当您重新导入时,Xcode 会警告您缺少某些字符串,然后它会使用源字符串但全部大写以通知您这些字符串没有翻译。所以,对于虚拟文本来说,去掉这些 xliff 根本不是问题。
  • 我相信全大写是一个方案设置。
【解决方案3】:

这是适用于最新 python 版本的解决方案:

def log(string_to_log):
if args.verbose:
    print(string_to_log)
import argparse
parser = argparse.ArgumentParser(description="Process xliff file against banned words and output new xliff with stripped translation.", epilog="Example usage: strip_loc.py en.xliff en-stripped.xliff exclude_words.txt -v")
parser.add_argument('source', help="Input .xliff file containing all the strings")
parser.add_argument('output', help="Output .xliff file which will containt the stripped strings according to the exclude_list")
parser.add_argument('exclude_list', help="Multi-line text file where every line is a banned string")
parser.add_argument('-v', '--verbose', action="store_true", help="print script steps while working")
args = parser.parse_args()
banned_words = [line.strip().lower() for line in open(args.exclude_list, 'r')]
log("original file: " + args.source)
log("output file: " + args.output)
log("banned words: " + ", ".join(banned_words))
log("")
import xml.etree.ElementTree as ET
ET.register_namespace('',"urn:oasis:names:tc:xliff:document:1.2")
ns = {"n": "urn:oasis:names:tc:xliff:document:1.2"}
with open(args.source, 'r') as xml_file:
    tree = ET.parse(xml_file)
root = tree.getroot()
counter = 1
for file_body in root.findall("./*/n:body", ns):
    for trans_unit in file_body.findall("n:trans-unit", ns):
        source = trans_unit.find("n:source", ns)
        if source.text is not None:
            source = source.text.encode("utf-8").lower()
            source = source.decode("utf-8")
            source = source.strip()
            for banned_word in banned_words:
                if source.find(banned_word) != -1:
                    log(str(counter) + ": removing <trans-unit id=\"" + trans_unit.attrib['id'] + "\">, banned: \"" + banned_word + "\"")
                    file_body.remove(trans_unit)
                    break
                    counter += 1
tree.write(args.output, "utf-8", True)
log("")
print("DONE")

和用法一样:

python strip_loc.py en.xliff en-stripped.xliff exclude_words.txt -v

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-29
    • 1970-01-01
    • 2015-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多