I18n omit text if parameter is empty

I have a case where the translation is being dynamically constructed to create a simple sentence based on user input. The phrase when complete looks like this

From {from} until {until} on {on}

What I would like to achieve is that if a parameter is null then that part of the text should be omitted.

For example, the translation

Yii::t('test', 'From {from} until {until} on {on}', [
    'from' => 'some html',
    'until' => null,
   'on' => 'some other html',
])

I would like it to generate something like
From some HTML on some other HTML

I’ve seen cases such as plural, select, etc but I can’t see if there’s a way to achieve this conditional branching and how.

Any hints are appreciated.

Have you tried?

Yii::t('test', 'From {from} {until} on {on}', [
    'from' => 'some html',
    'until' => ! empty( $condition ) ? 'until ' . $until_date : null,
    'on' => 'some other html',
])

The thing is that you are trying to remove the “until” text that comes before the {until} tag. While this method can only replace tag contents…