Jump to:

5513 Posts in 1732 Topics by 1218 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Allowing multiple language values for Textfield in CMS

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

Page: 1
Go to End
Author Topic: 1254 Views
  • weareorganism
    Avatar
    Community Member
    14 Posts

    Allowing multiple language values for Textfield in CMS Link to this post

    Hi,

    I feel like I must be missing something here and am not sure what. I'm running Silverstripe 2.4.2 and have configured Translatable and i18n as follows in my _config.php file.

    <?php
    /* ...
    */

    Object::add_extension('SiteTree', 'Translatable');
    Translatable::set_default_locale("en_CA");

    i18n::enable();
    global $allowed_locales;
    $allowed_locales = array(
    'fr_CA' => array('French (Canada)', 'fran&ccedil;ais'),
    'en_CA' => array('English (Canada)', 'English'),
    );
    i18n::$common_locales = $allowed_locales;
    i18n::set_locale('en_CA');

    I have been successful in creating pages in both english and french and the standard CMS fields (Page name, Navigation label, Content) all allow me to set different content in either of those languages.

    However, I have added a CMS field as follows:

    class LandingPage extends Page {
       public static $db = array(
             'ViewProjectLabel' => 'Text'
       );

    /* ... */

       function getCMSFields() {
          $fields = parent::getCMSFields();
          $viewprojectlabel = new TextField('ViewProjectLabel', 'View Project Label/Voir le projet');
          $fields->addFieldToTab('Root.Content.Settings', $viewprojectlabel);
          return $fields;
       }
    }

    /* ... */

    This field does not show up as the others do. Regardless which version of the page I'm editing (english or french) the field is always the same. The others show up as the familiar pairs of editable Content and greyed out Original Content.

    Do I have to do anything else when I set up this field to indicate that I would like it to set different values for it in each language?

    Please let me know if you need more info from me.

    Peter

  • weareorganism
    Avatar
    Community Member
    14 Posts

    Re: Allowing multiple language values for Textfield in CMS Link to this post

    Argh - I feel silly. Figured out where I was messing up. In _config.php I enabled Translatable for SiteTree and for some reason assumed that this extension would be inhereted by data objects declared in it's child classes SiteTree->Page->LandingPage.

    I have since edited my _config.php to include all of the classes that require Translatable fields:

    Object::add_extension('SiteTree', 'Translatable');
    Object::add_extension('SiteConfig', 'Translatable');
    Object::add_extension('LandingPage', 'Translatable');
    Object::add_extension('CategorySlidePage', 'Translatable');
    Object::add_extension('ProjectSlidePage', 'Translatable');

    BUT I'm still left with the challenge for displaying the previous translation in a Original Content block. If I figure that out I will update the thread. Feel free to beat me to the punch though!

  • weareorganism
    Avatar
    Community Member
    14 Posts

    Re: Allowing multiple language values for Textfield in CMS Link to this post

    Well now this is just getting embarassing... here is the code snippet for displaying a pair of fields showing Original Content and the translation Content:

          // If a translation exists, exchange them with
          // original/translation field pairs
          $translation = $this->getTranslation(Translatable::default_locale());
          if($translation && $this->Locale != Translatable::default_locale()) {
             $transformation = new Translatable_Transformation($translation);
             $fields->replaceField(
                'ViewProjectLabel',
                $transformation->transformFormField($viewprojectlabel)
             );
          }


    This snippet is directly from the SilverStripe docs: http://doc.silverstripe.org/multilingualcontent

    Hope this can be of some help to others.

  • Stefdv
    Avatar
    Community Member
    110 Posts

    Re: Allowing multiple language values for Textfield in CMS Link to this post

    Hello, i've found that snippit also...It does place the extra field but did you figure out how to call that field in a template so it 'switches' according to the chosen locale?

  • swaiba
    Avatar
    Forum Moderator
    1784 Posts

    Re: Allowing multiple language values for Textfield in CMS Link to this post

    I've recently done translation for a site and if you have Translateable added as an extension for the dataobject then you will get the translated version in the template - just the same as pages.

    1254 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.