Placeholders formatting
The Yii:t
method is not only limited to replace strings with their translation in other languages, but it handles the specific formatting of source strings to support many kinds of generalization.
Firstly, Yii:t()
supports placeholders in the following two formats:
- String in the
{nameOfPlaceholder}
format - Integer in the
{0}
format, and this type of placeholder is zero-based
Value arrays to replace the placeholder are passed as the third parameter to the Yii:t()
method.
For example, we want to display a page with only Hello World, I'm ...
by appending the custom name to the text.
Create basic/controllers/FileTranslatorController.php
:
public function actionHelloWorldWithName($name='') { $text = \Yii::t('app', 'Hello World! I\'m {name}', ['name' => $name]); return $this->render('helloWorldWithName', ['text' => $text]); }
Now, create the view in basic/views...