23
votes

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.

8

8 Answers

21
votes

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

13
votes

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.

9
votes

Use plugin i18n from jekyll supported plugins page.

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

8
votes

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.

3
votes

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 }}
2
votes

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>
2
votes

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
0
votes

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