Commit c3273183 authored by Fabien Potencier's avatar Fabien Potencier

simplifies the usage of file-based translations with the translation extension

parent a5a2599f
Changelog Changelog
========= =========
* **2012-06-16**: Removed the ``translator.loader`` service. See documentation
for how to use XLIFF or YAML-based translation files.
* **2012-06-15**: removed the ``twig.configure`` service. Use the ``extend`` * **2012-06-15**: removed the ``twig.configure`` service. Use the ``extend``
method instead: method instead:
......
...@@ -117,7 +117,7 @@ file: ...@@ -117,7 +117,7 @@ file:
"require": { "require": {
"symfony/config": "2.1.*", "symfony/config": "2.1.*",
"symfony/yaml": "2.1.*", "symfony/yaml": "2.1.*"
} }
Next, you have to create the language mappings in YAML files. A naming you can Next, you have to create the language mappings in YAML files. A naming you can
...@@ -128,27 +128,22 @@ use is ``locales/en.yml``. Just do the mapping in this file as follows: ...@@ -128,27 +128,22 @@ use is ``locales/en.yml``. Just do the mapping in this file as follows:
hello: Hello %name% hello: Hello %name%
goodbye: Goodbye %name% goodbye: Goodbye %name%
Repeat this for all of your languages. Then set up the ``translator.domains`` Repeat this for all of your languages. Then add the files to the
to map languages to files:: ``translator``::
$app['translator.domains'] = array( $app['translator']->addResource('yaml', __DIR__.'/locales/en.yml', 'en');
'messages' => array( $app['translator']->addResource('yaml', __DIR__.'/locales/de.yml', 'de');
'en' => __DIR__.'/locales/en.yml', $app['translator']->addResource('yaml', __DIR__.'/locales/fr.yml', 'fr');
'de' => __DIR__.'/locales/de.yml',
'fr' => __DIR__.'/locales/fr.yml',
),
);
Finally override the ``translator.loader`` to use a ``YamlFileLoader`` instead Finally, register the ``YamlFileLoader`` on the translator::
of the default ``ArrayLoader``::
use Symfony\Component\Translation\Loader\YamlFileLoader; use Symfony\Component\Translation\Loader\YamlFileLoader;
$app['translator.loader'] = $app->share(function () { $app['translator'] = $app->share($app->extend('translator', function($translator, $app) {
return new YamlFileLoader(); $translator->addLoader('yaml', return new YamlFileLoader());
});
That's all you need to load translations from YAML files. return $translator;
}));
XLIFF-based language files XLIFF-based language files
~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~
...@@ -156,18 +151,16 @@ XLIFF-based language files ...@@ -156,18 +151,16 @@ XLIFF-based language files
Just as you would do with YAML translation files, you first need to add the Just as you would do with YAML translation files, you first need to add the
Symfony2 ``Config`` component as a dependency (see above for details). Symfony2 ``Config`` component as a dependency (see above for details).
Then, similarly, create XLIFF files in your locales directory and setup the Then, similarly, create XLIFF files in your locales directory and add them to
``translator.domains`` to map to them. the translator::
Finally override the ``translator.loader`` to use a ``XliffFileLoader``::
use Symfony\Component\Translation\Loader\XliffFileLoader; $app['translator']->addResource('xliff', __DIR__.'/locales/en.xlf', 'en');
$app['translator']->addResource('xliff', __DIR__.'/locales/de.xlf', 'de');
$app['translator']->addResource('xliff', __DIR__.'/locales/fr.xlf', 'fr');
$app['translator.loader'] = $app->share(function () { .. note::
return new XliffFileLoader();
});
That's it. The XLIFF loader is already pre-configured by the extension.
Accessing translations in Twig templates Accessing translations in Twig templates
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
......
...@@ -17,6 +17,7 @@ use Silex\ServiceProviderInterface; ...@@ -17,6 +17,7 @@ use Silex\ServiceProviderInterface;
use Symfony\Component\Translation\Translator; use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\MessageSelector; use Symfony\Component\Translation\MessageSelector;
use Symfony\Component\Translation\Loader\ArrayLoader; use Symfony\Component\Translation\Loader\ArrayLoader;
use Symfony\Component\Translation\Loader\XliffFileLoader;
/** /**
* Symfony Translation component Provider. * Symfony Translation component Provider.
...@@ -36,7 +37,8 @@ class TranslationServiceProvider implements ServiceProviderInterface ...@@ -36,7 +37,8 @@ class TranslationServiceProvider implements ServiceProviderInterface
$translator->setFallbackLocale($app['locale_fallback']); $translator->setFallbackLocale($app['locale_fallback']);
} }
$translator->addLoader('array', $app['translator.loader']); $translator->addLoader('array', new ArrayLoader());
$translator->addLoader('xliff', new XliffFileLoader());
foreach ($app['translator.domains'] as $domain => $data) { foreach ($app['translator.domains'] as $domain => $data) {
foreach ($data as $locale => $messages) { foreach ($data as $locale => $messages) {
...@@ -47,10 +49,6 @@ class TranslationServiceProvider implements ServiceProviderInterface ...@@ -47,10 +49,6 @@ class TranslationServiceProvider implements ServiceProviderInterface
return $translator; return $translator;
}); });
$app['translator.loader'] = $app->share(function () {
return new ArrayLoader();
});
$app['translator.message_selector'] = $app->share(function () { $app['translator.message_selector'] = $app->share(function () {
return new MessageSelector(); return new MessageSelector();
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment