Locale specific date in jekyll
Asked Answered
K

9

28

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.

Klansman answered 23/5, 2012 at 7:1 Comment(0)
Z
23

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

Zoril answered 20/4, 2015 at 20:29 Comment(0)
C
15

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 passing m with the value "6" to site.data.fr.months[m], I only saw the trick when looking at Falc answer.

Cardcarrying answered 23/6, 2016 at 14:19 Comment(0)
K
9

Use plugin i18n from jekyll supported plugins page.

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

Klansman answered 24/5, 2012 at 12:57 Comment(4)
How can I specify the pt-BR locale? That is, the region part of the locale?Yeomanry
simply specify "pt-BR" instead of "lt", the list of supported locales is here: github.com/svenfuchs/rails-i18n/tree/master/rails/localeKlansman
Liquid Exception: Object must be a Date, DateTime or Time object. nil given. in _posts/2012-09-15-test.html.evn:ruby 2.0 ,jekyll-newestTetrachloride
It is a year old answer, maybe something changed.Klansman
F
9

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.

Freebooter answered 25/10, 2016 at 21:31 Comment(1)
It works with github pages and is the best solution I foundZusman
M
3

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>
Maher answered 2/10, 2015 at 19:27 Comment(0)
E
3

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 }}
Enid answered 5/12, 2016 at 3:39 Comment(0)
G
2

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
Gosport answered 14/8, 2016 at 21:16 Comment(0)
A
1

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
Authorization answered 25/7, 2021 at 15:45 Comment(0)
C
1

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
Coatee answered 21/4, 2022 at 13:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.