Quicktip: Using placeholders in language strings
Make language translations more powerful with placeholders.
Translations in language files usually look like this:
But what if you need more flexibility, if you want to add string parts dynamically, e.g. a number, a link, or the name of the file in the above example?
Placeholders to the rescue:
In your code, you can now access the translation strings and replace your placeholders using the
I18n::template() method like this:
echo I18n::template('file.success', null, [ 'filename' => $file->filename() ]); // Result: "The file some-image.jpg was successfully uploaded." $max = 3; echo I18n::template('file.max.error', null, [ 'max' => $max ]); // Result: "You can only upload 3 files." $from = 8; $to = 20; echo I18n::template('business.hours', null, [ 'from' => $from, 'to' => $to ]); // Result: "Our business hours are from 8 to 20."