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

Add .Site.Language.I18n method (or .Translate?) #7844

Open
nekr0z opened this issue Oct 16, 2020 · 7 comments
Open

Add .Site.Language.I18n method (or .Translate?) #7844

nekr0z opened this issue Oct 16, 2020 · 7 comments

Comments

@nekr0z
Copy link
Contributor

nekr0z commented Oct 16, 2020

Following the support question in this forum thread: it would be nice to have site.Translation method to leverage i18n information from the .Translations range.

@bep bep added this to the v0.77 milestone Oct 16, 2020
@bep
Copy link
Member

bep commented Oct 16, 2020

I'm not totally sure about the method name. Since it should behave the same as the current i18n func, maybe we should just name it site.I18n so people don't get other expectations.

@martignoni
Copy link
Contributor

martignoni commented Oct 17, 2020

Without a capitalized "I", I'd suggest: site.i18n.

@nekr0z
Copy link
Contributor Author

nekr0z commented Oct 20, 2020

.site.i18n with an alias of .site.T — for full consistency, you know...

@bep
Copy link
Member

bep commented Oct 21, 2020

Without a capitalized "I", I'd suggest: site.i18n.

  • As this would be a method on the Site interface, the Go spec forces first character to be uppercase for public/exported methods.
  • site.i18n is technically a function and could be all lowercase (I think), but that is just an alias for .Site.I18n, which is the useful method in this setting, as in: .Sites.First.I18n "foo"

And also, I don't want to fill the Site interface up with aliases.

@bep bep changed the title Add site.Translate method Add .Site.I18n method Oct 21, 2020
@bep bep added Enhancement and removed Proposal labels Oct 21, 2020
@bep bep modified the milestones: v0.77, v0.78 Oct 30, 2020
@bep bep modified the milestones: v0.78, v0.83 Apr 23, 2021
@bep bep modified the milestones: v0.83, v0.84 May 3, 2021
@bep bep modified the milestones: v0.84, v0.85 Jun 18, 2021
@bep bep modified the milestones: v0.85, v0.86 Jul 5, 2021
@bep bep modified the milestones: v0.86, v0.87, v0.88 Jul 26, 2021
@bep bep modified the milestones: v0.88, v0.89 Sep 2, 2021
@bep bep modified the milestones: v0.89, v0.90 Nov 2, 2021
@bep bep modified the milestones: v0.90, v0.91.0 Dec 13, 2021
@bep bep removed this from the v0.91.0 milestone Dec 22, 2021
@bep bep modified the milestones: v0.119.0, v0.120.0 Oct 4, 2023
@bep bep modified the milestones: v0.120.0, v0.121.0 Oct 31, 2023
@bep bep modified the milestones: v0.121.0, v0.122.0 Dec 6, 2023
@gitauris
Copy link

gitauris commented Dec 6, 2023

Was this ever added? I can't seem to find it in the current documentation.
Or, if not, is there any other way to retrieve a translation for a string in a different language than the current one?
Thanks, Michael

@jmooring
Copy link
Member

jmooring commented Dec 6, 2023

No, not added... this issue is still open.

If you are using a simple (no pluralization rules) translation table, you can mount the i18n directory to the data directory.

i18n/en.toml

privacy = 'privacy'
security = 'security'

i18n/pl.toml

privacy = 'prywatność'
security = 'bezpieczeństwo'

site config

[[module.mounts]]
source = 'data'
target = 'data'

[[module.mounts]]
source = 'i18n'
target = 'data'

Then in your template:

{{ index .Site.Data "pl" "privacy" }} --> always returns polish translation

{{ T "privacy" }} --> translated to the current language, falling back to the default language

I don't know of any (simple1) way of handling this if your translation tables include pluralization rules, for example:

i18n/pl.toml

[day]
few = 'miesiące'
many = 'miesięcy'
one = 'miesiąc'
other = 'miesiąca'

Footnotes

  1. There's a convoluted way to do this with a partial, a content view, and a page scratch, but I think the complexity far outweighs the functional benefit.

@bep bep modified the milestones: v0.122.0, v0.123.0, v0.124.0 Jan 27, 2024
@bep bep modified the milestones: v0.124.0, v0.125.0 Mar 4, 2024
@bep bep changed the title Add .Site.I18n method Add .Site.Language.I18n method Mar 13, 2024
@bep bep changed the title Add .Site.Language.I18n method Add .Site.Language.I18n method (or .Translate?) Mar 13, 2024
@hekuli
Copy link

hekuli commented Apr 16, 2024

@bep bep modified the milestones: v0.125.0, v0.126.0 Apr 23, 2024
jmooring added a commit to jmooring/hugo that referenced this issue Apr 28, 2024
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 gohugoio#7844
jmooring added a commit to jmooring/hugo that referenced this issue May 2, 2024
- 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
@bep bep modified the milestones: v0.126.0, v0.127.0 May 15, 2024
@bep bep modified the milestones: v0.127.0, v0.128.0 Jun 8, 2024
@bep bep modified the milestones: v0.128.0, v0.129.0 Jun 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants