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/file-translator/helloWorldWithName.php simply with the following command:

<?= $text ?>

It will display the $text value passed from the controller.

Test it by pointing the browser to http://hostname/basic/web/file-translator/hello-world-with-name, also passing the ?name= parameter, otherwise there will be no name at the end of the text.

Translations can be prepared using the message command that we have just seen:

$ ./yii message config/i18n.php

This will automatically create a new marker Hello World! I'm {name} in the basic/messages subfolders.

The placeholders can be specialized with two other attributes: ParameterType and ParameterStyle, adding a comma after PlaceholderName. So, the full form to specify a placeholder will be as follows:

{PlaceholderName, ParameterType, ParameterStyle}

Here, ParameterType can be:

  • number : The ParameterStyle can be an integer, currency, percent, or custom pattern (for example, 000)
  • date: The ParameterStyle can be short, medium, long, full, or custom pattern (for example, dd/mm/yyyy)
  • time: The ParameterStyle can be short, medium, long, full or custom pattern (for example, hh:mm)
  • spellout: There is no ParameterStyle
  • ordinal: There is no ParameterStyle
  • duration: There is no ParameterStyle

The most used message formatting is probably plural, and that allows us to specify different key strings based on the number passed as a parameter.

Consider the following code as an example:

// if $n = 0, it shows "There are no books!"
// if $n = 1, it shows "There is one book!"
// if $n = 4, it shows "There are 4 books!"

echo Yii::t('app', 'There {n, plural, =0{are no books} =1{is one book} other{are # books}}!', ['n' => $n]);

Here, =0 stands for the message to be displayed when $n is 0, =1 stands for the message to be displayed when $n is 1, and other stands for the message to be displayed when $n is other than 0 and 1.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.234.83