翻译在树枝模板中呈现的 ACF 字段 [英] Translate ACF field that renders in twig template
问题描述
我正在一个网站上工作,在那里我在树枝上呈现通过 ACF 字段设置的内容.目前我正在实施翻译,并想知道我会怎么做,因为它不是真正的字符串,它的树枝.
I'm working on a site where i in twig renders content that gets set via ACF-fields. Currently i'm implementing translations and was wondering how i would do it since it not really strings, its twig.
php 文件:
$context = Timber::get_context();
$context['header'] = array(
'title' => get_field('header_title')
);
Timber::render('/templates/index.twig', $context);
我的模板看起来像这样.
My template looks like this.
<header>
{% if header.title %}
<h1>
{{ header.title }}
</h1>
{% endif %}
</header>
但是对于翻译工具(po 文件)希望语法是:
But for the translation tool (po files) want the syntax to be:
{{ __("string to translate") }}
那么我怎样才能将 {{ header.title }}
传入其中?
So how can i instead pass in {{ header.title }}
into that?
推荐答案
您不能将标题标题传递给 __()
.只有编写在代码中的静态字符串才能使用诸如 __()
之类的 gettext 函数来处理.他们不使用变量.如果您有来自数据库的字符串,则不会使用字符串翻译函数.相反,您需要一个多语言的 WordPress 解决方案.
You can’t pass the header title into __()
. Only static strings that are written in your code are handled with gettext functions like __()
. They don’t work with variables. If you have strings from the database, you won’t use string translation functions. Instead, you need a multilingual solution for WordPress.
阅读有关多语言 WordPress 的 Codex 页面以开始使用.如果您想翻译从数据库中提取的内容,您可能需要使用插件.其中最受欢迎的是:
Read the Codex page about Multilingual WordPress to get started. You’ll probably want to use a plugin if you want to have content that is pulled from the database translated. Among the popular ones are:
这篇关于翻译在树枝模板中呈现的 ACF 字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!