Dates : only in english?

Is there a way to display the dates in another language ? Until now, I haven’t found a way to display a date in French, like this : “14 juin 2015”.

Have I missed something ?

1 Like

I’m not a golang programmer, but I think I read that that is one of the things on the golang roadmap.

It might be possible somehow, with the data function.

Given /data/translations/ja-JP.yaml, where I have translation strings like:

formemail: メール
formfirstname: 名
formlastname: 氏

… and given locale set in my config.toml as ja-JP, I can call strings from there like this:

{{ ( index $.Site.Data.translations $.Site.Params.locale ).formemail }}

If that’s the case, I may be able to make a more complex structure in the translation yaml file, to, say, return the ancient Japanese month names (and you would want the French ones).

formemail: メール
formfirstname: 名
formlastname: 氏
month:
  - 1: 睦月
  - 2: 如月
  - 3: 弥生
  - 4: etc ...

I tried it and cannot figure out how to structure this correctly or call it. These don’t work:

<p>{{ ( index $.Site.Data.translations $.Site.Params.locale ).month.1 }}</p>
<p>{{ ( index $.Site.Data.translations $.Site.Params.locale ).month.$1 }}</p>

My idea is, if you can get the number of a month, you can feed that to the data table and get back the name you want.

Perhaps @bep or @spf13 could kindly comment on how we might structure the translation file for this usage, and access the array? We’re in your debt.

2 Likes

Also, to get month or year etc out of a .Date, you can use the technique shown here:

Basically {{ .Date.Format "01" }} is what I think you’d use to get the month number.

Ok, cool, I think I got it. Using this yaml:

month:
  - 1: 睦月
  - 2: 如月
  - 3: 弥生
  - 4: etc ...

… gives a sort of map of maps. So, simplifying to:

month:
  1: 睦月
  2: 如月
  3: 弥生
  4: etc ...

… when you access like this:

<p>{{ ( index $.Site.Data.translations $.Site.Params.locale ).month }}</p>

… it gives a simple map, like this:

map[3:弥生 1:睦月 2:如月]

Therefore, assigning to a variable and indexing that should work. So, with the simplified yaml, do:

<p>
{{ $mymonths :=  ( index $.Site.Data.translations $.Site.Params.locale ).month }}
{{ index $mymonths 1 }}
</p>

That returns 睦月. The next challenge is to get the number to feed the second line’s index statement, with the number of the month in the year.

OK, I see the idea. It’s not ideal, but waiting for an official solution, it should work.

For now, I’m using this code to display dates. It’s better than plain english words, but I’d rather have french words instead. I don’t know if you could output the month number only, though.

   Article publié le {{ .Date.Format "02/01/2006" }} (dernière modification le {{ .Lastmod.Format "02/01/2006" }})

You can. Just see the code in the github commit I posted above. It separates out day, month, year. Just depends on the format you use, “01” or “02” or “2006”. You can get English US months by doing “Jan”.

Oh right, I read it too quickly.

I’ll try to see if I can make it work.

Please feed back what you discover. :slight_smile:

Not sure if you solved the last part, but this doc should give you a hint:

http://golang.org/pkg/time/#Time.Month

The month is Month type which is an integer starting on 1. I believe this should work:

Using the page’s date:

{{ index $mymonths .Date.Month }}

Or current month:

{{ index $mymonths .Now.Month }}

There may be some type issues here, not sure. If so, try something ala

{{ index $mymonths (add .Now.Month 0) }}

@bep thanks! You’re always a lifesaver!

Thanks also, I was heading to a much harder solution !

I can’t manage to make it work, though, I guess I’m doing something wrong here. I chose to store the data in a file named mois.toml inside the data folder. In it, all months are translated like this :

[mois]
    01 = "janvier"
    02 = "février
    03 = etc.

Using this line : {{ $mymonths := index $.Site.Data.mois.mois }}, I think it works, since if I display the variable, I have this output (I don’t know if the ordering is normal or not…) :

map[01:janvier 02:février 04:avril 09:septembre 10:octobre 03:mars 05:mai 06:juin 07:juillet 08:août 11:novembre 12:décembre]

But in the end, I can’t map the two informations. I have this error when building the website (using hugo server) :

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

Thanks for your help ! :slight_smile:

The above has strings as key. I would start by removing the leading zero.

The issue here is an issue of incompatible types. Your map key is the string “01” but you are looking for the integer 1. You need to convert the integer to a string as I believe the map will always have a string key when coming from TOML. You could try removing the leader 0 as well.

Best,
Steve

As to converting to a string:

{{ index $mymonths (printf "%02d" .Now.Month) }}

The above should let you keep your leading zero.

Or without padding:

{{ index $mymonths (printf "%d" .Now.Month) }}
1 Like

OK, some progress : no more error when running Hugo !

But it does not work, nothing appears on the site when using :

<p>Date : {{ index $mymonths (printf "%2d" .Date.Month) }}</p>

If TOML is a problem, maybe I can convert my file to YAML ? I don’t really care, as long as it works…

One more thing : @bep, you’re using .Now.Month, but I can use .Date.Month, right ?

Thanks everyone ! :slight_smile:

Yes.

TOML should work fine.

Try to print out the

{{ (printf “%2d” .Date.Month) }}

And check that it prints what you expect (1, 2 etc).

It outputs the right month, but without the 0. Like “6” for June. I changed the TOML file, so no problem I guess, and still nothing shows when using the full thing.

Here’s what I have :

{{ $mymonths := index $.Site.Data.mois.mois }}
<p>Date : {{ index $mymonths (printf "%2d" .Date.Month) }}</p>

OK then I guess the keys in the TOML map now are Integers. Try without the printf.
{{ index $mymonths .Date.Month }}

Nope, sorry, I’m back to errors :

ERROR: 2015/06/15 template: partials/meta.html:65: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

Can I mix TOML and YAML in the same project ? If so, I can have this file in YAML if it fixes everything.