symfonytwigsymfony4twig-extension

How to enable the Intl extension from twig/intl-extra


I've been running through the docs and couldn't find where to init twig/intl-extra

The twig doc here says to either use use Twig\Extra\Intl\IntlExtension; or to add the extension explicitly on the Twig environment

$twig = new \Twig\Environment(...);
$twig->addExtension(new IntlExtension());

I would like tpo use the second case and add explicitly to the twig environment.

In which file should I do it?


Solution

  • When using twig with Symfony, you should register the extension as a service in your services.yaml file.

    If installed using the twig/extensions flex recipe, it will provide the config/packages/twig_extensions.yaml file, you just have to uncomment the desired extensions (see the original comment on github).

    The default configuration for reference:

    services:
        _defaults:
            public: false
            autowire: true
            autoconfigure: true
    
        # Uncomment any lines below to activate that Twig extension
        #Twig\Extensions\ArrayExtension: null
        #Twig\Extensions\DateExtension: null
        #Twig\Extensions\IntlExtension: null
        #Twig\Extensions\TextExtension: null