rafaph / twig-translation-extension
Twig translate function for Illuminate\Translation\Translator.
Installs: 6 543
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 1
Forks: 1
Open Issues: 0
Requires
- php: >=5.5
- illuminate/translation: ^5.2
- twig/twig: ^1.24
This package is auto-updated.
Last update: 2025-05-05 19:22:13 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>