Hi, on a site that is almost finished I’d like to output the dates in german vs. english.
I could swear the dates were in german before. But maybe I’ve put this off until the last moment.
I don’t want to use 'handler' => 'intl'
since this would force me to update all sort of aspects of the site, since it’s date heavy.
I’ve tried to apply this in the config.php:
'languages' => [
'detect' => true
],
'locale' => 'de_CH.utf8',
'date' => [
'handler' => 'date',
'weekday' => 1 // will only work in kirby 5.x
],
I’ve checked in the terminal with locale -a
if my locale is available. Which it is.
Is there a simple way to convert for instance this portion of code into german, without using the intl
syntax?
<h3 class="h1">
<?= $date->date()->toDate('d.') ?>
<span style="font-size: 19px"><?= $date->date()->toDate('F Y') ?></span>
</h3>
<p>
<?= $date->date()->toDate('l') ?>
<?= $date->timeStart()->toDate('H:i') ?> – <?= $date->timeEnd()->toDate('H:i') ?><br>
<?= $date->spots()->toInt(); ?> freie Plätze<br>
<?= $projectTitle ?>
</p>
It’s a single language page at the moment.
I don’t mind ALL dates beeing in german!
Any help highly appreciated!
Sorry: I’ve forgot some essential details:
"kirby": "4.7.1",
"php": "8.3.22",
"server": "LiteSpeed",
"license": "Kirby Enterprise",
"languages": []
You can use the IntlDateFormatter without using intl in the config, example:
$dateFormatter = new IntlDateFormatter( "de_DE" ,IntlDateFormatter::LONG, 0,
'Europe/Berlin');
echo $dateFormatter->format($page->date()->toDate());
Check out the PHP docs for details: PHP: IntlDateFormatter::create - Manual
Ok, that was the motivation I needed. I knew this was possible but I thought there might be an easier way.
The Problem with my code was that ->toDate('F Y')
(F equals Month) and <?= $date->date()->toDate('l') ?>
(l equals Day) were displayed in english. So to correct this situation I’ve created two intl date formatters and replaced them in the code.
The result looks like this:
<?php
// Um daten in verschiedenen Sprachen darzustellen, muss man entweder in config.php date auf 'handler' => 'intl' stellen oder temporär auf intl umschalten (wie hier)
// formatter für Monat
$monthFormat = new IntlDateFormatter('de_DE', IntlDateFormatter::NONE, IntlDateFormatter::NONE, 'Europe/Zurich', IntlDateFormatter::GREGORIAN, 'MMMM' /* voller Monatsname */ );
// formatter für Wochentag
$dayFormat = new IntlDateFormatter( 'de_DE', IntlDateFormatter::NONE, IntlDateFormatter::NONE, 'Europe/Zurich', IntlDateFormatter::GREGORIAN, 'EEEE' /* voller Wochentagsname */ );
$dateToFormat = $date->date()->toDate();
?>
<h3 class="h1">
<?= $date->date()->toDate('d.') ?>
<span style="font-size: 19px"><?= $monthFormat->format($dateToFormat); ?> <?= $date->date()->toDate('Y') ?></span>
</h3>
<p>
<?= $dayFormat->format($dateToFormat) ?>
<?= $date->timeStart()->toDate('H:i') ?> – <?= $date->timeEnd()->toDate('H:i') ?><br>
<?= $date->spots()->toInt(); ?> freie Plätze<br>
<?= $projectTitle ?>
</p>
Finally it was less work than I’ve thought.
Thanks a lot Sonja!