vsalvans / twig-translation
Twig Translation Extension using YAML files
Requires
- php: >=5.3.0
- symfony/yaml: ^2.6
- twig/twig: ~1.0
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is not auto-updated.
Last update: 2026-09-16 18:11:25 UTC
README
Twig Translation Extension using YAML files
//TranslationTwigExtension is in include folder
$twig->addExtension(new TranslationTwigExtension($lang, __DIR__.'/locale'));
First argument is $lang and the second is the translation yaml files folder
/locale/es.yaml is the translation file
For instance in es.yaml
Hello: Hola
<a href="#">English</a>: <a href="#">Spanish</a>
Your name is @name: Your name is @name
In the twig template use trans function like this:
<h1>{{ "Hello" | trans }}</h1>
{{ "<a href=\"#\">English</a>" | trans | raw }} <!-- for translation of html content -->
<p>{{ "Your name is @name" | trans({"@name" : "vĂctor"}) }}</p> <!-- for translation with tokens -->
You can add a language fallback usign the third parameter:
$twig->addExtension(new TranslationTwigExtension($lang, __DIR__.'/locale', $fallbackLanguage));
If there is no translation for the current language it searchs a translation in the fallback language files
You can add debug log file. It add a new entry for each translation failed
$twig->addExtension(new TranslationTwigExtension($lang, __DIR__.'/locale', $fallbackLanguage, $logfile));
Then in the terminal you can list all strings without translation buy typing:
$ cat log_file | sort -u
Alert!!
This project has been archived because is old and has vulnerabilities