Get i18n string from arbitrary language (not current page's language)

I’m trying to create a link in a template from a page in one language to a page in another language, and I want the tooltip (HTML title attribute) to use a i18n string from the language of the linked page.

In other words, the English page should show a link to the Spanish page with title="Esta página tiene una traducción en español.", while the Spanish page should show a link to the English page with title="This page has a translation in English.".

When I use {{ i18n "pageHasTranslation" }}, I get the i18n value of pageHasTranslation for the current page’s language. I want to do something like {{ i18n "pageHasTranslation" "en" }}, but that doesn’t work. Maybe I could do something like the French month names example in the docs, but I don’t know how to access my i18n TOML files in that way.


I’m not sure if it is possible to do this using the i18n framework, but you can leverage the basic Hugo multilanguage facilities to achieve what you describe.

In your template:

{{ range .Translations }}
  {{ if ne .Language $.Site.Language }}
    <a href="{{ .Permalink }}" title="{{ .Site.Params.Translation" }}">{{ .Language.LanguageName }}</a> 
  {{ end }}
{{ end }}

in config.toml:

    languageName = "English"
    translation = "This page has a translation in English."
    languageName = "Español"
    translation = "Esta página tiene una traducción en español."
    languageName = "Русский"
    translation = "Эта страница переведена на русский язык."

For a page in English that also has versions in Spanish and Russian this template would create links to those, named “Español” and “Русский”, with appropriate title attributes.

… but we should add a site.Translate method, if someone could create the GitHub issue.

As in "translate this into .Site.Language"? Done.

Well, it would work the same as calling i18n from a template in that language.

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