翻译场景#
Manim Voiceover 支持将场景翻译成其他语言。它使用 gettext 约定,将可翻译字符串包装在 _()
中。gettext 是一个成熟的国际化和本地化(i18n 和 l10n)系统,常用于编写多语言程序。它允许您将 Manim 场景和所有翻译整齐地组织在同一个 git 仓库中。
请查看翻译示例
import os
from manim import *
from manim_voiceover import VoiceoverScene
from manim_voiceover.services.gtts import GTTSService
from manim_voiceover.translate import get_gettext
# It is good practice to get the LOCALE and DOMAIN from environment variables
LOCALE = os.getenv("LOCALE")
DOMAIN = os.getenv("DOMAIN")
# The following function uses LOCALE and DOMAIN to set the language, and
# returns a gettext function that is used to insert translations.
_ = get_gettext()
class TranslationExample(VoiceoverScene):
def construct(self):
# We use the free TTS service from Google Translate
# The TTS language is set via the LOCALE environment variable
self.set_speech_service(GTTSService(lang=LOCALE))
circle = Circle()
square = Square().shift(2 * RIGHT)
# The voiceover strings are wrapped in _()
# This means that their translations will be looked up in the .po files
# and used to replace the original strings
with self.voiceover(text=_("This circle is drawn as I speak.")) as tracker:
self.play(Create(circle), run_time=tracker.duration)
with self.voiceover(text=_("Let's shift it to the left 2 units.")) as tracker:
self.play(circle.animate.shift(2 * LEFT), run_time=tracker.duration)
with self.voiceover(
text=_("Now, let's transform it into a square.")
) as tracker:
self.play(Transform(circle, square), run_time=tracker.duration)
with self.voiceover(text=_("Thank you for watching.")):
self.play(Uncreate(circle))
self.wait()
在此示例中,德语和越南语翻译分别位于文件 locale/de/LC_MESSAGES/translation-example.po 和 locale/vi/LC_MESSAGES/translation-example.po 中。
我们引入了命令行工具 manim_render_translation
,以便更轻松地渲染翻译。它类似于调用 manim render
,但它还允许您选择要渲染的语言环境(即语言)。
# Assuming you are in the root directory of manim-voiceover repo
cd examples/translation-example
manim_render_translation translation-example.py \
-s TranslationExample \
-d translation-example \
-l de,vi \
-qh
其中,
-s
:要渲染的场景。-d
:gettext 域(不带.po
扩展名的翻译文件名称)-l
:要渲染的语言环境。如果未指定,将渲染所有语言环境。-q
:渲染质量,与manim render
中的相同。
欲了解更多信息,请运行 manim_render_translation --help
。
使用机器翻译场景#
我们还引入了命令行工具 manim_translate
,以便更轻松地翻译场景。它使用 DeepL API 将场景中的旁白字符串翻译成其他语言。DeepL 是一项付费服务,但每月允许您免费翻译多达 500,000 个字符。您可以在此处注册一个免费帐户。
注册 DeepL 帐户后,生成一个 API 密钥并将其设置为环境变量 DEEPL_API_KEY
。如果您将其保存在项目根目录下的 .env
文件中,则在运行 manim_translate
时它将自动加载。例如,
# Assuming you are in the root directory of manim-voiceover repo
cd examples/translation-example
manim_translate translation-example.py \
-s en \
-t tr \
-d translation-example
其中,
-s
:场景的原始(源)语言。-t
:要翻译到的目标语言。-d
:gettext 域,用于保存翻译(不带.po
扩展名的翻译文件名称)
运行此命令将生成一个文件 locale/tr/LC_MESSAGES/translation-example.po
,其中包含翻译后的字符串。然后,您可以通过运行以下命令以土耳其语渲染场景:
manim_render_translation translation-example.py -s TranslationExample -d translation-example -l tr
欲了解更多信息,请运行 manim_translate --help
。
可用语言#
Manim Voiceover 遵循 ISO 639-1 语言代码标准。例如,英语的语言代码是 en
,越南语的语言代码是 vi
。
请查看 DEEPL_SOURCE_LANG
以了解可翻译的源语言,以及 DEEPL_TARGET_LANG
以了解可翻译到的目标语言。
在 DeepL API 文档中包含有关可用语言的更详细信息。如果 DeepL 已添加对新语言的支持,但 Manim Voiceover 尚未提供,请提交一个问题或提交一个拉取请求以将其纳入。
编辑和维护翻译#
通过 manim_translate
生成的翻译可以在 .po
文件中手动编辑。您也可以使用 Poedit 等 GUI 工具编辑翻译。 .po
文件采用 gettext 格式,这是存储翻译的标准格式。您可以在此处找到有关该格式的更多信息。
运行 manim_translate
不会覆盖您现有的翻译,而只会填充缺失的翻译。如果您对原始场景进行了更改,可以再次运行 manim_translate
。这会将新字符串插入到 locale/<domain>.pot
和 locale/<domain>/LC_MESSAGES/<domain>.po
文件中,并使用 DeepL 将新字符串翻译成目标语言。
重要提示
我们建议为您的项目启动一个 git 仓库,并包含 .po
文件。这将使跟踪翻译更改和合并其他贡献者的更改变得更容易。如果出现错误,它还允许您恢复到早期版本的翻译。
请勿将 .mo
文件包含在您的 git 仓库中。这些是自动从 .po
文件生成的二进制文件。将行 *.mo
添加到您的 .gitignore
文件中,以防止它们被添加到您的 git 仓库。