No caso de usar yiisoft/config, a configuração é adicionada automaticamente. Se não, adicione o seguinte mapeamento:
use Yiisoft\Translator\MessageFormatterInterface;
use Yiisoft\Translator\SimpleMessageFormatter;
return [
MessageFormatterInterface::class => SimpleMessageFormatter::class,
];
/** @var \Yiisoft\Translator\Translator $translator **/
$categoryName = 'moduleId';
$pathToModuleTranslations = './module/messages/';
$additionalCategory = new Yiisoft\Translator\CategorySource(
$categoryName,
new \Yiisoft\Translator\Message\Php\MessageSource($pathToModuleTranslations),
new \Yiisoft\Translator\SimpleMessageFormatter()
);
$translator->addCategorySources($additionalCategory);
$translator->translate('Test string: {str}', ['str' => 'string data'], 'moduleId', 'en');
// output: Test string: string data
/** @var \Yiisoft\Translator\SimpleMessageFormatter $formatter */
$pattern = 'Test number: {number}';
$params = ['number' => 5];
$locale = 'en';
echo $formatter->format($pattern, $params, $locale);
// output: Test number: 5
$pattern = 'Test string: {str}';
$params = ['str' => 'string data'];
echo $formatter->format($pattern, $params, $locale);
// output: Test string: string data