10

i have a question about redirecting to a page with a different language. I found this old post for drupal 7 but I doesn't really help me - https://www.drupal.org/node/1280468

Now we are in Drupal 8 and the redirect works like this:

return new RedirectResponse(\Drupal::url('<front>'))

But how can I set the language for the redirect? In my code I have the languagecode like NZ for New Zealand.

2 Answers 2

20

You can try this code

 <?php

 use Drupal\Core\Url;
 use Symfony\Component\HttpFoundation\RedirectResponse;

 $language = Drupal::languageManager()->getLanguage('vi');
 $url = Url::fromRoute('<front>', [], ['language' => $language]);
 $response = new RedirectResponse($url->toString());
 $response->send();
1
  • It works but you can't pass 'nz', because it's not a language-code. It would be en-au for example
    – Yetispapa
    Sep 1, 2016 at 10:25
-1

Within your controller method:

$url = \Drupal\Core\Url::fromRoute('<front>')->toString();
return new RedirectResponse($url);
1
  • doesn't work, return current language Jan 21, 2021 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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