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
2456d5a5
Commit
2456d5a5
authored
May 02, 2011
by
Igor Wiedler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[docs] Documentation for the TranslationExtension
parent
74385506
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
92 additions
and
0 deletions
+92
-0
doc/extensions.rst
doc/extensions.rst
+1
-0
doc/extensions/index.rst
doc/extensions/index.rst
+1
-0
doc/extensions/translation.rst
doc/extensions/translation.rst
+90
-0
No files found.
doc/extensions.rst
View file @
2456d5a5
...
...
@@ -64,6 +64,7 @@ All of these are within the ``Silex\Extension`` namespace.
* :doc:`MonologExtension <extensions/monolog>`
* :doc:`SessionExtension <extensions/session>`
* :doc:`TwigExtension <extensions/twig>`
* :doc:`TranslationExtension <extensions/translation>`
* :doc:`UrlGeneratorExtension <extensions/url_generator>`
Creating an extension
...
...
doc/extensions/index.rst
View file @
2456d5a5
...
...
@@ -7,5 +7,6 @@ Silex
doctrine
monolog
session
translation
twig
url_generator
doc/extensions/translation.rst
0 → 100644
View file @
2456d5a5
TranslationExtension
=====================
The *TranslationExtension* provides a service for translating your application
into different languages.
Parameters
----------
* **translator.messages**: A mapping of locales to message arrays. This parameter
contains the translation data in all languages.
* **locale** (optional): The locale for the translator. You will most likely want
to set this based on some request parameter. Defaults to ``en``.
* **locale_fallback** (optional): Fallback locale for the translator. It will
be used when the current locale has no messages set.
* **translation.class_path** (optional): Path to where
the Symfony2 Translation component is located.
Services
--------
* **translator**: An instance of `Translator
<http://api.symfony.com/2.0/Symfony/Component/Translation/Translator.html>`_,
that is used for translation.
* **translator.loader**: An instance of an implementation of the translation
`LoaderInterface <http://api.symfony.com/2.0/Symfony/Component/Translation/Loader/LoaderInterface.html>`_,
defaults to an `ArrayLoader
<http://api.symfony.com/2.0/Symfony/Component/Translation/Loader/ArrayLoader.html>`_.
* **translator.message_selector**: An instance of `MessageSelector
<http://api.symfony.com/2.0/Symfony/Component/Translation/MessageSelector.html>`_.
Registering
-----------
::
use Silex\Extension\TranslationExtension;
$app->register(new TranslationExtension(), array(
'locale_fallback' => 'en',
'translation.class_path' => __DIR__.'/vendor/symfony/src',
));
Usage
-----
The Translation extension provides a ``translator`` service and makes use of
the ``translator.messages`` parameter.
::
$app['translator.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%',
),
);
$app->before(function () use ($app) {
if ($locale = $app['request']->get('locale')) {
$app['locale'] = $locale;
}
});
$app->get('/{locale}/{message}/{name}', function ($message, $name) use ($app) {
return $app['translator']->trans($message, array('%name%' => $name));
});
The above example will result in following routes:
* ``/en/hello/igor`` will return ``Hello igor``.
* ``/de/hello/igor`` will return ``Hallo igor``.
* ``/fr/hello/igor`` will return ``Bonjour igor``.
* ``/it/hello/igor`` will return ``Hello igor`` (because of the fallback).
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