Problem translating month names

Hi,

on a multi-language site (German/English/French) I output a date like this:
<?= strftime("%B %Y", $entry->date()->toDate()) ?>
On my local machine this translates the month name in all languages.
On the server however the month name is correctly translated in French, while it displays the English month names when the front end language is set to German.
Any idea what could be wrong here?

Thanks a lot,
Till

strftime relies on the OS level locale settings and language packs. Looks like your server doesn’t have the German language packs installed. Besides this, I’d recommend against the use of strftime as it has been deprecated as of PHP 8.1.

The good news is that you can use the IntlDateFormatter class. That uses language packs that come (as far as I understand it) bundled with PHP, so you don’t rely on OS functionality that changes from machine to machine.

The even better news is that Kirby 3.6.5 comes with support for the IntlDateFormmater stuff, you just have to configure it. Add this to your config:

return [
    'date'  => [
        'handler' => 'intl'
    ]
];

Then format your date like this:

<?= $entry->date()->toDate('MMMM y') ?>

The pattern uses the ICU style: Formatting Dates and Times - ICU Documentation

1 Like

Thank you for your reply and detailed explanation, glad I could learn something! I updated Kirby, applied the changes, and everything worked fine on my local machine. On the server though it had the effect that now also in French it shows the English month names, where before it would translate them. In German nothing changed, still English month names.

And now, five minutes later when I checked again before posting, on the production server all three languages show the French month names. This happened after I did a hard refresh on the French version.
After some minutes I did a hard refresh on the English version and now all three languages show the English language names again. As if the server somehow caches the month names and uses them for some minutes. This does not happen locally.

Also this only seems to happen with English and French – I can’t get the German month names to display.

This behavior is consistent across browsers (tested on Chrome/Chrome Incognito mode/Firefox/Safari).

Both installations run on PHP 7.4.28, locally I run nginx, production is running LiteSpeed.

In case it helps, this is the page in question: https://cervo.swiss/de/events

See 'intl' date handler outputs random translation on multilanguage site · Issue #4304 · getkirby/kirby · GitHub

Strange, seems to work, at least sometimes:
French: “mai”

German: “Mai”

English: “May”

Yes, it sometimes works, sometimes doesn’t.

Sorry I wasn’t clear: The screen shots show an external booking widget that is injected into the header, this has nothing to do with kirby. The month names I mean are below in the main area, the categories in the event calendar.

Looked at the source code… Yes, the kirby implementation doesn’t seem to use the locale to format dates… So I guess IntlDateFormatter falls back to the process locale (like strftime does), and that is probably why it’s random… (the process can only be in 1 language, therefore if your server uses the FPM module, many requests share the same process and one might override the language on another (in the middle of it). That was afaik the whole point of having intldateformatter. )

Anyway, until kirby fixes the implementation, you should be able to work around the issue by giving kirby your own formatter:

<?php 
$formatter = new IntlDateFormatter(
    $kirby->languageCode(), 
    IntlDateFormatter::FULL, 
    IntlDateFormatter::FULL,
    'Europe/Berlin',
    IntlDateFormatter::GREGORIAN,
    'MMMM y'
);
?>

....

<?= $entry->date()->toDate($formatter) ?>
2 Likes


This looks promising :slight_smile:

1 Like

Yes, I already applied your fix, it works like a charm. Just had to replace $kirby with kirby().
Thank you very much for the quick and detailed help!

1 Like