Internationalization

Internationalization of a theme works pretty much the same way as it does for default WordPress themes. Follow the guide in the WordPress Theme Handbookarrow-up-right 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.

Translation functions

Wordpress Bundle add 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.

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

Generating localization files

To generate .pot, .po and .mo files, you need a tool that supports parsing Twig files to detect all your translations. While there are a lot of tools that can parse PHP files, the solution that works best for Twig files is Poeditarrow-up-right.

Generating l10n files with Poedit 2

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

circle-info

Store .mo and .po into the /translations folder

Last updated