1

On my Drupal 8 multilingual project, I have made a custom controller which create a basic page, but I can't figure out which parameter I must use in the routing. Is this _locale?

This is the definition of the route.

custom.route:
path: '{_locale}/portail'
defaults:
  _content: '\Drupal\my_module\Controller\DisplayController::display'
requirements:
_access: 'TRUE

This is the code I am using in the controller class.

public function display(Request $request, $locale) {
    var_dump($locale);die;
}

2 Answers 2

3

The language is detected and the prefix removed before routing. So you have a static route without parameter and in the controller you can get the prefix from the language manager:

  $langcode = $this->languageManager()->getCurrentLanguage()->getId();
2
  • 2
    Yes, this answers to both How do I define a route in a multi-lingual site? and How do I get the language set for a page returned from my controller?
    – apaderno
    Dec 26, 2017 at 10:01
  • Not sure this is what the OP is asking; but my guess would be he is asking how to do a multilingual route (with multilingual paths)? Which I suspect is sadly not possible in D8.
    – liquidcms
    Jan 25, 2021 at 23:02
0

To those like @liquidcms wondering how to do a multilingual path : you can make as many routes as languages, to the same controller.

custom.route.lang_one:
  path: '/path/in/this/language'
  defaults:
    _controller: '\Drupal\my_module\Controller\MyController::method'
  requirements:
    _permission: 'access content'

custom.route.lang_two:
  path: '/path/in/that/other/language'
  defaults:
    _controller: '\Drupal\my_module\Controller\MyController::method'
  requirements:
    _permission: 'access content'

And then you check the language in the controller as already mentioned by @4k4.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.