The TranslationServiceProvider provides a service for translating your application into different languages.
en
.en
.1 2 3 | $app->register(new Silex\Provider\TranslationServiceProvider(), array(
'locale_fallbacks' => array('en'),
));
|
Note
The Symfony Translation Component comes with the "fat" Silex archive but not with the regular one. If you are using Composer, add it as a dependency:
1 | composer require symfony/translation
|
The Translation provider provides a translator
service and makes use of
the translator.domains
parameter:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | $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.',
),
),
);
$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).When translations are stored in a file, you can load them as follows:
1 2 3 4 5 6 7 8 9 10 | $app = new Application();
$app->register(new TranslationServiceProvider());
$app->extend('translator.resources', function ($resources, $app) {
$resources = array_merge($resources, array(
array('array', array('This value should be a valid number.' => 'Cette valeur doit être un nombre.'), 'fr', 'validators'),
));
return $resources;
});
|
Silex\Application\TranslationTrait
adds the following shortcuts:
1 2 3 | $app->trans('Hello World');
$app->transChoice('Hello World');
|
Having your translations in PHP files can be inconvenient. This recipe will show you how to load translations from external YAML files.
First, add the Symfony Config
and Yaml
components as dependencies:
1 | composer require symfony/config symfony/yaml
|
Next, you have to create the language mappings in YAML files. A naming you can
use is locales/en.yml
. Just do the mapping in this file as follows:
1 2 | hello: Hello %name%
goodbye: Goodbye %name%
|
Then, register the YamlFileLoader
on the translator
and add all your
translation files:
1 2 3 4 5 6 7 8 9 10 11 | use Symfony\Component\Translation\Loader\YamlFileLoader;
$app['translator'] = $app->share($app->extend('translator', function($translator, $app) {
$translator->addLoader('yaml', new YamlFileLoader());
$translator->addResource('yaml', __DIR__.'/locales/en.yml', 'en');
$translator->addResource('yaml', __DIR__.'/locales/de.yml', 'de');
$translator->addResource('yaml', __DIR__.'/locales/fr.yml', 'fr');
return $translator;
}));
|
Just as you would do with YAML translation files, you first need to add the
Symfony Config
component as a dependency (see above for details).
Then, similarly, create XLIFF files in your locales directory and add them to the translator:
$translator->addResource('xliff', __DIR__.'/locales/en.xlf', 'en');
$translator->addResource('xliff', __DIR__.'/locales/de.xlf', 'de');
$translator->addResource('xliff', __DIR__.'/locales/fr.xlf', 'fr');
Note
The XLIFF loader is already pre-configured by the extension.
Once loaded, the translation service provider is available from within Twig templates:
1 | {{ app.translator.trans('translation_key') }}
|
Moreover, when using the Twig bridge provided by Symfony (see TwigServiceProvider), you will be allowed to translate strings in the Twig way:
1 2 3 | {{ 'translation_key'|trans }}
{{ 'translation_key'|transchoice }}
{% trans %}translation_key{% endtrans %}
|