Skip to content

Instantly share code, notes, and snippets.

@masudiiuc
Forked from 2bard/translation_notes
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masudiiuc/9343133 to your computer and use it in GitHub Desktop.
Save masudiiuc/9343133 to your computer and use it in GitHub Desktop.
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!
Update 06-03-2014:
-------------------
#if you want to use this outside a symfony application then do the following:
$locale = 'en_GB';
$translator = new \Symfony\Component\Translation\Translator('en_GB', new \Symfony\Component\Translation\MessageSelector());
$ymlLoader = new \Symfony\Component\Translation\Loader\YamlFileLoader();
$translator->setFallbackLocale($locale);
$translator->addLoader('yaml',$ymlLoader);
$translator->addResource('yaml', __DIR__ . '/Translations/'. $locale . '.yml', $locale);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment