1

I'm trying to translate my Jekyll dates to French.

I followed the advice on this old reply on another StackOverflow question.

The idea is to create a custom plugin that:

  1. translates the names of months to French, and
  2. creates an html5-friendly date format for the <time> element.

Here's the code, copied from the other question and adapted from Italian: (french_dates.rb in my _plugins folder)

module Jekyll
module FrenchDates
    MONTHS = {"01" => "janvier", "02" => "février", "03" => "mars",
            "04" => "avril", "05" => "mai", "06" => "juin",
            "07" => "juillet", "08" => "août", "09" => "septembre",
            "10" => "octobre", "11" => "novembre", "12" => "décembre"}

    # http://man7.org/linux/man-pages/man3/strftime.3.html
    def frenchDate(date)
        day = time(date).strftime("%e") # leading zero is replaced by a space
        month = time(date).strftime("%m")
        year = time(date).strftime("%Y")
        day+' '+MONTHS[month]+' '+year
    end

    def html5date(date)
        day = time(date).strftime("%d")
        month = time(date).strftime("%m")
        year = time(date).strftime("%Y")
        year+'-'+month+'-'+day
    end
end
end

Liquid::Template.register_filter(Jekyll::FrenchDates)

And in Jekyll, call the plugin like this: (in my case in _layouts/blog.html)

<time datetime="{{ page.date | html5date }}">{{ page.date | frenchDate }}</time>

My problem is that when I try to implement this on my Jekyll site, I get the following error message:

Liquid Exception: Invalid Date: 'nil' is not a valid datetime. in /_layouts/blog.html

How can I make this plugin work?

Robin Métral
  • 3,099
  • 3
  • 17
  • 32
  • 1
    Try adding `return "nil!" unless date` to the first line of each method. I suspect that one of your pages isn't passing the date somehow. – Max Jul 27 '18 at 18:39
  • Thanks @Max, this made me look at where I was actually setting the date. It was never on the page, but on posts! So I had to slightly alter my code, and it's working. – Robin Métral Jul 28 '18 at 00:25

1 Answers1

1

I figured it out!

Even though I am editing a page, the date was set in the front matter of my posts.

Therefore this is how I have to call the plugin:

<time datetime="{{ post.date | html5date }}">{{ post.date | frenchDate }}</time>

My code was returning nil because the date is undefined on my page itself.

Robin Métral
  • 3,099
  • 3
  • 17
  • 32