Setting the x-default in the .IsTranslated loop as I want

{{- if .IsTranslated -}}
    {{ range first 1 .AllTranslations }}
    <link rel="alternate" hreflang="x-default" href="{{ .Permalink }}" />
    {{- end -}}
    {{ range .AllTranslations }}
    <link rel="alternate" hreflang="{{ .Language.Lang }}" href="{{ .Permalink }}" />
    {{- end -}}
{{ end }}

The code above finds which other pages the multi lang page is linked to and assigns values to them for SEO.

<link rel="alternate" hreflang="x-default" href="http://localhost:1313/tr/" />
<link rel="alternate "hreflang="tr" href="http://localhost:1313/tr/" />
<link rel="alternate "hreflang="en" href="http://localhost:1313/" />

As you can see, the "x-default" parameter corresponds to the TR page.

Instead of the TR page I want to make this link in English, i.e. without any url prefix (http://localhost:1313/)

The default language in my config.toml file is already English.

How can I do this?

According to Google’s documentation, the x-default value for the hreflang attribute is for home pages that:

…point visitors to localized pages, either via redirects or by changing the content to reflect the user’s language."

The example in Google’s documentation has three languages: en-gb, en-us, and en-au.

But the example has four sites:

  • https://example.com/en-gb
  • https://example.com/en-us
  • https://example.com/en-au
  • https://example.com/

So it makes sense to set x-default for the last site’s home page, because the last site’s home page “points visitors to localized pages, either via redirects or by changing the content to reflect the user’s language.”

If this were a Hugo site withdefaultContentLanguage = 'en-gb', the content would be available in three places only.

With defaultContentLanguageInSubdir = true:

  • https://example.com/en-gb
  • https://example.com/en-us
  • https://example.com/en-au

With defaultContentLanguageInSubdir = false:

  • https://example.com/
  • https://example.com/en-us
  • https://example.com/en-au

In either case above, it doesn’t make sense to set any of them to x-default, because none of them “points visitors to localized pages, either via redirects or by changing the content to reflect the user’s language.”

The only place it makes sense to set x-default is in the redirect file created in the public root when defaultContentLanguageInSubdir = true. So you must override the alias template by creating layouts/alias.html. Keep in mind that this template is used for all alias files created by the site, so you must have a conditional block based on .IsHome and lang detection.

In short, I wouldn’t bother.

3 Likes
{{- if .IsTranslated -}}
    {{ range .AllTranslations }}
    {{- if eq .Language.Lang "en" -}}
    <link rel="alternate" hreflang="x-default" href="{{ .Permalink }}" />
    {{- end -}}
    <link rel="alternate" hreflang="{{ .Language.Lang }}" href="{{ .Permalink }}" />
    {{- end -}}
{{ end }}

I solved the problem with this code, thank you for what you wrote anyway.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.