We will see how to set up a multilingual admin interface with a language selection button (language switcher).
Installing translationBundle
composer require sonata-project/translation-bundle
bin/console assets:install
The language switcher requires a specific twig filter.
Otherwise, you will get an error like:
Unknown "language_name" filter.
Then, you need to install these two bundles
composer require twig/intl-extra
composer require twig/extra-bundle
bin/console cache:clear
We add the default configuration
# config/packages/sonata_translation.yaml
sonata_translation:
locales: [en, fr, it, nl, es]
default_locale: en
# change default behavior for translated field filtering.
default_filter_mode: gedmo # must be either 'gedmo' or 'knplabs', default: gedmo
# here enable the types you need
gedmo:
enabled: true
knplabs:
enabled: true
#phpcr:
# enabled: true
And we include styles in the admin
# config/packages/sonata_admin.yaml
sonata_admin:
assets:
extra_stylesheets:
- bundles/sonatatranslation/css/sonata-translation.css
We override the display of the sonata template
# config/packages/sonata_admin.yaml
sonata_admin:
templates:
layout: '@SonataTranslation/standard_layout.html.twig'
We add the routes
# config/routes.yaml
sonata_translation:
resource: '@SonataTranslationBundle/Resources/config/routes.yaml'
And we enable the language switcher
# config/packages/sonata_translation.yaml
sonata_translation:
locale_switcher: true
And the last point to avoid the error
Argument 5 passed to Twig\Extra\Intl\IntlExtension::formatDateTime() must be of the type string, null given,
You will have to load the twig/extra-bundle before SonataIntlBundle so that sonataIntl's formatDateTime function overrides the one from TwigExtraBundle.
This gives us:
Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle::class => ['all' => true],
Twig\Extra\TwigExtraBundle\TwigExtraBundle::class => ['all' => true],
Sonata\IntlBundle\SonataIntlBundle::class => ['all' => true],
Sonata\TranslationBundle\SonataTranslationBundle::class => ['all' => true],
