从 Xcode 导出 XLIFF 时,如何排除虚拟字符串?

When exporting XLIFF from Xcode, how to exclude dummy strings?

我正在使用 Xcode 的 Editor > Export For Localization... 导出 XLIFF 文件进行翻译 但是 Main.storyboard 的翻译包含很多不必要的字符串,大部分 placeholders/dummies 在设计时很有用。

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

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

我写了一个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.

这是适用于最新 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