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

Functions don't work in translated strings #10083

Closed
isti03 opened this issue Jul 11, 2022 · 5 comments
Closed

Functions don't work in translated strings #10083

isti03 opened this issue Jul 11, 2022 · 5 comments

Comments

@isti03
Copy link

isti03 commented Jul 11, 2022

What version of Hugo are you using (hugo version)?

$ hugo version
hugo v0.101.0+extended linux/amd64 BuildDate=unknown

Does this issue reproduce with the latest release?

Yes

Issue description

I have the following translation in i18n/en.yaml:

lastmod:
  other: 'Last modified: {{ .Date | time.Format ":date_full" }}'

I then use it in a template this way:

{{ i18n "lastmod" . }}

I get the following error when building the site:

WARN 2022/07/11 10:16:36 Failed to get translated string for language "en" and ID "lastmod": template: :1: function "time" not defined
@bep bep added this to the v0.102.0 milestone Jul 11, 2022
@bep bep modified the milestones: v0.102.0, v0.103.0 Aug 28, 2022
@bep bep modified the milestones: v0.103.0, v0.104.0 Sep 15, 2022
@bep bep modified the milestones: v0.104.0, v0.105.0 Sep 23, 2022
@bep bep modified the milestones: v0.105.0, v0.106.0 Oct 26, 2022
@bep bep modified the milestones: v0.106.0, v0.107.0 Nov 18, 2022
@bep bep modified the milestones: v0.107.0, v0.108.0 Dec 3, 2022
@bep bep modified the milestones: v0.108.0, v0.109.0 Dec 14, 2022
@bep bep modified the milestones: v0.109.0, v0.111.0, v0.110.0 Jan 26, 2023
@bep bep modified the milestones: v0.111.0, v0.112.0 Feb 15, 2023
@bep bep modified the milestones: v0.112.0, v0.113.0 Apr 15, 2023
@bep bep modified the milestones: v0.113.0, v0.115.0 Jun 13, 2023
@bep bep modified the milestones: v0.115.0, v0.116.0 Jun 30, 2023
@bep bep modified the milestones: v0.116.0, v0.117.0 Aug 1, 2023
@bep bep modified the milestones: v0.117.0, v0.118.0 Aug 30, 2023
@bep bep modified the milestones: v0.118.0, v0.119.0 Sep 15, 2023
@bep bep modified the milestones: v0.119.0, v0.120.0 Oct 5, 2023
@jmooring
Copy link
Member

The texttemplate parser validates function existence, but that is currently limited to built-in template functions. For example, this works fine in an i18n file.

foo = '{{ or . "undefined" }}'

Not sure why Hugo's functions aren't in the function map at this point.

@bep
Copy link
Member

bep commented Oct 26, 2023

Not sure why Hugo's functions aren't in the function map at this point.

The go-i18n library has its own template handling and has no knowledge of Hugo's template functions etc.

This is unfortunate, and is probably fixable ... but I would strongly recommend keeping the i18n files as simple as possible.

The example in the intro could easily be rewritten as

lastmod:
  other: 'Last modified'

Which In my head is a better setup (separation of concerns and all that).

Then you can do templating/formatting in the layout template, e.g.:

{{ i18n "lastmod" }}: {{ .Date | time.Format ":date_full" }}

@jmooring
Copy link
Member

keeping the i18n files as simple as possible

Yeah, the shorthand is somewhat appealing, but that could quickly get out of control:

# i18n/en.toml

foo = '{{ partial (path.Join .Language.Lang "my-partial.html") }}'

@bep
Copy link
Member

bep commented Oct 27, 2023

but I would strongly recommend keeping the i18n files as simple as possible.

One weekend project I was thinking of doing, to get a better grasp of what's possible with the recent outflow of local large language models (e.g. Llama 2) to create some tooling to translate i18n/*.toml, and that task would be much harder if those files was filled with template logic (heck, even handing those files over to a human translator would make him/her hesitate).

Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants