Using i18n for date formatting

I found some tips to use data files for translating month indexes to month names. That works.

However, I think it makes more sense to use the i18n function for this. So what I did was to put this in my language toml file:

[long_month_1 ]
other = "januari"
[long_month_2 ]
other = "februari"
[long_month_3 ]
other = "mars"
[long_month_4 ]
other = "april"
[long_month_5 ]
other = "maj"
[long_month_6 ]
other = "juni"
[long_month_7 ]
other = "juli"
[long_month_8 ]
other = "augusti"
[long_month_9 ]
other = "september"
[long_month_10]
other = "oktober"
[long_month_11]
other = "november"
[long_month_12]
other = "december"

(That’s Swedish month names.)

Then I use it like so from my template:

{{ i18n (printf "long_month_%d" .PublishDate.Month) }}

Cheers! Thanks to everyone who make Hugo. It is fantastic.

8 Likes

Is this tip really necessary when we have this?

We didn’t have the localization capability when that topic was created.

Understood. I came across this post when searching for date localization.