Translate ACF field that renders in twig template

☆樱花仙子☆ 提交于 2020-02-07 02:38:24

问题


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 file:

$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>

But for the translation tool (po files) want the syntax to be:

{{ __("string to translate") }}

So how can i instead pass in {{ header.title }} into that?


回答1:


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.

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:

  • MultilingualPress
  • WPML
  • Polylang


来源:https://stackoverflow.com/questions/53640006/translate-acf-field-that-renders-in-twig-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!