Skip to content

Commit 341de03

Browse files
wouterjweaverryan
authored andcommitted
Added 'constructing the translator'
1 parent c8787fa commit 341de03

File tree

1 file changed

+25
-40
lines changed

1 file changed

+25
-40
lines changed

components/translation.rst

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,35 @@ entry point of the Translation component.
3636
3737
echo $translator->trans('Hello World!');
3838
39-
Message Catalogues
40-
------------------
39+
Constructing the Translator
40+
---------------------------
41+
42+
Before you can use the Translator, you need to configure it and load the
43+
message catalogues.
44+
45+
Configuration
46+
~~~~~~~~~~~~~
47+
48+
The constructor of the ``Translator`` class needs to arguments: The locale and
49+
a :class:`Symfony\\Component\\Translation\\MessageSelector` to use when using
50+
pluralization (more about that later)::
51+
52+
use Symfony\Component\Translation\Translator;
53+
use Symfony\Component\Translation\MessageSelector;
54+
$translator = new Translator('fr_FR', new MessageSelector());
55+
56+
.. note::
57+
58+
The locale set here is the default locale to use. You can override this
59+
locale when translating strings.
60+
61+
Loading Message Catalogues
62+
~~~~~~~~~~~~~~~~~~~~~~~~~~
4163

4264
The messages are stored in message catalogues inside the ``Translator``
43-
class. A Message Catalogue is like a dictionary of translations for a specific
65+
class. A message catalogue is like a dictionary of translations for a specific
4466
locale.
4567

46-
Loading catalogues
47-
~~~~~~~~~~~~~~~~~~
48-
4968
The Translation component uses Loader classes to load catalogues. You can load
5069
multiple resources for the same locale, it will be combined into one
5170
catalogue.
@@ -100,38 +119,4 @@ instead of an array::
100119
$translator->addLoader('yaml', new YamlFileLoader());
101120
$translator->addResource('yaml', 'path/to/messages.fr.yml', 'fr_FR');
102121

103-
Translate Strings
104-
-----------------
105-
106-
After you have loaded your Message Catalogues, you can begin to translate your
107-
strings. This is done with the
108-
:method:`Symfony\\Component\\Translation\\Translator::trans` method::
109-
110-
// ...
111-
$translator->addResource('array', array(
112-
'Hello World!' => 'Bonjour',
113-
), 'fr_FR');
114-
$translator->addResource('array', array(
115-
'Hello World!' => 'Hello World',
116-
), 'en_GB');
117-
118-
echo $translator->trans('Hello World!');
119-
// >> 'Bonjour'
120-
121-
By default, the ``trans`` method uses the locale that is set in the
122-
constructor of the ``Translator``. If you want to translate another locale,
123-
you can change that by setting the fourth argument to the locale::
124-
125-
// ...
126-
echo $translator->trans('Hello World!', array(), 'messages', 'en_GB');
127-
// >> 'Hello World!'
128-
129-
Learn More
130-
----------
131-
132-
The Translation component can do a lot more things. Read more about the usage
133-
of this component in :ref:`the Translation book article <basic-translation>`.
134-
That article is specific about the Translation component in the Symfony2
135-
Framework, but most of the article is framework independent.
136-
137122
.. _Packagist: https://packagist.org/packages/symfony/translation

0 commit comments

Comments
 (0)