- Notifications
You must be signed in to change notification settings - Fork5
foyoux/docts
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
docts
是一个利用 pygtrans 库翻译和处理 XLF 文件的 Python 包。
- XLF 文件解析:提取 XLF 文件中的源文本。
- 文本过滤:通过自定义过滤器,排除不需要翻译的内容。
- 文本映射:替换特定符号或字符串。
- 批量翻译:利用 Google 翻译 API 进行批量翻译。
- XLF 文件写入:将翻译结果写回新的 XLF 文件。
你可以通过以下几种方式安装docts
:
pip install -U doctspip install git+ssh://git@github.com/foyoux/docts.gitpip install git+https://github.com/foyoux/docts.git
docts
主要用于将 XLF 文件中的内容提取、翻译并重新保存。以下是一个基本的使用示例:
frompygtransimportTranslatefromdoctsimportDoctsif__name__=='__main__':# 创建翻译客户端,指定代理(如有需要)translator=Translate(proxies={'https':'http://localhost:10809'})# 初始化 Docts 实例,指定 XLF 文件路径和翻译客户端doc=Docts('<xlf_file_path>',translator)# 过滤无效或不需要翻译的文本(可选)doc.add_filter(lambdaword:'filter_condition'inword)# 执行翻译并保存结果,翻译后的 XLF 文件将保存在同一目录下translated_file_path=doc.save_words()print(f"翻译完成,结果保存至:{translated_file_path}")
你可以通过添加不同的过滤器来定制处理流程。例如,过滤掉所有包含等号(=
)的行:
doc.add_filter(lambdaword:'='inword)
支持通过正则表达式过滤文本。例如,过滤所有以error
开头的字符串:
importredoc.add_contain_filter(re.compile(r'^error'))
将特定符号映射为其他符号,例如将•
替换为●
:
doc.add_map(lambdaword:word.replace('•','●'))
如果想要撤销之前的过滤操作,可以使用reset()
方法恢复原始文本列表:
doc.reset()
除了保存翻译结果外,你还可以将被过滤掉的文本单独保存:
ignored_file_path=doc.save_ignores()print(f"忽略的文本已保存至:{ignored_file_path}")
请参阅旧文档 (仅作参考)。