Skip to content

Localisation and internationalisation

Radoslav Georgiev edited this page Sep 3, 2016 · 1 revision

You can read about internationalization at the I18n for WordPress Developers page in the WordPress codex.

If you are using Blade as your template language, or simply PHP, you can skip this article. However, if you are using Twig, those functions are not accessible by default, because of the way Twig works.

The L10N class within the plugin allows you to actually use those same functions within your .twig templates.

There are a couple of things you should have in mind:

Using a default text domain

As you probably saw in the codex, most internationalization functions look like this:

__( 'My string', 'my-theme-textdomain' )

The second argument, the text domain, can be quite annoying if you need to type it in each function within your template. Because of that, the framework supports a default text domain, which you can set through the rila.default_textdomain filter, like this:

add_filter( 'rila.default_textdomain', 'my_theme_default_textdomain' );
function my_theme_default_textdomain( $domain ) {
    return 'my-theme-textdomain';
}

If you copy that code in your theme, the __ example above becomes a bit shorter:

{{ __( 'My string' ) }}

Collecting translate-able text

In order to translate the string into a different language, you need to collect them first. Normally, this is done through gettext or another application, which utilizes it, like Poedit.

However, those applications are not meant to support .twig files by default. There is an excellent article in Timber's documentation which covers this topic.


Next article: Shortcodes