Skip to content

Commit 0227d2c

Browse files
committed
Add docs for translatable objects
1 parent 161a97b commit 0227d2c

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

translation.rst

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,35 @@ To manage these situations, Symfony follows the `ICU MessageFormat`_ syntax by
292292
using PHP's :phpclass:`MessageFormatter` class. Read more about this in
293293
:doc:`/translation/message_format`.
294294

295+
Translatable Objects
296+
--------------------
297+
298+
Sometimes you may want to create a message, but at the time of creation aren't
299+
sure how it would be translated. For example, it could be translated multiple
300+
times if intended to be displayed to multiple users.
301+
302+
Instead of translating a string at the time of creation, a ``Translatable``
303+
object can be created that can then be translated when used.
304+
305+
.. code-block:: php
306+
307+
$message = new Translatable('Symfony is great!');
308+
$message = t('Symfony is great!');
309+
310+
Later this message can be translated with a translator in either PHP or in Twig.
311+
312+
PHP:
313+
314+
.. code-block:: php
315+
316+
Translatable::trans($translator, $message);
317+
318+
Twig:
319+
320+
.. code-block:: html+twig
321+
322+
<h1>{{ message|trans }}</h1>
323+
295324
.. _translation-in-templates:
296325

297326
Translations in Templates

0 commit comments

Comments
 (0)