Current date inside an internationalised string

I have a footer string in i18n/en.toml that looks like this:

    other = "Copyright © 2015-{{ now.Format `2006` }}"

Building it gives this warning (and doesn’t create the string):

WARN 2021/03/26 13:51:17 Failed to get translated string for language "en" and ID "footer_text": template: :1: function "now" not defined

The warning is pretty clear. I know I can access variables from within the translated string, so I’d expect to be able to access Hugo’s set of functions too?

How should I access the current timestamp from inside an internationalised string?

I just read somewhere in the docs that translations of dates is not something Hugo can do yet, due to Golang issues. Not 100% sure if it was about time zones or languages though… it’s fickle.

Having said that: functions are NOT available in translation strings. Variables are.

Call the string like this:

{{ i18n "footer_text" now }}

and in your i18n file:

    other = "Copyright © 2015-{{ .Format `2006` }}"

should do the trick.

1 Like

Yup, that’ll do it nicely (or for anyone who needs more than just now use dict to pass both now and . in). Thanks.