Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

General

Multiple languages in blog date

Solved by Marco Cevoli View solution

Started by Ton Haarmans 5 years ago · 6 replies · 883 views
5 years ago

I am busy with a blog in 2 languages (NL & EN). I need to have the date of the posts also in the right language.
In the template blog_item.html.twig the date is constructed by this rule:
<span><i class="fa fa-clock-o"></i> {{ page.date|date(config.system.pages.dateformat.short) }}</span>

How should I change this rule to get the date in the right language???
Thanks.

5 years ago

If you want to translate months or similar, then check this

If you want to change date format only...
Well.. I'm going to need this one myself and was thinking a bit, but didn't get to implementing it yet.
Was thinking of having some kind of config of date formats for each language and then in twig use a filter. Probably a plugin would be the best solution here, combining config and twig filter using that config.

5 years ago

@TonHaarmans, You could try the following:

  • Create file '/user/translations/nl.yaml' with content:
    TXT
    DATE_FORMAT: d-m-Y
    
  • Create file '/user/translations/en.yaml' with content:
    TXT
    DATE_FORMAT: Y/m/d
    
  • In Twig template:
    TWIG
    {{ page.date|date('DATE_FORMAT'|t) }}
    

If you need months like 'March', April' you should have a look at the url shared by @Karmalakas

5 years ago

Nice workaround 🙂 Will keep in mind for when I need it

5 years ago

Thanks all. It is not about the date-format, but indeed about the names of the months and things like 1st, 2nd, etc. The link to the documentation is not helping me much. I managed to change the names of the month in the archives, but not the date of the blog postings. If you want, have a look at https://tijdelijk.marjolijnhof.nl/nl/blog

5 years ago Solution

I have this in my blog.html.twig template:
<span>{{ child.header.date|date('d') }} {{ 'GRAV.MONTHS_OF_THE_YEAR'|ta(child.header.date|date('n') - 1) }} {{ child.header.date|date('Y') }}</span>
You can see the result here: https://qabiria.com/it/risorse/blog
Months are translated, but not the date order.

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 99 16 hours ago
General · by pamtbaau, 21 hours ago
1 63 21 hours ago
General · by Andy Miller, 1 day ago
0 48 1 day ago
General · by Marcel, 12 months ago
6 357 5 days ago
General · by Duc , 6 days ago
3 45 6 days ago