Skip to content

I18N

The i18n module is used to translate your website into multiple languages.

Before you can use the i18n module you have to setup a multi site.

You can use the following guide to help you set it up wordpress.org.

To start using the i18n module you have to pick a translation strategy in the boilerplate there are 2 strategies given, the vanilla WordPress way or the MultiLingualPress way.

Picking a strategy should be easy you just have to ask the following question “Do I use Multilingualpress?” if yes pick the MultiLingualPress strategy.

Open config/i18n.php and set the strategy key to your chosen strategy class:

config/i18n.php
<?php
use App\Lib\I18n\WordPressLanguageStrategy;
return [
'enabled' => true,
'strategy' => WordPressLanguageStrategy::class,
];
config/i18n.php
<?php
use App\Lib\I18n\MultiLingualPressStrategy;
return [
'enabled' => true,
'strategy' => MultiLingualPressStrategy::class,
];

Don’t forget to add both import for the I18n and strategy classes

The boilerplate ships with 2 default language files (English Australia and Dutch) these can be found in the “translations” folder. If you want to add a translation file look up the language code and create a JSON file. (You can this table as a reference language codes).

Once you have your files set up it’s time to translate! Creating a translation is simple you will need only 2 things a key value and the translation. The key value is used in your code and will be shared across multiple translation files, so it’s best to follow the following rules.

  • Keep it English
  • Don’t use special characters (!?$# etc) or spaces

An example

translations/nl_NL.json
{
"hello_world": "Hallo wereld"
}
translations/en_AU.json
{
"hello_world": "Oi World"
}

Now that you have a strategy and translation files set up, it’s time to use them. First find a hard coded string, and replace it with the following:

echo I18n::translate('your-translation-key');

Don’t forget to import the I18n class

You can also get the current language code by using the following method:

I18n::current();

Or check if the current language is active:

I18n::is(LangCode::DUTCH);

All available language codes can be found here app/Lib/I18n/LangCode.php

If you need to create a dynamic translation something with a count in the middle of the sentence we got you covered!

First open your translation file and add “%s” to the place you want your dynamic text to appear (do this for all your translation files).

After that add a second argument to the translate method.

echo I18n::translate('your-translation-key', 'your-dynamic-value');

If you need more add more %s to your string and supply the method with more arguments, this works just like sprintf