Translate ACF field that renders in twig template

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 answer

  • answered 2018-12-06 15:05 Gchtr

    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: