tifenak
September 20, 2025, 1:41pm
1
Is there a way to parse months in a data file and translate them automatically? Or the only way is to use i18n?
[
{
"Year": "2025",
"Month": "April",
"Amount": "6,164"
},
{
"Year": "2025",
"Month": "March",
"Amount": "4,164"
},
{
"Year": "2025",
"Month": "February",
"Amount": "3,164"
},
{
"Year": "2025",
"Month": "January",
"Amount": "2,164"
}
]
Create a parsable date string using the first three letters of the month name, then use the time.Format
function to localize:
{{ range site.Data.foo }}
{{ $t := time.AsTime (fmt.Printf "01 %s %s" (strings.Substr .Month 0 3 | strings.ToLower) .Year) }}
{{ $t | time.Format "January 2006" }}
{{ end }}
Our documentation lists the most common parsable date strings. Here is the complete list:
https://github.com/spf13/cast/blob/fc73346bfc4e6597bc520fb6eea04360299e77d2/internal/time.go#L31-L57
1 Like
tifenak
September 20, 2025, 3:08pm
3
Is {{ $t | time.Format "January" }}
valid? I just need the month. It works in my test though.
tifenak:
valid?
Yes. Use one or more of these components in your layout string:
https://gohugo.io/functions/time/format/#layout-string
tifenak
September 20, 2025, 3:14pm
5
Thank you. Your solution saved me from duplicating multiple data files. I will try to consolidate those I had created in the data folder into a single file in page bundles.
1 Like
system
Closed
September 22, 2025, 3:15pm
6
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.