Multilanguage text interspersed with dynamic content

I have some text that needs lots of dynamic content inside:

<?= t('voting-closed') ?>.
<?= t('vote-on') ?> <span><?= $next_day->starts()->toDate('%e') ?>
<?= t('august') ?></span> <?= t('for') ?> <span><?= $next_day->title()->lower() ?></span>
<?= t('of-the') ?>, <?= t('from-time') ?>
<span><?= $next_day->starts()->toDate('%k:%M') ?> <?= t('a.m') ?></span>
<?= t('until-time') ?> <span><?= $next_day->ends()->toDate('%k:%M') ?>
<?= t('a.m') ?></span> <?= t('of-next-day') ?>

This outputs something like: “Voting is closed at this time. Vote at 23rd of August, for the third day of the competition, from 21:00 a.m until 20:00 a.m of the next day”.

I know it looks bad! What is the best way to insert these variables?

Using I::18n class, in particular is more useful here, I think.

Also, try to avoid unnecessary translation like month name and times by using appropriate strftime date syntax.

21.00 a.m doesn’t make sense anyway. If you use 24-hour format, don’t use a.m/p.m.

1 Like

Thanks. Putting a.m/p.m was silly! Also, I can’t seem to find how to set the locale.

I have 'date.handler' => 'strftime' in my config, a date field, and I am outputting it like this: <?= strftime('%A', $results_datetime->toDate()) ?>. This outputs the day’s name in English no matter the language selected.

If the date handler is set, and locale in config, you output a date like this:

$format = '%e %B'; // e.g. 23 August, whatever format you need here, the format can also be taken from your list of translations if you need different formats per language
echo $page->date()->toDate($format); 

Edit: If you are in a multi-lang environment, the locale is set in your language files, not need to set it in config.

In my language file I had the locale set to 'locale' => 'el_GR' as described here, but it needed to be 'locale' => 'el_GR.URF8' to work.