Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

翻译 xlf 文件

License

NotificationsYou must be signed in to change notification settings

foyoux/docts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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}")

具体流程

请参阅旧文档 (仅作参考)。


[8]ページ先頭

©2009-2025 Movatter.jp