Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Customising the CMS

Allowing multiple language values for Textfield in CMS


Reply

5 Posts   1296 Views

Avatar
weareorganism

23 October 2010 at 10:32am Community Member, 14 Posts

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

Avatar
weareorganism

23 October 2010 at 1:30pm Community Member, 14 Posts

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!

Avatar
weareorganism

23 October 2010 at 1:45pm Community Member, 14 Posts

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: [url]http://doc.silverstripe.org/multilingualcontent[/url]

Hope this can be of some help to others.

Avatar
Stefdv

26 September 2011 at 8:50am Community Member, 110 Posts

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?

Avatar
swaiba

29 September 2011 at 5:44am Forum Moderator, 1796 Posts

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.