Commit 49874395 authored by Fabien Potencier's avatar Fabien Potencier

added a way to register translations when the domain is not the default one (closes #185)

parent e3113f97
...@@ -10,6 +10,8 @@ Parameters ...@@ -10,6 +10,8 @@ Parameters
* **translator.messages**: A mapping of locales to message arrays. This * **translator.messages**: A mapping of locales to message arrays. This
parameter contains the translation data in all languages. parameter contains the translation data in all languages.
* **translator.domains**: 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``.
...@@ -93,6 +95,33 @@ The above example will result in following routes: ...@@ -93,6 +95,33 @@ 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
------- -------
......
...@@ -102,5 +102,19 @@ getters:: ...@@ -102,5 +102,19 @@ getters::
You will have to handle the display of these violations yourself. You can You will have to handle the display of these violations yourself. You can
however use the *FormServiceProvider* which can make use of the *ValidatorServiceProvider*. however use the *FormServiceProvider* which can make use of the *ValidatorServiceProvider*.
Translation
~~~~~~~~~~~
To be able to translate the error messages, you can use the translator
provider and register the messages under the ``validators`` domain::
$app['translator.domains'] = array(
'validators' => array(
'fr' => array(
'This value should be a valid number.' => 'Cette valeur doit être un nombre.',
),
),
);
For more information, consult the `Symfony2 Validation documentation For more information, consult the `Symfony2 Validation documentation
<http://symfony.com/doc/2.0/book/validation.html>`_. <http://symfony.com/doc/2.0/book/validation.html>`_.
...@@ -39,6 +39,12 @@ class TranslationServiceProvider implements ServiceProviderInterface ...@@ -39,6 +39,12 @@ class TranslationServiceProvider implements ServiceProviderInterface
$translator->addResource('array', $messages, $locale); $translator->addResource('array', $messages, $locale);
} }
foreach ($app['translator.domains'] as $domain => $data) {
foreach ($data as $locale => $messages) {
$translator->addResource('array', $messages, $locale, $domain);
}
}
return $translator; return $translator;
}); });
......
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