Skip to content

Commit be0aebc

Browse files
wouterjweaverryan
authored andcommitted
Rewrote book article
1 parent 0bea6e4 commit be0aebc

File tree

2 files changed

+51
-28
lines changed

2 files changed

+51
-28
lines changed

book/translation.rst

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ the user::
2626
`ISO639-1`_ *language* code, an underscore (``_``), then the `ISO3166 Alpha-2`_ *country*
2727
code (e.g. ``fr_FR`` for French/France) is recommended.
2828

29-
In this chapter, you'll learn how to prepare an application to support multiple
30-
locales and then how to create translations for multiple locales. Overall,
31-
the process has several common steps:
29+
In this chapter, you'll learn how to use the Translation component in the
30+
Symfony2 framework. Read the
31+
:doc:`components documentation </components/translation>` to learn how to use
32+
the Translator. Overall, the process has several common steps:
3233

33-
#. Enable and configure Symfony's ``Translation`` component;
34+
#. Enable and configure Symfony's Translation component;
3435

3536
#. Abstract strings (i.e. "messages") by wrapping them in calls to the ``Translator``;
3637

@@ -40,13 +41,10 @@ the process has several common steps:
4041
#. Determine, set and manage the user's locale for the request and optionally
4142
on the user's entire session.
4243

43-
.. index::
44-
single: Translations; Configuration
45-
4644
Configuration
4745
-------------
4846

49-
Translations are handled by a ``Translator`` :term:`service` that uses the
47+
Translations are handled by a ``translator`` :term:`service` that uses the
5048
user's locale to lookup and return translated messages. Before using it,
5149
enable the ``Translator`` in your configuration:
5250

@@ -94,7 +92,7 @@ The locale used in translations is the one stored on the request. This is
9492
typically set via a ``_locale`` attribute on your routes (see :ref:`book-translation-locale-url`).
9593

9694
Fallback and Default Locale
97-
---------------------------
95+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
9896

9997
If the locale hasn't been set explicitly in the session, the ``fallback_locale``
10098
configuration parameter will be used by the ``Translator``. The parameter
@@ -130,11 +128,31 @@ by defining a ``default_locale`` for the framework:
130128
originally, however, as of 2.1 this has been moved. This is because the
131129
locale is now set on the request instead of the session.
132130

133-
.. index::
134-
single: Translations; Translation resource locations
131+
Using the Translation inside Controllers
132+
----------------------------------------
133+
134+
When you want to use translation inside controllers, you need to get the
135+
``translator`` service and use ``trans`` or ``transChoice``::
136+
137+
// src/Acme/DemoBundle/Controller/DemoController.php
138+
namespace Amce\DemoBundle\Controller;
139+
140+
// ...
141+
142+
class DemoController extends Controller
143+
{
144+
public function indexAction()
145+
{
146+
$translator = $this->get('translator');
147+
148+
$translated = $translator->trans('Symfony2 is great!');
149+
150+
return new Response($translated);
151+
}
152+
}
135153

136154
Translation Locations and Naming Conventions
137-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
155+
--------------------------------------------
138156

139157
Symfony2 looks for message files (i.e. translations) in the following locations:
140158

@@ -157,7 +175,7 @@ to determine details about the translations. Each message file must be named
157175
according to the following path: ``domain.locale.loader``:
158176

159177
* **domain**: An optional way to organize messages into groups (e.g. ``admin``,
160-
``navigation`` or the default ``messages``) - see `Using Message Domains`_;
178+
``navigation`` or the default ``messages``) - see ":ref:`using-message-domains`";
161179

162180
* **locale**: The locale that the translations are for (e.g. ``en_GB``, ``en``, etc);
163181

@@ -168,8 +186,8 @@ The loader can be the name of any registered loader. By default, Symfony
168186
provides the following loaders:
169187

170188
* ``xliff``: XLIFF file;
171-
* ``php``: PHP file;
172-
* ``yml``: YAML file.
189+
* ``php``: PHP file;
190+
* ``yml``: YAML file.
173191

174192
The choice of which loader to use is entirely up to you and is a matter of
175193
taste.
@@ -181,7 +199,7 @@ taste.
181199
:class:`Symfony\\Component\\Translation\\Loader\\LoaderInterface` interface.
182200
See the :ref:`dic-tags-translation-loader` tag for more information.
183201

184-
.. tip::
202+
.. caution::
185203

186204
Each time you create a *new* translation resource (or install a bundle
187205
that includes a translation resource), be sure to clear your cache so
@@ -191,9 +209,6 @@ taste.
191209
192210
$ php app/console cache:clear
193211
194-
.. index::
195-
single: Translations; User's locale
196-
197212
Handling the User's Locale
198213
--------------------------
199214

@@ -282,9 +297,6 @@ as the locale for the user's session.
282297
You can now use the user's locale to create routes to other translated pages
283298
in your application.
284299

285-
.. index::
286-
single: Translations; In templates
287-
288300
Translations in Templates
289301
-------------------------
290302

@@ -296,11 +308,6 @@ support for both Twig and PHP templates.
296308
Twig Templates
297309
~~~~~~~~~~~~~~
298310

299-
..
300-
However, the ``%var%``
301-
notation is required when translating in Twig templates, and is overall a
302-
sensible convention to follow.
303-
304311
Symfony2 provides specialized Twig tags (``trans`` and ``transchoice``) to
305312
help with message translation of *static blocks of text*:
306313

@@ -316,6 +323,11 @@ The ``transchoice`` tag automatically gets the ``%count%`` variable from
316323
the current context and passes it to the translator. This mechanism only
317324
works when you use a placeholder following the ``%var%`` pattern.
318325

326+
.. caution::
327+
328+
The ``%var%`` notation of placeholders is required when translating in
329+
Twig templates.
330+
319331
.. tip::
320332

321333
If you need to use the percent character (``%``) in a string, escape it by
@@ -480,7 +492,9 @@ empty, add the following:
480492
}
481493
}
482494
483-
Create a translation file under the ``validators`` catalog for the constraint messages, typically in the ``Resources/translations/`` directory of the bundle. See `Message Catalogues`_ for more details.
495+
Create a translation file under the ``validators`` catalog for the constraint
496+
messages, typically in the ``Resources/translations/`` directory of the
497+
bundle.
484498

485499
.. configuration-block::
486500

components/translation.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@ You start this process by calling
140140
Translator looks for the exact string inside the appropriate message catalog
141141
and returns it (if it exists).
142142

143+
.. tip::
144+
145+
When a translation does not exist for a locale, the translator first tries
146+
to find the translation for the language (``fr`` if the locale is
147+
``fr_FR`` for instance). If this also fails, it looks for a translation
148+
using the fallback locale.
149+
143150
Fallback Locale
144151
~~~~~~~~~~~~~~~
145152

@@ -492,6 +499,8 @@ Or numbers between two other numbers:
492499
The left delimiter can be ``[`` (inclusive) or ``]`` (exclusive). The right
493500
delimiter can be ``[`` (exclusive) or ``]`` (inclusive). Beside numbers, you
494501

502+
.. _using-message-domains:
503+
495504
Using Message Domains
496505
---------------------
497506

0 commit comments

Comments
 (0)