Search code examples
phpsymfony4

Symfony 4: use localizeddate


I am trying to use localizeddate to show a date in French on my website:

<td>{{ saison.start|localizeddate('medium', 'none') }}</td>

I followed the various documentations and:

  • I installed the PHP Intl extension: sudo pacman -S php-intl
  • I installed Twig extensions on Symfony: composer require twig/extensions
  • As well in Intl extension: composer require symfony/intl

I also edited config/services.yaml to change the locale, but it does not seem to have an effect:

parameters:
    locale: 'fr'

I enabled Date and Intl extensions in files configpackages\twig_extensions.yaml`:

services:
    _defaults:
        public: false
        autowire: true
        autoconfigure: true

    # Uncomment any lines below to activate that Twig extension
    #Twig\Extensions\ArrayExtension: ~
    Twig\Extensions\DateExtension: ~
    Twig\Extensions\IntlExtension: ~
    #Twig\Extensions\TextExtension: ~

But I still have the date shown in english in the output file.

I also tried to specify more clearly the locale in localizeddate like this:

<td>{{ saison.start|localizeddate('medium', 'none', 'fr') }}</td>

But in this case I get an error:

An exception has been thrown during the rendering of a template ("The Symfony\Component\Intl\DateFormatter\IntlDateFormatter::__construct() method's argument $locale value 'fr' behavior is not implemented. Only the locale "en" is supported. Please install the "intl" extension for full localization capabilities.").

I don't know what is missing.


Solution

  • The filter name has changed. You should now use format_datetime provided by the twig/intl-extra package.

    https://twig.symfony.com/doc/2.x/filters/format_datetime.html