rafaph/twig-translation-extension

Twig translate function for Illuminate\Translation\Translator.

1.0.0 2016-03-15 17:17 UTC

This package is auto-updated.

Last update: 2024-04-05 16:55:55 UTC


README

Twig Translation Extension

Translation functions for Twig provide by Illuminate/translation.

Installation

composer require rafaph/twig-translation-extension

Usage

  • Configure the Illuminate\Translation\Translator:
$langPath = __DIR__ . '/lang';
$locale = 'pt';

$translator = new \Illuminate\Translation\Translator(
    new \Illuminate\Translation\FileLoader(
        new \Illuminate\Filesystem\Filesystem(),
        $langPath
    ),
    $locale
);
$translator->setFallback('en');

More details about the configurations of the Translator package you can find in the Laravel docs.

  • Add Twig extension
// ...
$twig->addExtension(
	new \Raph\Twig\Extension\TranslationExtension($translator)
);
  • Configure your messages
//in lang/en/messages.php

return [
	'hello' => 'Hello :name!',
	'there' => '{0} There are none|[1,19] There are some|[20,Inf] There are many'
];
//in lang/pt/messages.php

return [
	'hello' => 'Olá :name!',
	'there' => '{0} Nenhum|[1,19] Alguns|[20,Inf] Muitos'
];
  • Use in templates
<h2>{{ trans('messages.hello', {'name': 'Jane Doe'}) }}</h2>
<h2>{{ trans_choice('messages.there', 2) }}</h2>