如何配置 twig 和 Timber 以翻译带有 {% trans %} 标签的完整 twig 模板

How to config twig and Timber to translate full twig templates with {% trans %} tags

我一直在尝试使用 {% trans %} 标签制作 Timber 渲染和翻译模板。

我使用 poedit 和 Twig-Gettext-Extractor 创建了一个 .po 翻译文件,但找不到使其与 Timber 一起工作的方法。我通过 Timber get_twig 钩子将 i18n 国际化扩展添加到 twig,但我无法配置我的 wordpress 主题。我尝试了 twig docs and here 的不同方法,但没有成功。

是否有人使用带有 {% trans %} 标签的 Timber 和 twig 模板并想分享其配置?

用 Timber 翻译主题与开发普通 WordPress 主题时的处理方式并没有什么不同。您可能 不需要 Twig 的 i18n 扩展 提供的任何功能。您也不必使用 {% trans %} 标签。

当我去翻译一个 Timber 主题时,我关注了关于 Multilingual WordPress and the chapter about Internationalization (i18n) in the Theme Handbook. Since Timber version 1.x you can use most of the common translation functions right in your Twig files. There’s a section about using those in the Text Cookbook in the Timber Documentation 的 Codex 页面。

我想当您的 .po.mo 文件已经存在于主题的 languages 文件夹中时,我想您唯一需要确保的是正确使用 load_theme_textdomain.