D
D
Dmitry Dobryshin2020-12-30 11:06:21
Yii
Dmitry Dobryshin, 2020-12-30 11:06:21

How to use internationalization in Yii2 config?

Actually the essence. in all places of the project Yii::t('app', 'Original text') works, but there was a need for a global setting in $config . And there I run into a problem. At the time of initialization, the internationalization settings are not working yet, so the specified method always returns "Original text" . How or in what method can the global setting be overridden so that text is localized?
Code example:

$config = [
    ...
    'language' => 'ru-RU',
    'components' => [
        'i18n' => [
            'translations' => [
                '*' => [
                    'class' => 'yii\i18n\PhpMessageSource',
                    'sourceLanguage' => 'en-US',            // Исходный текст на английском
                    'basePath' => '@app/messages',
                    'fileMap' => [
                        'app'       => 'app.php',           // Для приложения
                    ],
                ],
            ],
        ],
    ],
    'container' => [
        'definitions' => [
            'yii\widgets\LinkPager' => [
                // Здесь нужна локализация, но так не работает.
                'firstPageLabel' => Html::tag('span', '', [ 'class' => 'glyphicon glyphicon-fast-backward', 'title' => Yii::t('app', 'First page'), ]),
                'prevPageLabel' => Html::tag('span', '', [ 'class' => 'glyphicon glyphicon-backward', 'title' => Yii::t('app', 'Previous page'), ]),
                'nextPageLabel' => Html::tag('span', '', [ 'class' => 'glyphicon glyphicon-forward', 'title' => Yii::t('app', 'Next page'), ]),
                'lastPageLabel' => Html::tag('span', '', [ 'class' => 'glyphicon glyphicon-fast-forward', 'title' => Yii::t('app', 'Last page'), ]),
            ],
        ],
    ],
    ...
];

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Dobryshin, 2020-12-30
@DimkaI

Thanks to the suggestion, Dmitry did the following:
in the root of the project in the /widgets directory, he created the ILinkPager.php file :

<?php
namespace app\widgets;

use Yii;
use Yii\helpers\Html;
use Yii\widgets\LinkPager;

class ILinkPager extends yii\widgets\LinkPager
{
    public function __construct() {
        $this->firstPageLabel = Html::tag('span', '', [ 'class' => 'glyphicon glyphicon-fast-backward', 'title' => Yii::t('app', 'First page'), ]);
        $this->prevPageLabel  = Html::tag('span', '', [ 'class' => 'glyphicon glyphicon-backward', 'title' => Yii::t('app', 'Previous page'), ]);
        $this->nextPageLabel  = Html::tag('span', '', [ 'class' => 'glyphicon glyphicon-forward', 'title' => Yii::t('app', 'Next page'), ]);
        $this->lastPageLabel  = Html::tag('span', '', [ 'class' => 'glyphicon glyphicon-fast-forward', 'title' => Yii::t('app', 'Last page'), ]);
    }
}

in the configuration file /config/web.php overridden the container:
$config = [
    ...
    'container' => [
        'definitions' => [
            'yii\grid\GridView' => [
                'pager' => [
                    'class' => 'app\widgets\ILinkPager',
                ],
            ],
        ],
    ],
   ...
];

As a result, internationalization began to work correctly on all pages of the paginator.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question