Jump to content

multilange urls code


kaz
 Share

Recommended Posts

I have a problem with the full path (urls) to multilanguage pages. How to set a multilanguage path? I could not find an answer in the documentation.

<?php foreach($languages as $language) {
        echo "<link rel='alternate' hreflang='$language->title' href='{$language->title}$page->url'>";
    }
    ?>

hreflang='$language->title' prints the correct number of available languages, only the href's are missing. Can anyone help?

Link to comment
Share on other sites

this is how I did it

// Output a default alternate link for search engines that don't support hreflang
echo "<link rel='alternate' hreflang='x-default' href='$page->httpUrl' />";

// Loop through all available languages
foreach($languages as $language) {

  if(!$page->viewable($language)) continue;

  $url = $page->localHttpUrl($language); 

  $hreflang = $language->name;

  // Replace "default" with "de" (assumes "de" is the default language)
  $hreflang = ($hreflang == 'default') ? "de" : $hreflang;

  // Output the alternate link for the current language
  echo "<link rel='alternate' hreflang='$hreflang' href='$url' />";
}

 

  • Like 1
Link to comment
Share on other sites

@TomPich Yes, but it only gives me back the ID. Most of the options in the documentation has printed the ID.

@ngrmm It works, the result:

<link rel='alternate' hreflang='x-default' href='https://domain.local:8890/leistungen/' />
<link rel='alternate' hreflang='de' href='https://domain.local:8890/leistungen/' />
<link rel='alternate' hreflang='english' href='https://domain.local:8890/en/services/' />

That's perfect. The only thing I changed is:

$hreflang = $language->title;

to get

<link rel='alternate' hreflang='en' href='https://domain.local:8890/en/services/' />

Thanks a lot!

  • Like 1
Link to comment
Share on other sites

@kaz beware

// hreflang="english" is not valid
// it should be hreflang="en"

<link rel="alternate" hreflang="en" href="https://domain.local:8890/en/services/" />

 

Link to comment
Share on other sites

On 7/1/2025 at 5:09 PM, ngrmm said:

@kaz beware

// hreflang="english" is not valid
// it should be hreflang="en"

<link rel="alternate" hreflang="en" href="https://domain.local:8890/en/services/" />

 

For exactly that reason we're always adding an ISO-Code field to languages and use that as the hreflang value. If you need help adding this, one hint: you need to show system templates temporarily to be able to mess with the language template.

Link to comment
Share on other sites

HI

i have a little idea of the reason why you're using $language->title, ths language neme being default for your default language 🙂

here comes an simple example of what i do for a language switcher (apparently what you're working on)

<?php
$lg = 0;
foreach($languages as $language)
{
    if( ! $page->viewable($language) ) continue;
    $url = $page->localUrl($language);
    $iso = $language->name == 'default' ? 'fr' : $language->name;
    // here, fr because i'm french but just use your website default language iso code
    if($language->id != $user->language->id)
    {
        echo '<a hreflang="' . $iso . '" href="' . $url . '" title="' . $language->title . '">' . $iso . '</a>' . "\n";
        // echo '<a hreflang="' . $iso . '" href="' . $url . '" title="' . $language->title . '">' . $language->title . '</a>' . "\n";
    }
    else // not very useful to add a link to the language you're on 🙂
    {
        echo '<span>' . $iso . '</span>' . "\n";
        // echo '<span>' . $language->title . '</span>' . "\n";
    }
    $lg++;
}
?>

have a nice day

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...