Module translations
How a module keeps its strings in translation files, prints them with __(), and fills in variables and plural forms.
By the end every string of your module shows in the visitor's language, and the owner can rewrite any of them in the admin without a module update wiping the edit. For that, all interface text lives in translation files and code prints it by key.
Minimal example
One file per language and domain. The file name becomes the first part of the key:
<?php
return [
'title' => 'Жалобы',
'reports' => [
'empty' => 'Жалоб пока нет.',
'by' => 'Автор: :name',
'count' => ':count жалоба|:count жалобы|:count жалоб',
],
];<?php
return [
'title' => 'Reports',
'reports' => [
'empty' => 'No reports yet.',
'by' => 'By :name',
'count' => ':count report|:count reports',
],
];Printing in PHP and Blade:
__('example.reports.empty');
__('example.reports.by', ['name' => $user->name]);
__('example.reports.count', ['count' => $total]);
__('example.reports.missing', 'Fallback text');<h2>{{ __('example.title') }}</h2>
<p>{{ __('example.reports.count', ['count' => $total]) }}</p>How it works
bootstrapModule() in the module's provider finds the Resources/lang folder and loads it. Each <locale>/<domain>.php file adds a domain: example.php gives example.* keys, and nested arrays continue the key with dots. The core uses the <lowercase module key>.title key as the module's name in the sidebar and in the role editor.
Keep admin strings in a separate domain such as example-admin.php. The Giveaways module does this: player-facing text in giveaways.php, staff text in giveaways-admin.php.
If the visitor's language lacks the key, the core takes the English string, or, when English is off on the site, the string of the first other enabled language. If that is missing too, __() returns the key itself. A string passed as the second argument instead of an array is returned in place of the key.
Outside debug mode the list of translation files is cached for a day. On a live site a new file or key appears after php flute cache:clear.
Variables
A value from the array is filled in by name in two forms: :name and %name%. Write :name; the second form exists for older strings.
Plural forms are separated by |, and the right one is picked by the language's rules when the array has count. Russian has three forms (1, 2 and 5 take different words), English has two. trans_choice('example.reports.count', $total) does the same and puts the number into :count itself. If a string has fewer forms than the language needs, 1 takes the first form and every other number the last.
Notification templates write variables differently, as {name}; see module notifications.
Engine languages
The engine knows 15 locales, listed in config('lang.all'): en, ru, pl, uk, de, zh, fr, es, uz, pt, br, sv, ja, cs, bg. The owner decides which of them visitors can use, see site languages.
ru and en are required. Add the other folders as translations get done: a string missing in a language shows in the fallback language, usually English.
In JavaScript
Module scripts translate with the global functions from public/assets/js/app/i18n.js:
const label = translate('example.reports.empty');
const count = await asyncTranslate('example.reports.count', { count: 3 });translate(key, replace, locale) returns the cached string; until the translation has loaded it returns the key and fetches the translation in the background. asyncTranslate() waits for the server. Text that is visible as soon as the page loads is better printed on the server with __() and handed to the script in an attribute.
How the owner edits strings
Every string of the module is listed in AdminTranslationsInterface. The owner types their own version, which is stored apart from the module's files, so a module update does not wipe it. The save is refused if the new text lost a variable of the original: "keep the variables :list.". See site translations.
On top of the module's files the core lays approved community translations, and the owner's edits on top of those. php flute i18n:sync refreshes the community translations.
Reference
| Function | What it does |
|---|---|
__(string $key, array|string $replacements = [], ?string $locale = null): string | string by key. A string instead of the array is the fallback text |
t(), trans() | same as __(). trans() takes only an array |
trans_choice(string $key, int|float $number, array $replacements = [], ?string $locale = null): string | the plural form for $number |
transValue($value, ?string $locale = null) | text from a {"ru": "...", "en": "..."} JSON value the owner typed into a multilingual field |
@t('key', [...]) | Blade, prints __() unescaped. Use {{ __() }} for ordinary text |
translate(), asyncTranslate() | JavaScript, see above |
Common mistakes
A module file is named like a core domain
For example permissions.php, admin.php or def.php. A domain with that name comes whole from the file loaded last, and the core's or the module's strings go missing. Name files after the module key: example.php, example-admin.php.
The page shows the key, example.reports.empty, instead of text. The key is in neither the visitor's language nor the fallback language, the file is not in Resources/lang/<locale>/, or the cache was not cleared.
A string is written straight into code or Blade. The owner cannot translate or edit it, and a visitor in another language sees it as is. Every piece of interface text, button labels, errors and empty states included, goes through a translation file.
A plural without count. __('example.reports.count', ['total' => 5]) prints every form joined by |, because the form is picked by count only.