Month in french

Hi,
I’m new to hugo, and still trying to understand how to set up my website. I’m investigating how to change name of months from english to french.

  • I have a i18n directory in the theme directory I use; if I copy it in root directory, it doesn’t work. Neither in layout directory. Where the modified i18n directory should be?
  • Is there a exhaustive list somewhere of generic id for translations in i18n files?
  • How to get months names translated to french?

Many thanks in advance.
Guillaume

Quelque chose comme ceci:

{{ $month_names := slice "janvier" "février" "mars" "avril" "mai" "juin" "juillet" "août" "septembre" "octobre" "novembre" "décembre" }}
{{ $month := sub .Date.Month 1 }}
{{ index $month_names $month }}

Je mets ça dans quel fichier ?

The time.Format function localizes time.Time values. You don’t need translation tables.

1 Like

Yes! Something like:

<time datetime="{{.Date.Format " 2006-01-02"}}" class="entry-date dateline">{{ .Date | time.Format ":date_long" }}</time>

But I prefer my solution for unbreakable spaces and ordinal (1er in French for 1st).

The Format method on a time.Time value does not localize the value. Use the time.Format function instead.

https://gohugo.io/methods/time/format/

To localize the return value, use the time.Format function instead.

EDIT: Disregard. I see that you are using the function in the display value.

Also note that you can use any valid layout string, not just the predefined tokens. For example:

{{ .Date | time.Format "January" }} --> novembre
2 Likes

OK, I’m lost. Is there a doc and/or a forum for dummies?

1 Like

You might want to start by sharing what you have done.

See Requesting Help.

Let us see your code

Include a link to the source code repository of your project, because we really need the context of seeing your templates and partials to be able to help you. It is trivial to do a quick git clone on your repo, then run hugo server in your project, to help you out. On the other hand, recreating your code from screenshots, or sort of guessing at it, is not.

If you can’t share your repository for whatever reason, consider creating a dummy repo that you can share, which reproduces the problem you’re experiencing.

Wow. Thank you so much, I didn’t know that was possible,

But now I have a question of detail. What’s the difference between:

{{ .Date | time.Format "2" }} {{ .Date | time.Format "January" }} {{ .Date | time.Format "2006"}}.

and:

{{ .Date.Day }} {{ .Date | time.Format "January" }} {{ .Date.Year }}

.Date | time.Format "2" and .Date.Day produce the same output.
Is one use better than the other?

But .Date | time.Format "January" is not equal to .Date.Month (no localization).

Data type.

{{ now.Day }} --> 27 (int)
{{ time.Format "2" now }} --> 27 (string)

{{ now.Year }} --> 2023 (int)
{{ time.Format "2006" now }} --> 2023 (string)

But in your example above, just do:

{{ .Date | time.Format "2 January 2006" }}
OR
{{ .Date | time.Format ":date_long" }}

Both are localized.

2 Likes