hugo, how to set date language
Asked Answered
C

1

6

With Hugo, I'm creating a blog in french. Currently all my date are displayed in english (february) but I want them to be displayed in french (février). How to set the language ?

My config.toml looks like this:

baseURL = 'http://example.org/'
languageCode = 'fr-FR'
defaultContentLanguage = "fr"
defaultContentLang = "fr"
title = 'TITLE'
theme = "THEME"

I try to set languageCode, defaultContentLanguage and defaultContentLang to fr but with no success.

I don't need multi language support, I just only need french.

Casket answered 7/3, 2022 at 20:42 Comment(2)
How are you printing the date? You probably need to use the hugo time formatter, not the default Go .Date.Format method. {{ time.Format "desired-format" "your-date" }}. Also "dateFormat" is an alias of time.Format. gohugo.io/functions/dateformat . Alias documented here: github.com/gohugoio/hugo/blob/master/docs/content/en/functions/…Anthraquinone
@BrianWagner thanks, that's working. It translate the date in my language. Could you add this as an official answer so I can approve it for the next people getting stuck ?Casket
A
9

To use localization, you need to use the Hugo function time.Format (which has an alias dateFormat). It takes two parameters:

  • desired format
  • time.Time object, or timestamp

Example:

{{ time.Format "Jan. 2, 2006" .Date }}

or

{{ dateFormat "Jan. 2, 2006" .Date }}

Docs: https://gohugo.io/functions/dateformat/

The .Format method (e.g. {{.Date.Format "Jan. 2, 2006}}) will not apply the desired localization.

Anthraquinone answered 8/3, 2022 at 21:58 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.