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

Enable cross-site translations #12444

Closed
wants to merge 1 commit into from

Conversation

jmooring
Copy link
Member

This introduces two methods to enable cross-site translations:

  • Site.Translate returns a translated string in the language of the given site.

    {{ range .AllTranslations }} {{ .Site.Translate "cat" 6 }} {{ end }}

  • Sites.GetSite returns the site corresponding to the given language. This method simplifies cross-site translations.

    {{ with .Sites.GetSite "en" }} {{ .Translate "dog" 7 }} {{ end }}

Closes #7844


// Translate returns a translated string for id.
func (s *Site) Translate(id string, args ...any) (string, error) {
ctx := context.Background()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add ctx as the first arg in Translate. It will be "auto injected".

That said. I don't remember the original issue, but looking at the below I would say that this methods belongs on the Language struct.

You would need to say .Site.Language.Translate ... which is a little longer ... but cleaner.

// Translate returns a translated string for id.
func (s *Site) Translate(id string, args ...any) (string, error) {
ctx := context.Background()
return lang.New(s.Deps, langs.GetTranslator(s.Language())).Translate(ctx, id, args...)
Copy link
Member

@bep bep Apr 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The above is propbably simpler to reason about when moving the method, but we do not want to do lang.New for each translated string.

return site, nil
}
}
return nil, fmt.Errorf("unable to find site with lang %q", lang)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it matches most of the similar situations to return nil in this case. That way you can try to translate arbitrary languages by wrapping it in with.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review. It looks like you have recently implemented something similar to site.GetSites while working on the pages from data stuff, so I was going to hold off until that was finalized. It looked like it I might get hugo.GetSite for free. Let me know if that's not accurate.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I haven't touched anything similar in that branch. What I was concerned about in that setting was to make sure that when you do i18n and similar, you would get the current site/language's translation, the same as you do today in single.html and friends.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That said #12440 is shaping up nicely. I need to take it the final mile (resources support is missing, for one), but I have tested it out on a "real project" and it's very nice to work with (especially after I figured out how to map the gotmpl extension to html in VSCode). Having it in the templates gives the unexpected benefit of partial rebuilds "for free", so edits to assets included via resources.Get etc. just works. I also think this finally can make Hugo's OpenAPI integration generally useful.

- Site.Language.Translate returns a translated string in the language of the
  given site.

  {{ range .AllTranslations }}
    {{ .Site.Language.Translate "cat" 6 }}
  {{ end }}

- Sites.GetSite returns the site corresponding to the given language.
  This method simplifies cross-site translations.

  {{ with .Sites.GetSite "en" }}
    {{ .Language.Translate "dog" 7 }}
  {{ end }}

Closes gohugoio#7844
@jmooring
Copy link
Member Author

jmooring commented May 2, 2024

I am unable to figure this out in a reasonable time frame, so I am going to punt for now.

@jmooring jmooring closed this May 2, 2024
@jmooring jmooring deleted the feat/site-translate-7844 branch May 3, 2024 13:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add .Site.Language.I18n method (or .Translate?)
2 participants