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