SilverStripe - Multilingual Custom Form Template

220 views Asked by At

I am building a page with two languages (English and German). For this I am using the translatable module offered on the SilverStripe page (http://addons.silverstripe.org/add-ons/silverstripe/translatable). On regular pages it works just like a charm.

The contact form gets created in a controller. By $form->setTemplate I load a custom template for the form, which is located in the template's include subdirectory. In the class file I defined the different TextFields (model part), which I want to use in the form template.

Here is the controller:

class MietenPage extends Page {

    private static $db = array (
        //Form
        'ChoosPakage' => 'Varchar',
        'FormEventHeadline' => 'Varchar',
        'FormNoteHeadline' => 'Varchar',
        'FormContactHeadline' => 'Varchar',
        'FormAdditionalHeadline' => 'Varchar',
        'FormCheckOverlay' => 'Varchar',
        'FormCheckCaseDesign' => 'Varchar',
        'FormCheckAGB' => 'Varchar',
        'FormSubmit' => 'Varchar',
        'AGBAlert' => 'Varchar'
    );

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

        //...

        return $fields;
    }
}

class MietenPage_Controller extends Page_Controller {

    private static $allowed_actions = array('ContactForm');

    public function ContactForm() { 
        $fields = new FieldList( 
            //...
        ); 
        $actions = new FieldList( 
            new FormAction('submit', 'Anfrage Senden!') 
        );

        $form = new Form($this, 'ContactForm', $fields, $actions); 
        $form->setTemplate('ContactFormTemplate');

        return $form;

    }

    public function submit($data, $ContactForm) { 
        $email = new Email(); 

        //...
    }
}

In my template I am calling the form using $ContactForm. This works totally fine, including all functionalities.

My Problem is, that I can not access the variables (like headline or submit button text), which hold the text in my custom template - just emptyness is returned. I think it has to do something with scope, but I can not solve this problem.

1

There are 1 answers

0
Martimiz On

From your example I can't see where your variables live, and how you planned to use them, but to create a multlingual form, you'd best use the _t() functionality: https://docs.silverstripe.org/en/3.1/developer_guides/i18n/#translating-text

You'd use that for any 'hardcoded' text, in php as well as in your templates.