'Locale specific date in jekyll

I am trying out jekyll for website creation. I am using jekyll-bootstrap.

The default configuration has the page archive, where all the posts are listed grouped by year and month of the post date. Currently the months appear in English. I've looked at the code and this is an excerpt which is responsible for putting the date:

{% capture this_month %}{{ post.date | date: "%B" }}{% endcapture %}

I've found a lot of information here, so there is a way to specify the desired locale. But how can you make jekyll respect it? Simply adding

default_locale: "lt"

in _config.yml naturally does not work.



Solution 1:[1]

You can overwrite the current month by using Liquid Date Format:

{% assign m = page.date | date: "%-m" %}
{{ page.date | date: "%-d" }}
{% case m %}
  {% when '1' %}Januar
  {% when '2' %}Februar
  {% when '3' %}März
  {% when '4' %}April
  {% when '5' %}Mai
  {% when '6' %}Juni
  {% when '7' %}Juli
  {% when '8' %}August
  {% when '9' %}September
  {% when '10' %}Oktober
  {% when '11' %}November
  {% when '12' %}Dezember
{% endcase %}
{{ page.date | date: "%Y" }}

If your date is, for example 2015-02-20, the output will be 20 Februar 2015

Solution 2:[2]

Because i18n is not available on github pages, I built upon answer of @Kleo Petroff and the answer of @Falc, I set up a way to have a date with locale names defined in a YAML file :

The code is almost the same without the whole case statement :

{% capture i18n_date %}
{{ page.date | date: "%-d" }}
{% assign m = page.date | date: "%-m" | minus: 1 %}
{{ site.data.fr.months[m] }}
{{ page.date | date: "%Y" }}
{% endcapture %}

I set the following data-structure (could be in _config.yml, or in some _data/some.yml file), in the above code the file is _data/fr.yml :

months:
    - Janvier
    - Février
    - Mars
    - Avril
    - Mai
    - Juin
    - Juillet
    - Aout
    - Septembre
    - Octobre
    - Novembre
    - Décembre

Note that page.date | date: "%-m" output the month number as a string, ie June number is actually "6" not 6, liquid silently casts that string to a number when piping the minus filter. During development it was not something I was aware and thus liquid didn't returned anything when passingmwith the value "6" tosite.data.fr.months[m]`, I only saw the trick when looking at Falc answer.

Solution 3:[3]

Use plugin i18n from jekyll supported plugins page.

Note that github pages does not support local plugins. See the related issue.

Solution 4:[4]

My turn to share my solution without plugin inspired by the previous ones: I've created an include with some parameters like: {% translated_date.html ... %}

The idea is to translate the months and days names respecting the format using the date filter syntax (ex: "%A %-d %B %Y"). The strings used for the translation are in yaml files stores in _data.

Code and usage available on the repo oncleben31/jekyll-date-basic-i18n.

Example of integration in my blog with Jekyll sources available in the repo oncleben31/oncleben31-cc. Look at the layouts post.html and home.html.

Solution 5:[5]

I started using the i18n plugin suggested by @mpictas but when Jekyll regenerates a page it starts to print "error" instead of the localized date. So I removed the plugin and started to use this simple code, similar to the "case/when" solution:

{% assign months = "Enero|Febrero|Marzo|Abril|Mayo|Junio|Julio|Agosto|Septiembre|Octubre|Noviembre|Diciembre" | split: "|" %}
{% assign m = page.date | date: "%-m" | minus: 1 %}
{% assign day = page.date | date: "%d" %}
{% assign month = months[m] %}
{% assign year = page.date | date: "%Y" %}
<span class="date">{{ day }}/{{ month }}/{{ year }}</span>

Solution 6:[6]

You also could write an own Liquid filter in a Ruby file like following:

module DateFilter
  MONTHS = %w(Januar Februar März April Mai Juni July August September Oktober November Dezember)

  def german_long_month(input)
    MONTHS[input.strftime("%m").to_i - 1]
  end
end

Liquid::Template.register_filter(DateFilter)

When you put this file into the _plugins folder of your Jekyll site you can use the filter in your template file like other filter.

{{ post.date | german_long_month }}

Solution 7:[7]

You can combine @Falc answer with jekyll-multiple-languages-plugin:

Simply use in template:

{% assign months = "january|february|march|april|may|june|july|august|september|october|november|december" | split: "|" %}
{% assign m = post.date | date: "%-m" | minus: 1 %}
{% assign day = post.date | date: "%d" %}
{% assign month = months[m] %}
{% assign year = post.date | date: "%Y" %}
<span class="post-meta">{{day}} {% t month %} {{year}}</span>

Then in _i18n/en.yml, .../pl.yml, .../any-language.yml:

january: January
february: February
march: March
april: April
may: May
june: June
july: July
august: August
september: September
october: October
november: November
december: December

Solution 8:[8]

I am working in a fully automated translations to locale to work on GitHub pages (no plugins), I modified previous answers to this solution as a first approximation of what I need:

{{ page.date | date: "%-d" }}
{% assign m = page.date | date: "%-m" | minus: 1 %}
{{ site.data.ui-text[site.locale].months[m] }}
{{ page.date | date: "%Y" }}

in _config.yml you can set de default site locale as in my case:

locale: "es-ES"

in _data I have a YAML file (ui-text.yaml) containing:

# English (default)
# -----------------
en: &DEFAULT_EN
  months: 
    - January 
    - February 
    - March 
    - April 
    - May 
    - June 
    - July
    - August 
    - September 
    - October 
    - November 
    - December

en-US:
  <<: *DEFAULT_EN

# Spanish
# -------
es: &DEFAULT_ES

months:
    - Enero
    - Febrero
    - Marzo
    - Abril
    - Mayo
    - Junio
    - Julio
    - Agosto
    - Septiembre
    - Octubre
    - Noviembre
    - Diciembre

es-ES:
  <<: *DEFAULT_ES

# french
# -------
fr: &DEFAULT_FR
months:
    - Janvier
    - Février
    - Mars
    - Avril
    - Mai
    - Juin
    - Juillet
    - Aout
    - Septembre
    - Octobre
    - Novembre
    - Décembre

Solution 9:[9]

Based upon the answer of @Brice I came up with this solution to output the month name in Spanish.

<p class="post-meta">
    {% assign mes_index = page.date | date: "%-m" | minus: 1 %}
    {% assign mes = site.data.spanish.meses[mes_index] %}

    <time datetime="{{ page.date | date_to_xmlschema }}">
        {{ page.date | date: "%-d" }} de {{ mes }} de {{ page.date | date: "%Y" }}
    </time>
</p>

"Mes" means "month" in Spanish.

_data/spanish.yml

meses:
  - Enero
  - Febrero
  - Marzo
  - Abril
  - Mayo
  - Junio
  - Julio
  - Agosto
  - Septiembre
  - Octubre
  - Noviembre
  - Diciembre

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Community
Solution 3 Bobík
Solution 4 Oncleben31
Solution 5 Falc
Solution 6
Solution 7 Daniel Kmak
Solution 8 kinsay
Solution 9 ManuAlvarado22