Allow using functions in i18n messages

Backgrounds

I want to separate collections with delimit function, since the seperators are different between different languages, so I’m trying to handle it in i18n messages, but seems not able to use functions there.

// en.toml
[parameter_options]
other = "Available options: {{ delimit . `, ` ` and ` }}."

// zh-hans.toml
[parameter_options]
other = "可选项:{{ delimit . `、` ` 和 ` }}。"
Failed to get translated string for language "en" and ID "parameter_options": template: :1: function "delimit" not defined

I may be wrong, but you are not putting functions in i18n. The thing that you want to achieve shall be done at theme/layout level.

This is why:

function "delimit" not defined

Here is example on serving different Article publish date for different languages.

<div class="date">
   {{ T "Published" }}
    {{ if eq .Lang "en"}}
        <time datetime='{{ .Date.Format "2006-01-02T15:04:05.000" }}'>{{ .Date.Format "2 January 2006, 15:04 MST" }}</time>
    {{ else }}
        <time datetime='{{ .Date.Format "2006-01-02T15:04:05.000" }}'>{{ .Date.Format "02.01.2006, 15:04 MST" }}</time>
    {{ end }}
</div>

In simillar way you may incorporate what you want to achievie on your end.

1 Like

Yes, I can do it on templates, I think it would be better if i18n messages support functions:

  1. No conditional directives in templates.
  2. Put language’s scoped stuff in same place.

Something like that:

// en.toml
[options_intro]
other = "Available options: {{.}}"

[options_delimiter]
other = " and "

// zh-hans.toml
[options_intro]
other = "可选项: {{.}}"

[options_delimiter]
other = " 和 "

// usage in template
{{ T "options_intro" (delimit . ", " (T "options_delimiter")) }}
2 Likes

That’s a better workround, I’ll probably go with this approach if this feature request won’t be accepted.

This feature request won’t be accepted.

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

https://github.com/gohugoio/hugo/issues/10083#issuecomment-1781578444

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.