Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
S
Silex
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
common
Silex
Commits
7c496d60
Commit
7c496d60
authored
May 26, 2012
by
Fabien Potencier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
removed translation.messages in favor of translation.domains
parent
34b67523
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
43 additions
and
67 deletions
+43
-67
doc/changelog.rst
doc/changelog.rst
+3
-0
doc/cookbook/translating_validation_messages.rst
doc/cookbook/translating_validation_messages.rst
+8
-7
doc/providers/translation.rst
doc/providers/translation.rst
+29
-49
src/Silex/Provider/TranslationServiceProvider.php
src/Silex/Provider/TranslationServiceProvider.php
+3
-11
No files found.
doc/changelog.rst
View file @
7c496d60
...
@@ -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.
...
...
doc/cookbook/translating_validation_messages.rst
View file @
7c496d60
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
doc/providers/translation.rst
View file @
7c496d60
...
@@ -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.
message
s`` parameter::
the ``translator.
domain
s`` 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.
message
s``
Repeat this for all of your languages. Then set up the ``translator.
domain
s``
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.
message
s`` to map to them.
``translator.
domain
s`` to map to them.
Finally override the ``translator.loader`` to use a ``XliffFileLoader``::
Finally override the ``translator.loader`` to use a ``XliffFileLoader``::
...
...
src/Silex/Provider/TranslationServiceProvider.php
View file @
7c496d60
...
@@ -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
);
}
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment