Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Translating a DataObject using the _t() method?

Tags:

silverstripe

I am trying to translate a DataObject using the _t() method.

I have been using it on Pages without a problem, but it doesn't seem to work on data objects.

class SliderItem extends DataObject {

    private static $default_sort = 'Sort';

    private static $db = array(
        'Sort' => 'Int',
        'Title' => 'Varchar(255)',
        'Summary' => 'Text'
    );


    public function getCMSFields() {
        $fields = parent::getCMSFields();
        $fields->addFieldsToTab('Root.Main', array(
            TextField::create(_t('SliderItem.Title', 'Title')),
            TextareaField::create(_t('SliderItem.Summary', 'Summary'))
        ));
        $fields->removeByName(array('HomePageID', 'Sort'));
        return $fields;
    }
}

mysite/lang/de.yml

de:
  SliderItem:
    Title: 'I want to change this'
    Summary: 'And this..'

Why does this example not work?

like image 459
ifusion Avatar asked Apr 10 '26 00:04

ifusion


1 Answers

Make sure to define the variable name in the FormField constructor as the first parameter and the translatable title as the second parameter.

This:

TextField::create(_t('SliderItem.Title', 'Title'))

Should be this:

TextField::create('Title', _t('SliderItem.Title', 'Title'))

Your getCMSFields function should look something like this:

public function getCMSFields() {
    $fields = parent::getCMSFields();

    $fields->addFieldsToTab('Root.Main', array(
        TextField::create('Title', _t('SliderItem.Title', 'Title')),
        TextareaField::create('Summary', _t('SliderItem.Summary', 'Summary'))
    ));

    $fields->removeByName(array('HomePageID', 'Sort'));

    return $fields;
}
like image 54
3dgoo Avatar answered Apr 14 '26 22:04

3dgoo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!