GridView, translation of records

I’v found the solution:

http://www.yiiframework.com/forum/index.php/topic/75907-gridview-translation-of-records/page__view__findpost__p__313655


<?= GridView::widget([

        'dataProvider' => $dataProvider,

         ...   

            

            [

                'attribute' => yii::t('app', 'fruit')

            ],

            

         ...



Doesn’t work:


yii::t('app', 'fruit')

Thanks in advance.


<?= GridView::widget([

        'dataProvider' => $dataProvider,

         ...   

            

            [

                'attribute' => 'payment_company',

                'label' => yii::t('app', 'payment_company')

            ],

            

         ...



Or, I would rather recommend you using “yii::t(‘app’, ‘payment_company’)” in attributeLabels() method in your model class.

Thanks, but I didn’t mean the lable. The payment_company was bad example. So I changed the Gridview with ‘fruit’.

the records of the fruit are:

Apple

pear

I want to tranlate it into German in the column "fruit":

Apfel

Birne

I have tryed:


 

1.

['attribute' => yii::t('app', '{fruit}', ['fruit' => 'fruit'])],


2.

yii::t('app', '{fruit}', ['fruit' => 'fruit'])


3.

yii::t('app', 'fruit')



But they are all show the same:

Apple

pear

not:

Apfel

Birne

I’m not sure you can do what you are trying to do. The Yii::t() looks at a file, ‘app’ in your case, looks for ‘fruit’, and pulls what it has as the translation. You would need to have a translation in there FOR EVERY FRUIT possible.

Data in a table is normally displayed AS IS. You need to look at another mechanism, Lookup table or extra column, to support the translations of the data keeping in mind EVERY language you want to support.

So here the solution(table: Billing; attribute: payment_company).

to translate of all records of ‘payment_company’:

  1. call the getModels(), method of ActiveDataProvider.

  2. use foreach translate the records.

That’s all!


Example, in controller:




            $dataProvider = new ActiveDataProvider([

                'query' => Billing::find()

            ]);

            

            $dataProviderModels = $dataProvider->getModels();

            

            foreach($dataProviderModels as $key=>$item){

                $dataProviderModels[$key]['payment_company'] = yii::t('app', $dataProviderModels[$key]['payment_company']);

            }


             return $this->render('index', [

                'dataProvider' => $dataProvider,

             ]);




End