Robin Métral
Robin Métral

Reputation: 3209

Translating Jekyll dates

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?

Upvotes: 1

Views: 564

Answers (1)

Robin M&#233;tral
Robin M&#233;tral

Reputation: 3209

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.

Upvotes: 1

Related Questions