Translate-plugin: Retrieve translated value for backend list column value

Created on 24 Sep 2018  路  3Comments  路  Source: rainlab/translate-plugin

Hi,

In my backend, i have a plugin which displays a list of products.
Those products can have a translated content into the creation form in other languages.
I would like to display in my backend list both languages.

I'm using a Partial column type but when i get back my datas, they are always in french, and even if i dump the $record to see the model used. It's not showing me the translation. Only translable fields.

Is there a way to retrieve all translation in a partial ?

thanks for your help

Question

Most helpful comment

Yes, sure. Let's say you have a model like this:

use Model;
use RainLab\Translate\Behaviors\TranslatableModel;

class Item extends Model
{
    public $implement = [TranslatableModel::class];
    public $translatable = ['name'];

    public $table = 'demo_demo_items';

    // this gets the name in english
    public function getNameEnAttribute()
    {
        return $this->noFallbackLocale()->lang('en')->name;
    }

    // this gets the name in french
    public function getNameFrAttribute()
    {
        return $this->noFallbackLocale()->lang('fr')->name;
    }
}

and in your columns.yaml

columns:
    id:
        label: ID
        type: number
    name:
        label: name
        type: text
        sortable: true
    name_en:
        label: name (en)
        type: text
        sortable: false
    name_fr:
        label: name (fr)
        type: text
        sortable: false

note: you don't need to call noFallbackLocale() but if you don't, it'll fall back and display your default language which is usually not what you want in a list view.

hope this helps.

All 3 comments

Yes, sure. Let's say you have a model like this:

use Model;
use RainLab\Translate\Behaviors\TranslatableModel;

class Item extends Model
{
    public $implement = [TranslatableModel::class];
    public $translatable = ['name'];

    public $table = 'demo_demo_items';

    // this gets the name in english
    public function getNameEnAttribute()
    {
        return $this->noFallbackLocale()->lang('en')->name;
    }

    // this gets the name in french
    public function getNameFrAttribute()
    {
        return $this->noFallbackLocale()->lang('fr')->name;
    }
}

and in your columns.yaml

columns:
    id:
        label: ID
        type: number
    name:
        label: name
        type: text
        sortable: true
    name_en:
        label: name (en)
        type: text
        sortable: false
    name_fr:
        label: name (fr)
        type: text
        sortable: false

note: you don't need to call noFallbackLocale() but if you don't, it'll fall back and display your default language which is usually not what you want in a list view.

hope this helps.

Thanks for the response @munxar :)

Thanks a lot @munxar :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stidges picture stidges  路  6Comments

Ladylain picture Ladylain  路  3Comments

o0alexela0o picture o0alexela0o  路  4Comments

anonimo9 picture anonimo9  路  5Comments

sophie-vdv picture sophie-vdv  路  4Comments