O'Reilly logo

Mastering Yii by Charles R. Portwood II

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Widget translations

In a similar vein, widgets can also have their own message translation files by following the same process outlined for modules. Using our GreetingWidget class we created in Chapter 5, Modules, Widgets, and Helpers would look as follows:

<?php
namespace app\components;

use yii\base\Widget;
use yii\helpers\Html;

use Yii;
class GreetingWidget extends Widget
{
    public $name = null;
    
    public $greeting;
    
    public function init()
    {
        parent::init();
        
        Yii::$app->i18n->translations['widgets/GreetingWidget*'] = [
            'class' => 'yii\i18n\PhpMessageSource',
            'sourceLanguage' => 'en-US',
            'basePath' => '@app/components/widgets/GreetingWidget'
        ];
        
        $hour = date('G');
        
        if ( $hour >= 5 && $hour <= 11 )
           $this->greeting = GreetingWidget::t("Good Morning"); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required