Translation

You are viewing documentation for an unreleased version of Craft CMS. Please be aware that the material is changing frequently and may be incomplete or inaccurate, and links may point back to older versions.

One of the few remaining Yii components is its static translation engine (opens new window)—we weren’t ready to leave behind its powerful parameterization and formatting API.

#Language Files

The structure of message files remains exactly the same, but your entire translations/ directory must be renamed lang/ to be discoverable by the new base plugin class.

#Helpers

Craft::t() has been replaced with a global helper function, and its signature has changed:

use function CraftCms\Cms\t;

// t($message, $params, $category)
t('Successfully logged the {name} event.', ['name' => $event->name], 'activity');

When you only need a message and category, use a named argument:

t('Failed to log the event.', category: 'activity');

The Twig filter’s signature has not changed:

{{ 'Summary of {num,plural,=1{one event} other{# events}}'|t('activity', { num: report.totalEvents }) }}

#Lazy Translation

In situations where the static message is known, but the language it will be displayed in is not, use a “prepared” string:

use CraftCms\Cms\Support\Facades\I18N;

$description = I18N::prep('Crunching numbers for {author}…', ['owner' => $template->getAuthor()], 'activity');
GenerateReport::dispatch($template, $description);

In this example, $description is just a special encoded string with the source message, parameters, and category; when it comes time to display the job description in the control panel, Craft resolves it in the current user’s language.