Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tpl/lang: Add lang.Language template function #12212

Open
jmooring opened this issue Mar 7, 2024 · 2 comments
Open

tpl/lang: Add lang.Language template function #12212

jmooring opened this issue Mar 7, 2024 · 2 comments

Comments

@jmooring
Copy link
Member

jmooring commented Mar 7, 2024

This is a pretty good idea from the forum, allowing you to get a translated language name from a language tag. One example (which may not be great) is a language switcher that displays "french" in three different languages on a three language site. For example:

Screencast.from.03-07-2024.12.32.25.PM.webm

Instead of using translation tables, use Go's language package that we already use for other things (no new dependencies).

Signature:

lang.LanguageName SOURCE [TARGET]

If TARGET is not provided, set to SRC.

{{ lang.LanguageName "pt-br" "en-US" }} → Brazilian Portuguese
{{ lang.LanguageName "pt" "en" }} → Portuguese
{{ lang.LanguageName "pt" }} → português

{{ lang.LanguageName "de-DE" "fr-FR" }} → allemand
{{ lang.LanguageName "de" "fr" }} → allemand
{{ lang.LanguageName "de" }} → Deutsch

A language switcher would look like this:

{{ if .Translations }}
  {{ $targetLang := .Language.LanguageCode }}
  <ul>
    {{ range .AllTranslations }}
      {{ $sourceLang := .Language.LanguageCode }}
      <li><a href="{{ .RelPermalink }}">{{ lang.LanguageName $sourceLang $targetLang }}</a></li>
    {{ end }}
  </ul>
{{ end }}

Reference: https://discourse.gohugo.io/t/get-a-translated-language-name-from-its-code/48697/8

@bep
Copy link
Member

bep commented Mar 13, 2024

To me it would make slightly more sense to have this as a method on Language (or maybe both):

{{ if .Translations }}
  {{ $targetLang := .Language }}
  <ul>
    {{ range .AllTranslations }}
      <li><a href="{{ .RelPermalink }}">{{ .LanguageName $targetLang }}</a></li>
    {{ end }}
  </ul>
{{ end }}

Which means that we would need a better name than LanguageName (which already exists), maybe GetLanguageName.

We have a related issue in #7844

@bep bep modified the milestones: v0.124.0, v0.125.0 Mar 18, 2024
jmooring added a commit to jmooring/hugo that referenced this issue Jun 10, 2024
@jmooring
Copy link
Member Author

My preference would be to implement this as function first. It seems like this would be the most flexible, and the API in a language switcher seems clean to me:

{{ if .Translations }}
  {{ $targetLang := .Language.LanguageCode }}
  <ul>
    {{ range .AllTranslations }}
      {{ $sourceLang := .Language.LanguageCode }}
      <li><a href="{{ .RelPermalink }}">{{ lang.LanguageName $sourceLang $targetLang }}</a></li>
    {{ end }}
  </ul>
{{ end }}

I have a working implementation if you wish to proceed.

Having said that, the need for this functionality was expressed here, but I haven't seen any similar requests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants