-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
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
Conversation
hugolib/site_new.go
Outdated
|
||
// Translate returns a translated string for id. | ||
func (s *Site) Translate(id string, args ...any) (string, error) { | ||
ctx := context.Background() |
There was a problem hiding this comment.
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.
hugolib/site_new.go
Outdated
// 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...) |
There was a problem hiding this comment.
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.
resources/page/site.go
Outdated
return site, nil | ||
} | ||
} | ||
return nil, fmt.Errorf("unable to find site with lang %q", lang) |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
f7ce5cf
to
e178848
Compare
I am unable to figure this out in a reasonable time frame, so I am going to punt for now. |
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