Jump to:

22997 Posts in 11620 Topics by 2827 members

General Questions

SilverStripe Forums » General Questions » [SOLVED] Custom Form and site Multilanguage

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1109 Views
  • biapar
    Avatar
    Forum Moderator
    435 Posts

    [SOLVED] Custom Form and site Multilanguage Link to this post

    Hi,

    I've a multilanguage site and I've created an input form with Form class in a Page class.

    How translate field label?

    Thank you

  • dompie
    Avatar
    Community Member
    88 Posts

    Re: [SOLVED] Custom Form and site Multilanguage Link to this post

    Translating fields can be done with the _t function, e.g.

    public function MyForm(){
    $fields = new FieldSet(
    new TextField('Name', _t('MyformPage.FIRST_LAST_NAME', 'Name'), '', 200),
    ...);
    return new Form(....);
    }

  • biapar
    Avatar
    Forum Moderator
    435 Posts

    Re: [SOLVED] Custom Form and site Multilanguage Link to this post

    How Do I use director::redirect in a multilanguage site?

    That is from that form I have to redirect to a page in the site that is page-it or page-en...

  • biapar
    Avatar
    Forum Moderator
    435 Posts

    Re: [SOLVED] Custom Form and site Multilanguage Link to this post

    In my page controller where there is form, I write:

    public function init(){
    parent::init();

    if($this->dataRecord->hasExtension('Translatable')) {
    i18n::set_locale($this->dataRecord->Locale);
    }
    }

    and for labels, I use _t('DisponibilitaPage.PRIVACY','Accetto Regolamento Privacy') in my Form costructor.

    I my include template , I use for label <% _t('ROOM','Camera') %>

    Language files was saved into lang folder under mysite. So, for this template, I use $lang['it_IT']['MytemplateUnderInclude.ss']['ROOM'] = 'Camera' and for class, I use $lang['it_IT']['DisponibilitaPage']['PRIVACY'] = 'Accetto Regolamento Privacy';

    Bye

  • Gelert
    Avatar
    Community Member
    8 Posts

    Re: [SOLVED] Custom Form and site Multilanguage Link to this post

    Thank you, that's brilliant. Solved my template translation issues too!

    I now have Italian and English text dropping into my custom form.

    I added the above

    if($this->dataRecord->hasExtension('Translatable')) {
       i18n::set_locale($this->dataRecord->Locale);
    }

    to mysite/page.php

    And then in mysite/lang folder I have:
    en_GB.php
    it_IT.php

    mysite/lang/en_GB.php:

    global $lang;
    $lang['en_GB']['Sidebar.ss']['SIDEBARTITLE'] = 'English Title';

    mysite/lang/it_IT.php:

    i18n::include_locale_file('mysite','en_GB');

    global $lang;

    if(array_key_exists('it_IT', $lang) && is_array($lang['it_IT'])) {
       $lang['it_IT'] = array_merge($lang['en_GB'], $lang['it_IT']);
    } else {
       $lang['it_IT'] = $lang['en_GB'];
    }

    $lang['it_IT']['Sidebar.ss']['SIDEBARTITLE'] = 'Italian Title';

    then in themes/mytheme/templates/Includes/Sidebar.ss I have:

    <h3><% _t('SIDEBARTITLE','My English Title') %></h3>

    This needs better documentation from Silverstripe itself. That little dataRecord>hasExtension bit in the Page Controller is totally KEY to it all working.

    Yet it's never mentioned anywhere! (I'm looking at you Silverstripe Documentation: http://doc.silverstripe.org/sapphire/en/topics/i18n).

    Update: now I know what I'm looking for I've found it on this page [Which I had read several times] (http://doc.silverstripe.org/sapphire/en/topics/translation) but says:

    Unser the heading: Setting the i18n locale
    You can set the i18n locale value which is used to format dates, currencies and other regionally different values to the same as your current page locale.

    That doesn't say to me include this code to get your lang/<locale>.php files working. Thanks to the wonderful users of the SS Forum these bits of code are sorted out.

    It's so difficult from a new user point of view.

    When I've finished the project I'm on, I might write my own tutorial about languages, filtering page data objects and custom side bar forms.

    All I don't know how to do now is make my Custom Form a "sticky" form. So if you select to filter by "Price Range 123" the drop down remembers that selection when the search is displayed instead of jumping back to the top item "Any". I guess I'll just be happy it's all working and get on with creating the rest of the site now.

    1109 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.