Locale specific date liquid code

Hi,
I used a pre-built template for my site and wanted to change the names of months to my local language, but don’t know how to implment it in the current code.

I want to change:
January - Januar
February - Februar
March - Marec
and so forth.

I found the code, but don’t have much experience with liquid:
{% 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” }}

My current code is:
Source: Github: https://github.com/nandinozderic/nandinozderic.github.io/blob/main/_layouts/archive.html

layout: default

<ul class="archive__items">
  {% for post in site.posts %}
    {% assign month = post.date | date: '%B %Y' %}
    {% unless post.next %}<!-- first post -->
      <h4 class="archive__month">{{ month }}</h4>
    {% else %}
      {% capture next_month %}{{ post.next.date | date: '%B %Y' }}{% endcapture %}
      {% if month != next_month %}
        <h4 class="archive__month">{{ month }}</h4>
      {% endif %}
    {% endunless %}
    <li class="archive__item">
      <time class="archive__day" datetime="{{ post.date | date_to_xmlschema }}">{{ post.date | date: '%d' }}</time>
      <a class="archive__link" href="{{ post.url | prepend: site.baseurl }}">{{ post.title }}</a>
    </li>
  {% endfor %}
</ul>

and

Source: Github: _includes/post dot html

 </ul>
    {% endif %}
    {% if post.date %}
    <time class="article__date" datetime="{{ post.date | date_to_xmlschema }}">{% if page.url == '/privacy/' or page.url == '/terms/' %}Last updated: {% endif %}{{ post.date | date: site.date_format }}</time>
    {% endif %}
  </section>
  {% endif %}

My pages are:


The other is ending in /blog

Thanks

2 Likes

I recommend using triple backticks for formatting in the forum. Check help.

E.g.

```
My code
```

It makes it monospaced and highlighted and separates well from plain text

I recommend putting your entire snippet in an includes file

_includes/locale-date.html

{% 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” }}

Then use on page or layout as

## My title

Last updated: {% include locale-date.html %}

Which will render as 1 Januar 2020 for example

1 Like

I would not even use if post.date because a post has to have a date. It is required in the filename.

More relevant example for your code.

<time class="article__date" datetime="{{ post.date | date_to_xmlschema }}">
  {% if page.url == '/privacy/' or page.url == '/terms/' %}Last updated: {% endif %}
  {% include locale-date.html %}
</time>
   
1 Like

Thank you Michael,
I put the snippet here _includes/locale-date.html but now I don’t know how/where do I include this {% include locale-date.html %} in the code below

layout: default

<ul class="archive__items">
  {% for post in site.posts %}
    {% assign month = post.date | date: '%m %Y' %}
    {% unless post.next %}<!-- first post -->
      <h4 class="archive__month">{{ month }}</h4>
    {% else %}
      {% capture next_month %}{{ post.next.date | date: '%m %Y' }}{% endcapture %}
      {% if month != next_month %} 
        <h4 class="archive__month">{{ month }}</h4>
      {% endif %}
    {% endunless %}
    <li class="archive__item">
      <time class="archive__day" datetime="{{ post.date | date_to_xmlschema }}">{{ post.date | date: '%d' }}</time>
      <a class="archive__link" href="{{ post.url | prepend: site.baseurl }}">{{ post.title }}</a>
    </li>
  {% endfor %} 
</ul>

Note: I have 0% experience with liquid and maybe 5% with html

The code you sent renders the month and then the day and then a post title.

And if the month is the same between two posts it doesnt repeat it.

If you want to get rid of that system and just use 1 Januar 2020 etc. for each post then you can take out most of the code and do this:

<ul class="archive__items">
  {% for post in site.posts %}
    <li class="archive__item">
      <time class="archive__day" datetime="{{ post.date | date_to_xmlschema }}">
        {% include locale-date.html %}
      </time>
      <a class="archive__link" href="{{ post.url | prepend: site.baseurl }}">
         {{ post.title }}
       </a>
    </li>
  {% endfor %} 
</ul>

If you want to keep the month heading and day under as before but just use to locale month, then you could do this.

Setup includes file to be month. No year. No day.

_includes/locale-date.html

{% assign m = page.date | date: “%-m” %}
{% 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 %}

But it is trickier to get that to fit into the snippet you sent because of use of post.next and having to pass a date to the includes.