@@ -107,13 +107,12 @@ of text (called a *message*), use the
107
107
for example, that you're translating a simple message from inside a controller::
108
108
109
109
// ...
110
- use Symfony\Component\HttpFoundation\Response;
111
110
112
111
public function indexAction()
113
112
{
114
113
$translated = $this->get('translator')->trans('Symfony is great');
115
114
116
- return new Response($translated);
115
+ // ...
117
116
}
118
117
119
118
.. _translation-resources :
@@ -185,13 +184,11 @@ Message Placeholders
185
184
186
185
Sometimes, a message containing a variable needs to be translated::
187
186
188
- use Symfony\Component\HttpFoundation\Response;
189
-
190
187
public function indexAction($name)
191
188
{
192
189
$translated = $this->get('translator')->trans('Hello '.$name);
193
190
194
- return new Response($translated);
191
+ // ...
195
192
}
196
193
197
194
However, creating a translation for this string is impossible since the translator
@@ -258,11 +255,11 @@ You can also specify the message domain and pass some additional variables:
258
255
259
256
.. code-block :: twig
260
257
261
- {% trans with {'%name%': 'Fabien'} from " app" %}Hello %name%{% endtrans %}
258
+ {% trans with {'%name%': 'Fabien'} from ' app' %}Hello %name%{% endtrans %}
262
259
263
- {% trans with {'%name%': 'Fabien'} from " app" into "fr" %}Hello %name%{% endtrans %}
260
+ {% trans with {'%name%': 'Fabien'} from ' app' into 'fr' %}Hello %name%{% endtrans %}
264
261
265
- {% transchoice count with {'%name%': 'Fabien'} from " app" %}
262
+ {% transchoice count with {'%name%': 'Fabien'} from ' app' %}
266
263
{0} %name%, there are no apples|{1} %name%, there is one apple|]1,Inf[ %name%, there are %count% apples
267
264
{% endtranschoice %}
268
265
@@ -277,7 +274,7 @@ texts* and complex expressions:
277
274
278
275
{{ message|transchoice(5) }}
279
276
280
- {{ message|trans({'%name%': 'Fabien'}, " app" ) }}
277
+ {{ message|trans({'%name%': 'Fabien'}, ' app' ) }}
281
278
282
279
{{ message|transchoice(5, {'%name%': 'Fabien'}, 'app') }}
283
280
@@ -308,7 +305,7 @@ texts* and complex expressions:
308
305
309
306
.. code-block :: twig
310
307
311
- {% trans_default_domain " app" %}
308
+ {% trans_default_domain ' app' %}
312
309
313
310
Note that this only influences the current template, not any "included"
314
311
template (in order to avoid side effects).
@@ -329,6 +326,33 @@ The translator service is accessible in PHP templates through the
329
326
array('%count%' => 10)
330
327
) ?>
331
328
329
+ Extracting Translation Contents and Updating Catalogs Automatically
330
+ -------------------------------------------------------------------
331
+
332
+ The most time-consuming tasks when translating an application is to extract all
333
+ the template contents to be translated and to keep all the translation files in
334
+ sync. Symfony includes a command called ``translation:update `` that helps you
335
+ with these tasks:
336
+
337
+ .. code-block :: terminal
338
+
339
+ # updates the French translation file with the missing strings found in app/Resources/ templates
340
+ $ ./app/console translation:update --dump-messages --force fr
341
+
342
+ # updates the English translation file with the missing strings found in AppBundle
343
+ $ ./app/console translation:update --dump-messages --force en AppBundle
344
+
345
+ .. note ::
346
+
347
+ If you want to see the missing translation strings without actually updating
348
+ the translation files, remove the ``--force `` option from the command above.
349
+
350
+ .. tip ::
351
+
352
+ If you need to extract translation strings from other sources, such as
353
+ controllers, forms and flash messages, consider using the more advanced
354
+ third-party `TranslationBundle `_.
355
+
332
356
.. _translation-resource-locations :
333
357
334
358
Translation Resource/File Names and Locations
@@ -513,3 +537,4 @@ Learn more
513
537
.. _`ISO 639-1` : https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
514
538
.. _`Translatable Extension` : http://atlantic18.github.io/DoctrineExtensions/doc/translatable.html
515
539
.. _`Translatable Behavior` : https://github.com/KnpLabs/DoctrineBehaviors
540
+ .. _`TranslationBundle` : https://github.com/php-translation/symfony-bundle
0 commit comments