Created
December 18, 2012 16:29
-
-
Save 2bard/4329452 to your computer and use it in GitHub Desktop.
Using Symfony translation component in Twig outside of Symfony2 framework.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Note: I've left some of the full package names in tact to show exactly where the class comes from. | |
#### Pull dependencies in using composer #### | |
//Example composer.json | |
{ | |
"require": { | |
"symfony/config" : "2.1.0", | |
"symfony/yaml" : "2.1.0", | |
"twig/twig": "1.9.0", | |
"twig/extensions": "*", | |
"symfony/twig-bridge": "2.1.*", | |
"symfony/form": "*", | |
"symfony/templating": "*", | |
"symfony/translation": "2.1.*" | |
}, | |
} | |
Then run "php composer.phar install" in your shell (or "php composer.phar update" if you're updating) | |
#### Setup twig #### | |
//make twig loader | |
$loader = new \Twig_Loader_Filesystem('/path/to/templates/'); | |
//pass loader to new twig environment | |
$twig = new \Twig_Environment($loader); | |
#### Setup translation #### | |
//make translator, pass locale and instance of MessageSelector | |
$translator = new Translator('en_GB', new \Symfony\Component\Translation\MessageSelector()); | |
//set default locale (good for testing!) | |
$translator->setFallbackLocale('fr_FR'); | |
//build the yaml loader | |
$yamlLoader = new \Symfony\Component\Translation\Loader\YamlFileLoader(); | |
//add the loader to the translator | |
$translator->addLoader('yaml', $yamlLoader); | |
//add some resources to the translator. mmmm... resources | |
$translator->addResource('yaml', '/myapp/strings/fr_FR.yml', 'fr_FR'); | |
//we can also add resources from an array | |
$translator->addLoader('array', new \Symfony\Component\Translation\Loader\ArrayLoader()); | |
$translator->addResource('array', array( 'Hello World!' => 'Bonjour tout le monde!',),'fr_FR'); | |
#### Bridge between twig and translator #### | |
$twig->addExtension(new \Symfony\Bridge\Twig\Extension\TranslationExtension($translator)); | |
#### Example translation file.. it's just a YAML array #### | |
Welcome: Bienvenue | |
Welcome to your dashboard, %name%: Bienvenue sur votre tableau de bord, %name% | |
#### Usage in twig template #### | |
- Using 'trans' filter: | |
<h3> {{"Welcome to your dashboard, %name%" | trans({'%name%' : user.getName})}}</h3> | |
(Note: user is an object which we have exposed to twig) | |
- Using 'trans' tags: | |
<h3>{% trans %}Hello World!{% endtrans %}</h3> | |
#### Calling the translation object directly #### | |
You can call the translator object directly if you have access to it (stick it in a dependency injection container if you want unicorns) | |
echo $translator->trans('Hello World!'); | |
...and that's magic! | |
Not sure if "symfony/templating" is needed at all?
https://gist.github.com/2bard/4329452#file-translation_notes-L55 totally saved my day, this was exactly the connecting dot i was missing, the bridge isn't mentioned in either twig or symfony docs
Thanks a lot!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks