Guidance on translations

I’ve been starting to use translations, I get the list of translations for articles, but as I don’t have all the articles translated, when clicking on an article, let’s say in Spanish, and try to link to an article that it’s in English via {{<ref "">}} it will complain… however if I force {{<ref path="" lang="en">}} it will work.

It’s painful but there’s that workaround, but the other issue is related to other pages like ‘search’, static pages,etc…

I would expect Hugo to GET the default translation (English) when there’s no alternate one in the language being used…

Is that possible or it’s a bug/request for enhancement to address?

Thanks in advance

Hugo does, what your template tells it to do. So if the layout files have a routine in them to get the default translation then that will happen. You will have to post your repo or layout files here for somebody to dig through that.

On your “dot” you have all translations of the current page available, so what you do with it is something like the following in the shortcode:

{{ $translations := index .Translations "en" }}
{{ range .Translations }}
  {{ if eq .Lang "it" }}
    {{ $translation = . }}
  {{ end }}
{{ end }}

After that, if there is an Italian translation available then $translation.Permalink will link to it, if not, then it will link to the English version. Add some shortcode options or configuration parameters instead of en and it to make it more dynamic.

Thanks David
I’m using GitHub - adityatelange/hugo-PaperMod: A fast, clean, responsive Hugo theme. as the theme and the translations loop is:

{{- if .IsTranslated -}}
{{- if (ne .Layout "search") }}
{{- if or $ (.Param "ShowReadingTime") (not .Date.IsZero) }}&nbsp;|&nbsp;{{- end -}}
{{- end }}
<ul class="i18n_list">
    {{- i18n "translations" | default "Translations" }}:
    {{- range .Translations }}
        <a href="{{ .Permalink }}">
            {{- if (and $.Site.Params.displayFullLangName (.Language.LanguageName)) }}
            {{- .Language.LanguageName | emojify -}}
            {{- else }}
            {{- .Lang | title -}}
            {{- end -}}
    {{- end }}
{{- end -}}

I was wondering that this, will list the translations for an article, but still not cover the issue I had…
from an article in English, if I want to link to an article in Spanish or French, etc I would put something like:

{{<ref “”>}} and this will fail, as that article doesn’t exist in the language of the source document (english), but only exists as another language.

I’m updating my question as I saw that it’s missing some of the codes there

If you search long enough you find the solution :wink: Much of it is very hidden in the docs. relref has a language parameter. I never tested it, but I think this might help:

Thanks, that’s the one I stated I was using… the problem is that I would make use of it, when I specifically want to link to one article in one specific language… but when I link to an article, via the “md file”, I would expect the link to get me any language of the article if the same as the origin one is not there, or doesn’t exist (which is my case when linking from a Spanish article to an English one and there’s no Spanish translation)

Ok, so slowly we arrive somewhere :slight_smile:

  • You are in a Spanish post
  • you link to another Spanish post
  • that post does not exist, so you expect a link to english

right? In that case English is the sites main language, you have the following somewhere in your config:

    title = 'My blog'
    weight = 1

If your English is en-us or anything else, keep that on notes.

I don’t see any way to get the defaultContentLanguage from the root section of your config, so we go a “hard-coded” way for now.

NOTE: this is hobbled together and untested, so don’t give up immediately if it does not work. MIght have typoes.

call this layouts/shortcodes/langref.html for now

{{- $reference := .Get "reference" -}}
{{- $defaultlanguage := "en" -}}{{- /* <-- put your fallback language code here  */ -}}
{{- $language := .Get "language" | default $defaultlanguage -}}
{{- $context := $ -}}{{- /* <-- I could be wrong with this one, but inside of the shortcode it should be the current page  */ -}}
{{- with site.GetPage $reference }}{{- /* loading the referenced page */ -}}
  {{- if .IsTranslated }}{{- /* checking if the page has translations */ -}}
    {{- with index .Translation $language }}{{- /* translation is available, so let's do this */ -}}
      {{- ref $context (dict "path" $reference "lang" $language) -}}
    {{- else -}}
      {{- ref $context (dict "path" $reference "lang" $defaultlanguage) -}}
    {{- end -}}
  {{- else -}}{{- /* no translations, so let's print out what ref would do at this point */ -}}
    {{- ref $context $reference -}}
  {{- end -}}
{{- else -}}{{- /* referenced page is not available, so let's print out what ref would do at this point */ -}}
  {{- ref $context $reference -}}
{{- end -}}

Then call it with {{< langref reference="/" lang="es" >}}.

What it should do:

  • check if “/” exists or go into “blind” ref creation (as if you called {{< ref “”>}})
  • check if is translated or return the default reference
  • check if language exist and either return that or the default language

Keep an eye on your template tags, those - everywhere will result in the shortcode returning only your HTML in the end, not plenty of empty new lines.

Also, not 100% sure if that returns a formatted link or just an URL, you might need to add some HTML at the end.

Edit: I fixed some obvious errors in the shortcode. Still not sure if it works, I have no language site.


I’ll check it, the idea of using a custom shortcode for the checks is something I didn’t had in mind, but it’s a clever one, thanks a lot!

I’m working on something along the lines:

{{- $reference := .Get 0 -}}
{{- $.Site.Params.languages }}

{{- $context := $ -}}{{- /* <-- I could be wrong with this one, but inside of the shortcode it should be the current page  */ -}}

{{- if site.GetPage $reference }}{{- /* loading the referenced page */ -}}
{{ $reference }}

{{- else -}}{{- /* referenced page is not available, so let's print out what ref would do at this point */ -}}

{{- range $.Site.Params.languages -}}
{{- ref $context (dict "path" $reference "lang" "{{ . }}") -}}

{{ end }}

{{- /* site.GetPage $reference  */ -}}
{{- end -}}

Of course it doesn’t work but I want it for ‘art’ (my shortcode) to use the first (and unique) argument to find reference in same language as linking article, and if not, iterate over the site languages to find the first available.

Is not working but I think that this might work fine

Any hint welcome :slight_smile:

site config

weight = 1
contentDir = 'content/en'
weight = 2
contentDir = 'content/es'
weight = 3
contentDir = 'content/de'


{{- with $path := .Get 0 -}}
  {{- $link := "" -}}
  {{- with site.GetPage $path -}}
    {{- $link = .RelPermalink -}}
  {{- else -}}
    {{- range where site.Sites "LanguagePrefix" "ne" site.LanguagePrefix -}}
      {{- if not $link -}}
        {{- with .GetPage $path -}}
          {{- $link = .RelPermalink -}}
        {{- end -}}
      {{- end -}}
    {{- end -}}
  {{- end -}}
  {{- with $link -}}
    {{ . }}
  {{- else -}}
    {{- errorf "The %q shortcode was unable to find %q. See %s" $.Name $path $.Position -}}
  {{- end -}}
{{- else -}}
  {{- errorf "The %q shortcode requires a positional parameter. See %s" .Name .Position -}}
{{- end -}}

This looks for the page in the current site (language). If it doesn’t find a match, it ranges through the other sites (languages) ordered by the weight in the [language] table. And it works fine for single language sites as well.

1 Like

The above code was also giving me issues, but I finally got into this… that seems to work:

{{- /* Get path of the article requested */ -}}
{{- $path := .Get 0 -}}

{{- if site.GetPage $path }}
    {{ $path }} {{- /* Article exists, link it */ -}}
{{- else -}}
    {{- /* Article doesn't exist, loop over languages */ -}}
    {{- /* Get total languages supported */ -}}
    {{- $totlang := len .Site.Languages -}}

    {{- /* Iterate over them */ -}}
    {{ range $index, $num := (seq $totlang) }}
        {{ if (index site.Sites $index ).GetPage $path }}
            {{- /* Article in language is valid */ -}}
            {{- with (index site.Sites $index ).GetPage $path -}}
                {{- /* Return path to article */ -}}
                {{ .Permalink | relURL}}

                {{- /* Alter index so that we're finished */ -}}
                {{ $index := $totlang}}
        {{ end }}
    {{ end }}
{{ end }}

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