We talked about rendering dates and the last modification date in the last post. Now, we’ll look into how can we convert month names into another language? I’ve had a similar experience last year with jekyll. The idea is the same, we need to map each month to their version of the preferred language and we need to store this information somewhere. We have data files for that.
1. Create a data file. /data/aylar.yaml
1: Ocak
2: Şubat
3: Mart
4: Nisan
5: Mayıs
6: Haziran
7: Temmuz
8: Ağustos
9: Eylül
10: Ekim
11: Kasım
12: Aralık
Now, you can access that data by calling {{ .Site.Data.aylar }}
.
2. Format date using your data file.
{{ .Date.Format "02" }} {{ index .Site.Data.aylar (.Date.Format "1")}}, {{ .Date.Format "2006"}}
Remember, 1 means the month in date format. 2 is the day, 2006 is the year. 02-01-2006
is the reference time in Go Language. So, the output for this article will be: 22 Ağustos, 2020
since we just mapped 08: Ağustos
.
That’s it! You can do something similar for weekdays.
If you are curious, {{ .Site.Data.foo }}
returns a map and index function returns the resulting value (“Ağustos”) for a given key (“8”). If you know about programming languages, a map is basically a dictionary and index function helps you get the corresponding value for a key.
So, {{ .Site.Data.aylar }}
will return map[1:Ocak 10:Ekim 11:Kasım 12:Aralık 2:Şubat 3:Mart 4:Nisan 5:Mayıs 6:Haziran 7:Temmuz 8:Ağustos 9:Eylül]
and {{ index .Site.Data.aylar "8" }}
return Ağustos
.