Approach to translating date structures (not only words)

This MAY no tbe Kirby specific.

An english date such as “December 5” in spanish would be “5 de Diciembre”.

Not only the words change, but the order too AND even a word is added or substracted.

This is not something that can be done with strftime alone, is it ?

How do you/people do it, and particularly in the Kirby context?

With if() clauses against the locale ?

Thank you

You could put the format into the translation variables.

Oh, and do something like toDate(t(‘this format’)) ?

Assuming strftime as date function.

Yes

that is actually perfect, thank you

Actually that was too pretty to be true, the formats are like this:

spanish
<?= 'Del ' . $page->fecha_inicio()->toDate('%e de %B') . ' al ' . $page->fecha_final()->toDate('%e de %B %Y') ?>

english
<?= $page->fecha_inicio()->toDate('%B %e') . ' to ' . $page->fecha_final()->toDate('%B %e , %Y') ?>

So they involve more than one field, more than one toDate and extra words between them.

I cannot have a code block in a language variable right? that is for strings isn’t it ?

So I guess I am looking for blocks of code that are language dependant. if() clauses against the locales then ?

thank you

Ah, ok, that’s not really about date formats anymore. For something like that, maybe best use language dependent snippets

date-snippet-es.php

<?= 'Del ' . $page->fecha_inicio()->toDate('%e de %B') . ' al ' . $page->fecha_final()->toDate('%e de %B %Y') ?>

date-snippet-en.php

<?= $page->fecha_inicio()->toDate('%B %e') . ' to ' . $page->fecha_final()->toDate('%B %e , %Y') ?>

In template:

<?php snippet('date-snippet-' . $kirby->language()->locale()) ?>
2 Likes

Ah, that is also an interesting solution, thank you

Another possibility is using templates in your language variables: https://getkirby.com/docs/cookbook/i18n/using-variables-in-language-strings

Spanish:

 'translations' => [
    'from.to'          => 'Del {startdate } al {enddate}',
    'date.from.format' => '%e de %B',
    'date.to.format'   => '%e de %B %Y'
]

English:

 'translations' => [
    'from.to'          => 'From {startdate } to {enddate ',
    'date.from.format' => '%B %e',
    'date.to.format'   => '%B %e , %Y'
]

Template:

<?php
$startDate = $page->fecha_inicio()->toDate(t('date.from.format'));
$endDate   = $page->fecha_final()->toDate(t('date.to.format'));

echo I18n::template('from.to', null, [
    'startdate'  => $startDate,
    'enddate'    => $endDate,
]);