I have a route of a page, and I want get the link for it on every language of my site.

On my site the contact page is: en/contact for English, and es/contactenos for Spanish.

I am trying using the following code, but I am getting just the URL for the current language set for the site.

$route = 'contact.site_page';
$url = new Url($route);
$uri = $url->toString();

How do I get it for every language available for the site?


I found the solution myself, with the following code.

// Get all languages of the site
$languages = \Drupal::languageManager()->getLanguages();

// Route of contact for example:
$route = 'contact.site_page';
foreach ($languages as $language) {
  $url = new Url($route, [], ['language' => $language]);
  $uri = $url->toString();

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.