Skip to main content
Timber

You are reading the documentation for Timber v2.x. Switch to the documentation for Timber v1.x.

Internationalization

Internationalization of a Timber theme works pretty much the same way as it does for default WordPress themes. Follow the guide in the WordPress Theme Handbook to setup i18n for your theme.

Twig has its own i18n extension that gives you {% trans %} tags to define translatable blocks, but there’s no need to use it, because with Timber, you have all you need.

Translation functions #

Timber supports all the translation functions used in WordPress:

  • __()
  • _x()
  • _n()
  • _nx()
  • _n_noop()
  • _nx_noop()
  • translate()
  • translate_nooped_plural()

The functions _e() and _ex() are also supported, but you probably won’t need them in Twig, because {{ }} already echoes the output.

WordPress:

<p class="entry-meta">
<?php _e( 'Posted on', 'my-text-domain' ) ?> [...]
</p>

Timber:

<p class="entry-meta">
{{ __('Posted on', 'my-text-domain') }} [...]
</p>

sprintf notation #

You can use sprintf-type placeholders, using the format filter:

WordPress:

<p class="entry-meta">
<?php printf( __('Posted on %s', 'my-text-domain'), $posted_on_date ) ?>
</p>

Timber:

<p class="entry-meta">
{# Translators: The placeholder will be replaced with the localized post date #}
{{ __('Posted on %s', 'my-text-domain')|format(posted_on_date) }}
</p>

If you want to use the sprintf function in Twig, you have to add it yourself.

Generating localization files #

The Timber team provides a dedicated WP-CLI package that takes care of extracting translations from Twig files.

The main benefit over other tools is that it perfectly integrates with the existing wp i18n make-pot command, making .pot file generation simple and easy.

Note that the package isn't Timber specific, it will work with any Twig implementation that also provides WordPress translation functions.

Alternatives #

Poedit 2 #

You can also generate .pot, .po and .mo files, with Poedit.

Poedit 2 fully supports Twig file parsing (Pro version only) with the following functions: __(), _x(), _n(), _nx().