HUGO

Localization of date and time

At some point Hugo will probably be able to localize date and time. In the interim, you can use a combination of configuration, translation, and a partial. Here’s an example for a site with english and spanish content.

config.toml
[languages.en]
  weight = 1
  [languages.en.params.formats.date]
    long = "Monday, January 2, 2006"
    short = "1/2/06"
  [languages.en.params.formats.time]
    long = "3:04:05 PM"
    short = "3:04 PM"
  [languages.en.params.formats.datetime]
    long = "Monday, January 2, 2006 3:04:05 PM"
    short = "1/2/06 3:04 PM"

[languages.es]
  weight = 2
  [languages.es.params.formats.date]
    long = "el Monday 2 de January de 2006"
    short = "2/1/06"
  [languages.es.params.formats.time]
    long = "3:04:05 PM"
    short = "3:04 PM"
  [languages.es.params.formats.datetime]
    long = "el Monday 2 de January de 2006 3:04:05 PM"
    short = "2/1/06 3:04 PM"

i18n/en.toml
[date.month.long]
1 = "January"
2 = "February"
3 = "March"
4 = "April"
5 = "May"
6 = "June"
7 = "July"
8 = "August"
9 = "September"
10 = "October"
11 = "November"
12 = "December"

[date.month.short]
1 = "Jan"
2 = "Feb"
3 = "Mar"
4 = "Apr"
5 = "May"
6 = "Jun"
7 = "Jul"
8 = "Aug"
9 = "Sep"
10 = "Oct"
11 = "Nov"
12 = "Dec"

[date.weekday.long]
0 = "Sunday"
1 = "Monday"
2 = "Tuesday"
3 = "Wednesday"
4 = "Thursday"
5 = "Friday"
6 = "Saturday"

[date.weekday.short]
0 = "Sun"
1 = "Mon"
2 = "Tue"
3 = "Wed"
4 = "Thu"
5 = "Fri"
6 = "Sat"

i18n/es.toml
[date.month.long]
1 = "enero"
2 = "febrero"
3 = "marzo"
4 = "abril"
5 = "mayo"
6 = "junio"
7 = "julio"
8 = "agosto"
9 = "septiembre"
10 = "octubre"
11 = "noviembre"
12 = "diciembre"

[date.month.short]
1 = "enero"
2 = "feb"
3 = "marzo"
4 = "abr"
5 = "mayo"
6 = "jun"
7 = "jul"
8 = "agosto"
9 = "set"
10 = "oct"
11 = "nov"
12 = "dic"

[date.weekday.long]
0 = "domingo"
1 = "lunes"
2 = "martes"
3 = "miércoles"
4 = "jueves"
5 = "viernes"
6 = "sábado"

[date.weekday.short]
0 = "D"
1 = "L"
2 = "M"
3 = "X"
4 = "J"
5 = "V"
6 = "S"

layouts/partials/functions/localize-date.html
{{ $ns := "functions" }}
{{ $partial := "localize-date.html" }}

{{ $pre := printf "[partial: %s/%s]" $ns $partial }}

{{ $msg1 := printf "%s Invalid date." $pre }}
{{ $msg2 := printf "%s Missing date." $pre }}
{{ $msg3 := printf "%s Invalid format." $pre }}
{{ $msg4 := printf "%s Missing format." $pre }}

{{ $date := "" }}
{{ with .date }}
  {{ if eq (printf "%T" .) "time.Time" }}
    {{ $date = . }}
  {{ else }}
    {{ errorf $msg1 }}
  {{ end }}
{{ else }}
  {{ errorf $msg2 }}
{{ end }}

{{ $format := "" }}
{{ with .format }}
  {{ if eq (printf "%T" .) "string" }}
    {{ $format = . }}
  {{ else }}
    {{ errorf $msg3 }}
  {{ end }}
{{ else }}
  {{ errorf $msg4 }}
{{ end }}

{{ $monthFormat := "" }}
{{ if in $format "January" }}
  {{ $monthFormat = "long" }}
{{ else if in $format "Jan" }}
  {{ $monthFormat = "short" }}
{{ end }}

{{ $weekdayFormat := "" }}
{{ if in $format "Monday" }}
  {{ $weekdayFormat = "long" }}
{{ else if in $format "Mon" }}
  {{ $weekdayFormat = "short" }}
{{ end }}

{{ $dateFormatted := $date.Format $format }}

{{ with $monthFormat }}
  {{ $monthT := i18n (printf "date.month.%s.%d" $monthFormat $date.Month) }}
  {{ if eq . "long" }}
    {{ $dateFormatted = replaceRE `January|February|March|April|May|June|July|August|September|October|November|December` $monthT $dateFormatted }}
  {{ else }}
    {{ $dateFormatted = replaceRE `Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec` $monthT $dateFormatted }}
  {{ end }}
{{ end }}

{{ with $weekdayFormat }}
  {{ $weekdayT := i18n (printf "date.weekday.%s.%d" $weekdayFormat $date.Weekday) }}
  {{ if eq . "long" }}
    {{ $dateFormatted = replaceRE `Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday` $weekdayT $dateFormatted }}
  {{ else }}
    {{ $dateFormatted = replaceRE `Mon|Tue|Wed|Thu|Fri|Sat|Sun` $weekdayT $dateFormatted }}
  {{ end }}
{{ end }}

{{ return $dateFormatted }}

Example usage within a template:

{{ partial "functions/localize-date" (dict "date" .Date "format" site.Params.formats.datetime.long) }}

Given date = 2021-02-26T05:22:00-07:00 in frontmatter, this will be rendered as:

Language Value
en Friday, February 26, 2021 5:22:00 AM
es el viernes 26 de febrero de 2021 5:22:00 AM

Try it:
git clone --single-branch -b hugo-forum-topic-33995 https://github.com/jmooring/hugo-testing hugo-forum-topic-33995
cd hugo-forum-topic-33995
hugo server

Then visit:

http://localhost:1313/en/post/test/
http://localhost:1313/es/post/test/
6 Likes