Skip to content

Commit d0dcc10

Browse files
committed
minor #7477 Added a new section "Extracting Translation Contents and Updating Cat… (javiereguiluz)
This PR was squashed before being merged into the 2.7 branch (closes #7477). Discussion ---------- Added a new section "Extracting Translation Contents and Updating Cat… This fixes #4061. I propose to mention [Symfony Translation Bundle](https://github.com/php-translation/symfony-bundle), created by @Nyholm, instead of the semi-abandoned JMSTranslationBundle. @Nyholm is actively supporting his bundle and soon he's going to propose integrating some of its best features into the core Symfony bundle. Commits ------- 74e82e0 Added a new section "Extracting Translation Contents and Updating Cat…
2 parents 0528509 + 74e82e0 commit d0dcc10

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

translation.rst

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,12 @@ of text (called a *message*), use the
107107
for example, that you're translating a simple message from inside a controller::
108108

109109
// ...
110-
use Symfony\Component\HttpFoundation\Response;
111110

112111
public function indexAction()
113112
{
114113
$translated = $this->get('translator')->trans('Symfony is great');
115114

116-
return new Response($translated);
115+
// ...
117116
}
118117

119118
.. _translation-resources:
@@ -185,13 +184,11 @@ Message Placeholders
185184

186185
Sometimes, a message containing a variable needs to be translated::
187186

188-
use Symfony\Component\HttpFoundation\Response;
189-
190187
public function indexAction($name)
191188
{
192189
$translated = $this->get('translator')->trans('Hello '.$name);
193190

194-
return new Response($translated);
191+
// ...
195192
}
196193

197194
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:
258255

259256
.. code-block:: twig
260257
261-
{% trans with {'%name%': 'Fabien'} from "app" %}Hello %name%{% endtrans %}
258+
{% trans with {'%name%': 'Fabien'} from 'app' %}Hello %name%{% endtrans %}
262259
263-
{% trans with {'%name%': 'Fabien'} from "app" into "fr" %}Hello %name%{% endtrans %}
260+
{% trans with {'%name%': 'Fabien'} from 'app' into 'fr' %}Hello %name%{% endtrans %}
264261
265-
{% transchoice count with {'%name%': 'Fabien'} from "app" %}
262+
{% transchoice count with {'%name%': 'Fabien'} from 'app' %}
266263
{0} %name%, there are no apples|{1} %name%, there is one apple|]1,Inf[ %name%, there are %count% apples
267264
{% endtranschoice %}
268265
@@ -277,7 +274,7 @@ texts* and complex expressions:
277274
278275
{{ message|transchoice(5) }}
279276
280-
{{ message|trans({'%name%': 'Fabien'}, "app") }}
277+
{{ message|trans({'%name%': 'Fabien'}, 'app') }}
281278
282279
{{ message|transchoice(5, {'%name%': 'Fabien'}, 'app') }}
283280
@@ -308,7 +305,7 @@ texts* and complex expressions:
308305

309306
.. code-block:: twig
310307
311-
{% trans_default_domain "app" %}
308+
{% trans_default_domain 'app' %}
312309
313310
Note that this only influences the current template, not any "included"
314311
template (in order to avoid side effects).
@@ -329,6 +326,33 @@ The translator service is accessible in PHP templates through the
329326
array('%count%' => 10)
330327
) ?>
331328

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+
332356
.. _translation-resource-locations:
333357

334358
Translation Resource/File Names and Locations
@@ -470,3 +494,4 @@ Learn more
470494
.. _`ISO 639-1`: https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
471495
.. _`Translatable Extension`: http://atlantic18.github.io/DoctrineExtensions/doc/translatable.html
472496
.. _`Translatable Behavior`: https://github.com/KnpLabs/DoctrineBehaviors
497+
.. _`TranslationBundle`: https://github.com/php-translation/symfony-bundle

0 commit comments

Comments
 (0)