439439Creates a ``Translatable `` object that can be passed to the
440440:ref: `trans filter <reference-twig-filter-trans >`.
441441
442+ .. configuration-block ::
443+
444+ .. code-block :: yaml
445+
446+ # translations/blog.en.yaml
447+ message : Hello %name%
448+
449+ .. code-block :: xml
450+
451+ <!-- translations/blog.en.xlf -->
452+ <?xml version =" 1.0" encoding =" UTF-8" ?>
453+ <xliff version =" 1.2" xmlns =" urn:oasis:names:tc:xliff:document:1.2" >
454+ <file source-language =" en" datatype =" plaintext" original =" file.ext" >
455+ <body >
456+ <trans-unit id =" message" >
457+ <source >message</source >
458+ <target >Hello %name%</target >
459+ </trans-unit >
460+ </body >
461+ </file >
462+ </xliff >
463+
464+ .. code-block :: php
465+
466+ // translations/blog.en.php
467+ return [
468+ 'message' => "Hello %name%",
469+ ];
470+
471+ Using the filter will be rendered as:
472+
473+ .. code-block :: twig
474+
475+ {{ t(message = 'message', parameters = {'%name%': 'John'}, domain = 'blog')|trans }}
476+ {# output: Hello John #}
477+
442478 Form Related Functions
443479~~~~~~~~~~~~~~~~~~~~~~
444480
@@ -516,6 +552,42 @@ trans
516552Translates the text into the current language. More information in
517553:ref: `Translation Filters <translation-filters >`.
518554
555+ .. configuration-block ::
556+
557+ .. code-block :: yaml
558+
559+ # translations/messages.en.yaml
560+ message : Hello %name%
561+
562+ .. code-block :: xml
563+
564+ <!-- translations/messages.en.xlf -->
565+ <?xml version =" 1.0" encoding =" UTF-8" ?>
566+ <xliff version =" 1.2" xmlns =" urn:oasis:names:tc:xliff:document:1.2" >
567+ <file source-language =" en" datatype =" plaintext" original =" file.ext" >
568+ <body >
569+ <trans-unit id =" message" >
570+ <source >message</source >
571+ <target >Hello %name%</target >
572+ </trans-unit >
573+ </body >
574+ </file >
575+ </xliff >
576+
577+ .. code-block :: php
578+
579+ // translations/messages.en.php
580+ return [
581+ 'message' => "Hello %name%",
582+ ];
583+
584+ Using the filter will be rendered as:
585+
586+ .. code-block :: twig
587+
588+ {{ 'message'|trans(arguments = {'%name%': 'John'}, domain = 'messages', locale = 'en') }}
589+ {# output: Hello John #}
590+
519591 yaml_encode
520592~~~~~~~~~~~
521593
@@ -563,6 +635,16 @@ abbr_class
563635Generates an ``<abbr> `` element with the short name of a PHP class (the
564636FQCN will be shown in a tooltip when a user hovers over the element).
565637
638+ .. code-block :: twig
639+
640+ {{ 'App\\Entity\\Product'|abbr_class }}
641+
642+ The above example will be rendered as:
643+
644+ .. code-block :: html
645+
646+ <abbr title =" App\Entity\Product" >Product</abbr >
647+
566648abbr_method
567649~~~~~~~~~~~
568650
@@ -577,6 +659,16 @@ Generates an ``<abbr>`` element using the ``FQCN::method()`` syntax. If
577659``method `` is ``Closure ``, ``Closure `` will be used instead and if ``method ``
578660doesn't have a class name, it's shown as a function (``method() ``).
579661
662+ .. code-block :: twig
663+
664+ {{ 'App\\Controller\\ProductController::list'|abbr_method }}
665+
666+ The above example will be rendered as:
667+
668+ .. code-block :: html
669+
670+ <abbr title =" App\Controller\ProductController" >ProductController</abbr >
671+
580672format_args
581673~~~~~~~~~~~
582674
@@ -713,6 +805,21 @@ serialize
713805Accepts any data that can be serialized by the :doc: `Serializer component </serializer >`
714806and returns a serialized string in the specified ``format ``.
715807
808+ For example::
809+
810+ $object = new \stdClass();
811+ $object->foo = 'bar';
812+ $object->content = [];
813+ $object->createdAt = new \DateTime('2024-11-30');
814+
815+ .. code-block :: twig
816+
817+ {{ object|serialize(format = 'json', context = {
818+ 'datetime_format': 'D, Y-m-d',
819+ 'empty_array_as_object': true,
820+ }) }}
821+ {# output: {"foo":"bar","content":{},"createdAt":"Sat, 2024-11-30"} #}
822+
716823 .. _reference-twig-tags :
717824
718825Tags
0 commit comments