Commit 7c496d60 authored by Fabien Potencier's avatar Fabien Potencier

removed translation.messages in favor of translation.domains

parent 34b67523
......@@ -3,6 +3,9 @@ Changelog
This changelog references all backward incompatibilities as we introduce them:
* **2012-05-26**: Removed the ``translator.messages`` parameter (use
``translator.domains`` instead).
* **2012-05-24**: Removed the ``autoloader`` service (use composer instead).
The ``*.class_path`` settings on all the built-in providers have also been
removed in favor of Composer.
......
Translating Validation Messages
===============================
When working with Symfony2 validator, a common task would be to show localized validation messages.
When working with Symfony2 validator, a common task would be to show localized
validation messages.
In order to do that, you will need to register translator and point to translated resources:
::
In order to do that, you will need to register translator and point to
translated resources::
$app->register(new Silex\Provider\TranslationServiceProvider(), array(
'locale' => 'sr_Latn',
'translator.messages' => array()
'translator.domains' => array(),
));
$app->before(function () use ($app) {
$app['translator']->addLoader('xlf', new Symfony\Component\Translation\Loader\XliffFileLoader());
$app['translator']->addResource('xlf', __DIR__ . '/vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Resources/translations/validators.sr_Latn.xlf', 'sr_Latn', 'validators');
$app['translator']->addResource('xlf', __DIR__.'/vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Resources/translations/validators.sr_Latn.xlf', 'sr_Latn', 'validators');
});
And that's all you need to load translations from Symfony2 ``xlf`` files.
\ No newline at end of file
And that's all you need to load translations from Symfony2 ``xlf`` files.
......@@ -7,10 +7,8 @@ application into different languages.
Parameters
----------
* **translator.messages** (optional): A mapping of locales to message arrays.
This parameter contains the translation data in all languages.
* **translator.domains** (optional): Same as above but stored by domains.
* **translator.domains**: A mapping of domains/locales/messages. This
parameter contains the translation data for all languages and domains.
* **locale** (optional): The locale for the translator. You will most likely
want to set this based on some request parameter. Defaults to ``en``.
......@@ -58,20 +56,27 @@ Usage
-----
The Translation provider provides a ``translator`` service and makes use of
the ``translator.messages`` parameter::
the ``translator.domains`` parameter::
$app['translator.messages'] = array(
'en' => array(
'hello' => 'Hello %name%',
'goodbye' => 'Goodbye %name%',
),
'de' => array(
'hello' => 'Hallo %name%',
'goodbye' => 'Tschüss %name%',
$app['translator.domains'] = array(
'messages' => array(
'en' => array(
'hello' => 'Hello %name%',
'goodbye' => 'Goodbye %name%',
),
'de' => array(
'hello' => 'Hallo %name%',
'goodbye' => 'Tschüss %name%',
),
'fr' => array(
'hello' => 'Bonjour %name%',
'goodbye' => 'Au revoir %name%',
),
),
'fr' => array(
'hello' => 'Bonjour %name%',
'goodbye' => 'Au revoir %name%',
'validators' => array(
'fr' => array(
'This value should be a valid number.' => 'Cette valeur doit être un nombre.',
),
),
);
......@@ -95,33 +100,6 @@ The above example will result in following routes:
* ``/it/hello/igor`` will return ``Hello igor`` (because of the fallback).
The messages defined with ``translator.messages`` are automatically stored in
the default domain. When you need to explicitly set the translation domain
(for the validation error messages for instance), use the
``translator.domains`` parameter instead::
$app['translator.domains'] = array(
'messages' => array(
'en' => array(
'hello' => 'Hello %name%',
'goodbye' => 'Goodbye %name%',
),
'de' => array(
'hello' => 'Hallo %name%',
'goodbye' => 'Tschüss %name%',
),
'fr' => array(
'hello' => 'Bonjour %name%',
'goodbye' => 'Au revoir %name%',
),
),
'validators' => array(
'fr' => array(
'This value should be a valid number.' => 'Cette valeur doit être un nombre.',
),
),
);
Recipes
-------
......@@ -149,13 +127,15 @@ 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.messages``
Repeat this for all of your languages. Then set up the ``translator.domains``
to map languages to files::
$app['translator.messages'] = array(
'en' => __DIR__.'/locales/en.yml',
'de' => __DIR__.'/locales/de.yml',
'fr' => __DIR__.'/locales/fr.yml',
$app['translator.domains'] = array(
'messages' => array(
'en' => __DIR__.'/locales/en.yml',
'de' => __DIR__.'/locales/de.yml',
'fr' => __DIR__.'/locales/fr.yml',
),
);
Finally override the ``translator.loader`` to use a ``YamlFileLoader`` instead
......@@ -176,7 +156,7 @@ 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.messages`` to map to them.
``translator.domains`` to map to them.
Finally override the ``translator.loader`` to use a ``XliffFileLoader``::
......
......@@ -36,17 +36,9 @@ class TranslationServiceProvider implements ServiceProviderInterface
$translator->addLoader('array', $app['translator.loader']);
if (isset($app['translator.messages'])) {
foreach ($app['translator.messages'] as $locale => $messages) {
$translator->addResource('array', $messages, $locale);
}
}
if (isset($app['translator.domains'])) {
foreach ($app['translator.domains'] as $domain => $data) {
foreach ($data as $locale => $messages) {
$translator->addResource('array', $messages, $locale, $domain);
}
foreach ($app['translator.domains'] as $domain => $data) {
foreach ($data as $locale => $messages) {
$translator->addResource('array', $messages, $locale, $domain);
}
}
......
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