I18N
The i18n module is used to translate your website into multiple languages.
Prerequisites
Section titled “Prerequisites”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.
How To Use
Section titled “How To Use”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.
How To Setup The Strategy
Section titled “How To Setup The Strategy”Open config/i18n.php and set the strategy key to your chosen strategy class:
Vanilla (WordPress)
Section titled “Vanilla (WordPress)”<?php
use App\Lib\I18n\WordPressLanguageStrategy;
return [ 'enabled' => true, 'strategy' => WordPressLanguageStrategy::class,];Multilingualpress (plugin)
Section titled “Multilingualpress (plugin)”<?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
Creating a translation file
Section titled “Creating a translation file”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).
Creating a translation
Section titled “Creating a translation”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
{ "hello_world": "Hallo wereld"}{ "hello_world": "Oi World"}The I18n class
Section titled “The I18n class”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
Advanced Translations
Section titled “Advanced Translations”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

