@@ -36,16 +36,35 @@ entry point of the Translation component.
36
36
37
37
echo $translator->trans('Hello World!');
38
38
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
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
41
63
42
64
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
44
66
locale.
45
67
46
- Loading catalogues
47
- ~~~~~~~~~~~~~~~~~~
48
-
49
68
The Translation component uses Loader classes to load catalogues. You can load
50
69
multiple resources for the same locale, it will be combined into one
51
70
catalogue.
@@ -100,38 +119,4 @@ instead of an array::
100
119
$translator->addLoader('yaml', new YamlFileLoader());
101
120
$translator->addResource('yaml', 'path/to/messages.fr.yml', 'fr_FR');
102
121
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
-
137
122
.. _Packagist : https://packagist.org/packages/symfony/translation
0 commit comments