352352Creates a ``Translatable `` object that can be passed to the
353353:ref: `trans filter <reference-twig-filter-trans >`.
354354
355+ .. configuration-block ::
356+
357+ .. code-block :: yaml
358+
359+ # translations/blog.en.yaml
360+ message : Hello %name%
361+
362+ .. code-block :: xml
363+
364+ <!-- translations/blog.en.xlf -->
365+ <?xml version =" 1.0" encoding =" UTF-8" ?>
366+ <xliff version =" 1.2" xmlns =" urn:oasis:names:tc:xliff:document:1.2" >
367+ <file source-language =" en" datatype =" plaintext" original =" file.ext" >
368+ <body >
369+ <trans-unit id =" message" >
370+ <source >message</source >
371+ <target >Hello %name%</target >
372+ </trans-unit >
373+ </body >
374+ </file >
375+ </xliff >
376+
377+ .. code-block :: php
378+
379+ // translations/blog.en.php
380+ return [
381+ 'message' => "Hello %name%",
382+ ];
383+
384+ Using the filter will be rendered as:
385+
386+ .. code-block :: twig
387+
388+ {{ t(message = 'message', parameters = {'%name%': 'John'}, domain = 'blog')|trans }}
389+ {# output: Hello John #}
390+
355391 Form Related Functions
356392~~~~~~~~~~~~~~~~~~~~~~
357393
@@ -429,6 +465,42 @@ trans
429465Translates the text into the current language. More information in
430466:ref: `Translation Filters <translation-filters >`.
431467
468+ .. configuration-block ::
469+
470+ .. code-block :: yaml
471+
472+ # translations/messages.en.yaml
473+ message : Hello %name%
474+
475+ .. code-block :: xml
476+
477+ <!-- translations/messages.en.xlf -->
478+ <?xml version =" 1.0" encoding =" UTF-8" ?>
479+ <xliff version =" 1.2" xmlns =" urn:oasis:names:tc:xliff:document:1.2" >
480+ <file source-language =" en" datatype =" plaintext" original =" file.ext" >
481+ <body >
482+ <trans-unit id =" message" >
483+ <source >message</source >
484+ <target >Hello %name%</target >
485+ </trans-unit >
486+ </body >
487+ </file >
488+ </xliff >
489+
490+ .. code-block :: php
491+
492+ // translations/messages.en.php
493+ return [
494+ 'message' => "Hello %name%",
495+ ];
496+
497+ Using the filter will be rendered as:
498+
499+ .. code-block :: twig
500+
501+ {{ 'message'|trans(arguments = {'%name%': 'John'}, domain = 'messages', locale = 'en') }}
502+ {# output: Hello John #}
503+
432504 yaml_encode
433505~~~~~~~~~~~
434506
@@ -476,6 +548,11 @@ abbr_class
476548Generates an ``<abbr> `` element with the short name of a PHP class (the
477549FQCN will be shown in a tooltip when a user hovers over the element).
478550
551+ .. code-block :: twig
552+
553+ {{ 'App\\Entity\\Product'|abbr_class }}
554+ {# output: <abbr title="App\Entity\Product">Product</abbr> #}
555+
479556 abbr_method
480557~~~~~~~~~~~
481558
@@ -490,6 +567,11 @@ Generates an ``<abbr>`` element using the ``FQCN::method()`` syntax. If
490567``method `` is ``Closure ``, ``Closure `` will be used instead and if ``method ``
491568doesn't have a class name, it's shown as a function (``method() ``).
492569
570+ .. code-block :: twig
571+
572+ {{ 'App\\Controller\\ProductController::list'|abbr_method }}
573+ {# output: <abbr title="App\Controller\ProductController">ProductController</abbr> #}
574+
493575 format_args
494576~~~~~~~~~~~
495577
@@ -621,6 +703,21 @@ serialize
621703Accepts any data that can be serialized by the :doc: `Serializer component </serializer >`
622704and returns a serialized string in the specified ``format ``.
623705
706+ For example::
707+
708+ $object = new \stdClass();
709+ $object->foo = 'bar';
710+ $object->content = [];
711+ $object->createdAt = new \DateTime('2024-11-30');
712+
713+ .. code-block :: twig
714+
715+ {{ object|serialize(format = 'json', context = {
716+ 'datetime_format': 'D, Y-m-d',
717+ 'empty_array_as_object': true,
718+ }) }}
719+ {# output: {"foo":"bar","content":{},"createdAt":"Sat, 2024-11-30"} #}
720+
624721 .. _reference-twig-tags :
625722
626723Tags
0 commit comments