I need to switch user's language programmatically. The issue is I don't want it as an url part, and unable to switch it even with the regular language switcher block.

What I've try:

  • admin/config/regional/language/detection: Disable url, Enable Session
  • Now the Language Switcher core block adds ?language= to the current path. But this setting does not persist between pages, and does not appears in the session.
  • manual setup of $_SESSION['language'] does nothing
  • \Drupal::languageManager()->setConfigOverrideLanguage($language_manager->getLanguage($langcode)) does nothing

Drupal version is 8.beta2

  • How do you get the language? – penyaskito Dec 21 '14 at 1:09

I don't really understand your use case correctly, but probably you need to implement a language negotiation manager. See an example at /core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationBrowser.php.

If you also need to change the url for adding language, or changing how the links in language switcher work, you may need to override some more methods. See an example at /core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationUrl.php.

| improve this answer | |

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.