Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a2ad274

Browse filesBrowse files
natewiebe13javiereguiluz
authored andcommitted
Translatable objects
1 parent 92e8e2a commit a2ad274
Copy full SHA for a2ad274

File tree

Expand file treeCollapse file tree

2 files changed

+63
-1
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+63
-1
lines changed

‎reference/twig_reference.rst

Copy file name to clipboardExpand all lines: reference/twig_reference.rst
+28-1Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,27 @@ impersonation_exit_url
304304
It's similar to the `impersonation_exit_path`_ function, but it generates
305305
absolute URLs instead of relative URLs.
306306

307+
t
308+
~
309+
310+
.. code-block:: twig
311+
312+
{{ t(message, parameters = [], domain = 'messages')|trans }}
313+
314+
``message``
315+
**type**: ``string``
316+
``parameters`` *(optional)*
317+
**type**: ``array`` **default**: ``[]``
318+
``domain`` *(optional)*
319+
**type**: ``string`` **default**: ``messages``
320+
321+
.. versionadded:: 5.2
322+
323+
The ``t()`` function was introduced in Symfony 5.2.
324+
325+
Creates a ``Translatable`` object that can be passed to the
326+
:ref:`trans filter <reference-twig-filter-trans>`.
327+
307328
Form Related Functions
308329
~~~~~~~~~~~~~~~~~~~~~~
309330

@@ -341,6 +362,8 @@ Makes a technical name human readable (i.e. replaces underscores by spaces
341362
or transforms camelCase text like ``helloWorld`` to ``hello world``
342363
and then capitalizes the string).
343364

365+
.. _reference-twig-filter-trans:
366+
344367
trans
345368
~~~~~
346369

@@ -349,14 +372,18 @@ trans
349372
{{ message|trans(arguments = [], domain = null, locale = null) }}
350373
351374
``message``
352-
**type**: ``string``
375+
**type**: ``string`` | ``Translatable``
353376
``arguments`` *(optional)*
354377
**type**: ``array`` **default**: ``[]``
355378
``domain`` *(optional)*
356379
**type**: ``string`` **default**: ``null``
357380
``locale`` *(optional)*
358381
**type**: ``string`` **default**: ``null``
359382

383+
.. versionadded:: 5.2
384+
385+
``message`` accepting ``Translatable`` as a valid type was introduced in Symfony 5.2.
386+
360387
Translates the text into the current language. More information in
361388
:ref:`Translation Filters <translation-filters>`.
362389

‎translation.rst

Copy file name to clipboardExpand all lines: translation.rst
+35Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,41 @@ 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+
.. versionadded:: 5.2
299+
300+
Translatable objects were introduced in Symfony 5.2.
301+
302+
Sometimes you may want to create a message, but at the time of creation aren't
303+
sure how it would be translated. For example, it could be translated multiple
304+
times if intended to be displayed to multiple users.
305+
306+
Using translatable objects also allows preparing translations without having a
307+
dependency on an entrypoint (such as a router) where the context for performing
308+
the translation is provided. For example, entities could prepare translatable
309+
strings (such as labels) without the need for a translator.
310+
311+
Instead of translating a string at the time of creation, a ``Translatable``
312+
object can be created that can then be translated when used. Later this message
313+
can be translated with a translator in either PHP or in Twig.
314+
315+
PHP::
316+
317+
$message = new Translatable('Symfony is great!');
318+
$message = t('Symfony is great!');
319+
320+
Translatable::trans($translator, $message);
321+
322+
Twig:
323+
324+
.. code-block:: html+twig
325+
326+
{% set message = t('Symfony is great!') %}
327+
328+
<h1>{{ message|trans }}</h1>
329+
295330
.. _translation-in-templates:
296331

297332
Translations in Templates

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.