Dates : only in english?

Yes you can mix and match. I will stop guessing now, as it obviously doesn´t solve anything …

I think I’m really close, but…

Here’s my new mois.yaml :

1: "janvier"
2: "février"

Etc, until december.

Here’s what I have in the output side :

{{ $mymonths := index $.Site.Data.mois }}
<p>Date : {{ index $mymonths (add .Date.Month 0) }}</p>

I checked, both $mymonths and (add .Date.Month 0) are OK when displayed separately.

But when I run Hugo, I have this kind of error :

ERROR: 2015/06/15 template: partials/meta.html:69:13: executing "partials/meta.html" at <index $mymonths (add...>: error calling index: int64 is not index type for map[string]interface {} in partials/meta.html

And it does not work.

I tried with strings instead of integers, and still nothing, but a different error :

ERROR: 2015/06/15 template: partials/meta.html:71:13: executing "partials/meta.html" at <index $mymonths .Dat...>: error calling index: time.Month is not index type for map[string]interface {} in partials/meta.html

Now, I’m not sure what to do…

I will test it … but not tonight.

In my head, this should work given that error message:

{{ index $mymonths (printf “%d” .Date.Month) }}

1 Like

Yes ! This time it works ! :slight_smile:

Article publié le 5 mai 2013 (dernière modification le 15 juin 2015)

As soon as I have time, I’ll write a sort of tutorial to sum everything up.

Thank you for your help ! :slight_smile:

Can you add this technique to the docs so others can benefit from it?

Best,
Steve

No problem, but I may need some help or doc to learn how to do so.

@spf13 if you guys would like to merge my “multilingual” tutorial PR to the docs, a good place for this piece to go might be inside that, since the topic fits.

@nicolinuxfr in the end, was it the {{ index $mymonths (printf "%d" .Date.Month) }} that @bep suggested that worked? I’d like to try it on my end today.

Haven’t tested it, but you can probably get slightly cleaner syntax with an array instead of a map…

# months_data.toml or months_data.yml... inline array syntax should be the same for both
months = [ "Jan", "Feb", "Mar", "Apr", "..." ]

and

{{ index $.Site.Data.months_data.months .Date.Month }}
1 Like

Yep ! You can see the details here : https://github.com/nicolinuxfr/voiretmanger-hugo/commit/5ecc162a0e89d803997fff5e9ef0a2507c0ff6d0

I think it’s a great idea to make a more broader tutorial about localization. So if you need help for the date part, let me know.

@lotrfan, thanks, that’s interesting. I’ll try that.

@nicolinuxfr thanks. My PR is here:

I think adding this bit about custom month names into that, and improving it gradually would be a good idea. It’s not merged yet, though.

If it worked, maybe we could put this array in config file, rather than creating a new file inside the data folder ?

Slices are 0-indexed, months start at 1. So you have to do a (sub .Date.Month 1) to get it in line.

1 Like

@nicolinuxfr it’s up. The technique I use is to make the locale name be the file name, then leverage that fact, but yes, since each language has a config file, it might work. Though I’m not sure of functional differences between how config.toml works, and /data/path/to/myfile.yaml works.

Good point!

One other option would be to insert a dummy element at the beginning of the array; e.g.,

months = [ "unused", "Jan", "Feb", ... ]

@RickCogley I tried at first to use the config.toml, but I’m not sure you can have any data there… Anyway, I tried and it did not work.

@nicolinuxfr added the concept of getting month names from a data file to the tutorial.

Used your French data file as the example, since my original Japanese example would probably be too esoteric.

Sorry I didn’t even answer the topic. But thanks for the tutorial ! In fact, I had a chance to use it again very recently for a new project and it worked really well, so I think explanations were good. :slightly_smiling:

Hi,
Thanks for the suggestion. Unfortunately I’m writing a Theme and I don’t want (or don’t know how) to mess with the Data subdir of Hugo.
I’ve come up with a simple solution, using the i18n files of the template:
Since {{.Date.Month}} gives June for example, we can write in the template:

<p>{{i18n "created_on'}} {{.Date.Day}} {{i18n .Date.Month}} {{.Date.Year}}</p>

provided in the i18n subfolder of the theme dir, for each language in your TOML file you have:

[January]
other = "Gennaio"
[February]
other = "Febbraio"
....
[December]
other = "Dicembre"
4 Likes