5

I have a Hugo site with a few translated pages. Not all the pages are translated, since I don't always have volunteers for every page in every language.

In the menu, I have links to some pages that I'd like to redirect to English if the local translation is not there (so there's always a link, but it might not be translated).

# Force to have /en/my-page and /fr/my-page routes, even for default language.
defaultContentLanguageInSubdir= true

# English is the default language
defaultContentLanguage = "en"

# This page exists in en and fr, but not de
[[menu.shortcuts]]
name = "Licence"
url = "/licence"

[Languages]
[Languages.en]
languageName = "English"

[Languages.fr]
languageName = "Français"

[Languages.de]
languageName = "Deutsch"

On the English and French page, this works fine, the menu link goes to /en/licence and /fr/licence. But on the German page, it goes to /de/licence, which is a 404.

Is there a way to redirect missing pages to the corresponding page in the default language?

Inductiveload
  • 6,094
  • 4
  • 29
  • 55

1 Answers1

2

Actually there is no default hugo solution. I've spend a lot of time to figure out how to reach the goal and don't copy data content all the time for all languages, because it's is a way to difficult to controll different versions.

So I simply check if it's an internal link and the page under the current language exists and if so then I create a link for this language if no then I simply assign a default one.

{{ $link := .link }}
{{ if and ( $.Site.GetPage ( substr ( $link ) 0 -1 ) ) ( ne ( $.Site.BaseURL | relLangURL ) "/" ) ( not ( in $link "://" ) ) }}
    {{ $link = ( print ( $.Site.BaseURL | relLangURL ) $link ) }}
{{ end }}

All my links have / symbol in the end, so if your links look like /blog and not as /blog/ then just change ( substr ( $link ) 0 -1 ) to just $link.

I'm a hugo newbie, sorry if it's not elegant solution but it works for me so far.

Anna Bakurova
  • 37
  • 1
  • 11