Commit c3273183 authored by Fabien Potencier's avatar Fabien Potencier

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

parent a5a2599f
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``
method instead:
......
......@@ -117,7 +117,7 @@ file:
"require": {
"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
......@@ -128,27 +128,22 @@ use is ``locales/en.yml``. Just do the mapping in this file as follows:
hello: Hello %name%
goodbye: Goodbye %name%
Repeat this for all of your languages. Then set up the ``translator.domains``
to map languages to files::
Repeat this for all of your languages. Then add the files to the
``translator``::
$app['translator.domains'] = array(
'messages' => array(
'en' => __DIR__.'/locales/en.yml',
'de' => __DIR__.'/locales/de.yml',
'fr' => __DIR__.'/locales/fr.yml',
),
);
$app['translator']->addResource('yaml', __DIR__.'/locales/en.yml', 'en');
$app['translator']->addResource('yaml', __DIR__.'/locales/de.yml', 'de');
$app['translator']->addResource('yaml', __DIR__.'/locales/fr.yml', 'fr');
Finally override the ``translator.loader`` to use a ``YamlFileLoader`` instead
of the default ``ArrayLoader``::
Finally, register the ``YamlFileLoader`` on the translator::
use Symfony\Component\Translation\Loader\YamlFileLoader;
$app['translator.loader'] = $app->share(function () {
return new YamlFileLoader();
});
$app['translator'] = $app->share($app->extend('translator', function($translator, $app) {
$translator->addLoader('yaml', return new YamlFileLoader());
That's all you need to load translations from YAML files.
return $translator;
}));
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
Symfony2 ``Config`` component as a dependency (see above for details).
Then, similarly, create XLIFF files in your locales directory and setup the
``translator.domains`` to map to them.
Finally override the ``translator.loader`` to use a ``XliffFileLoader``::
Then, similarly, create XLIFF files in your locales directory and add them to
the translator::
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 () {
return new XliffFileLoader();
});
.. note::
That's it.
The XLIFF loader is already pre-configured by the extension.
Accessing translations in Twig templates
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
......
......@@ -17,6 +17,7 @@ use Silex\ServiceProviderInterface;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\MessageSelector;
use Symfony\Component\Translation\Loader\ArrayLoader;
use Symfony\Component\Translation\Loader\XliffFileLoader;
/**
* Symfony Translation component Provider.
......@@ -36,7 +37,8 @@ class TranslationServiceProvider implements ServiceProviderInterface
$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 ($data as $locale => $messages) {
......@@ -47,10 +49,6 @@ class TranslationServiceProvider implements ServiceProviderInterface
return $translator;
});
$app['translator.loader'] = $app->share(function () {
return new ArrayLoader();
});
$app['translator.message_selector'] = $app->share(function () {
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