7

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.

13

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();
  • It works but you can't pass 'nz', because it's not a language-code. It would be en-au for example – Stephan Hofmann Sep 1 '16 at 10:25
  • My bad, i just check my language. :) – MrD Sep 1 '16 at 10:37
1

Within your controller method:

$url = \Drupal\Core\Url::fromRoute('<front>')->toString();
return new RedirectResponse($url);

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.