Discuss your project
Partitech Sonata Extra Bundle

Sonata Extra Bundle: language selector

Sonata Extra Bundle provides a feature-rich language selector, compatible with hybrid pages. This document describes the steps to activate and use the language selector in your Sonata application. Language selector configuration Integration with hybrid pag...

Contrôles de compatibilité Symfony et Sonata

Sonata Extra Bundle provides a feature-rich language selector, compatible with hybrid pages. This document describes the steps to activate and use the language selector in your Sonata application.

Language selector configuration

Integration with hybrid pages

The language selector can integrate perfectly with your existing controllers and routes. Once you have a controller with a defined route, you can find and manage it in the Sonata page manager. The Sonata Extra Bundle allows customization of action URLs.

Example:

  • French version of a blog article page: /blog-mon-article/{slug} languageSwitcher_localized_route_fr.png
  • English version of the same page: /blog-my-article/{slug} languageSwitcher_localized_route_fr.png

The language selector automatically detects localized pages based on the configuration. languageSwitcher_menuDropdown.png

Location service injection

To enable the language change feature, inject the LocaleService into your application:


 use Partitech\SonataExtra\Service\LocaleService;
 

Use the service to inject the calculated localized route into the request object:


$linkHeader = $this->localeService->languageSelectorGetHeaderLinks($tagEntity, 'sonata_extra_blog_tag');
$response->headers->set('Link', $linkHeader);

The request object is then used in the language change block to display the language selection dropdown menu.

Process the entity and associate the route

The main feature lies in the handling of entities. Whenever you manipulate an entity, the process checks the related localized items. To enable this, inform the entity of the associated routes:

Example:

class SonataClassificationCategory extends BaseCategory
{
use EntityTranslationTrait;

    private array $baseRouteName = [
        'sonata_extra_blog_category',
        'sonata_extra_blog_search'
    ];
    // ...
}

The process can handle different types of route configurations. Set an argument with a corresponding accessor to facilitate this.

Example: For an itinerary like /blog-category/{slug}, the process uses the $entity->getSlug() function to pass it to the URL generator.

Adapt the example to the installed version

The Symfony and Sonata examples remain tied to the versions indicated in the article. Symfony 4.x and Symfony 6.2 are no longer maintained: for a current project, use a maintained branch and the documentation that exactly matches your dependencies.

php bin/console about
composer show symfony/framework-bundle sonata-project/admin-bundle

Before resuming the code, check the signatures, services, routes, and templates concerned. Then add a functional test covering permissions, HTTP code, and the visible result.

Reference: maintained Symfony versions.

Share this article