Semicolon
Semicolon

Reputation: 1914

SilverStripe translate fieldlabels

I simply use _t() to translate CMS Fields in a DataObject: TextField::create('Title', _t('cms.TitleField', 'Title'));. I thought translating $summary_fields was just as simple, but it's not.

Instead of trying to translate Fields and their accompanying summary_fields seperately, I believe I noticed a better way how these fields are translated using the function FieldLabels as used in SiteTree.

Is there way I can translate these both fields in one place (DRY principle) and apply to both easily by calling the var?

Upvotes: 1

Views: 135

Answers (1)

Barry
Barry

Reputation: 3318

Yes I would certainly say the use of FieldLabels is for localisation / translation because of the comment "Localize fields (if possible)" here in the DataObject code...

public function summaryFields() {
     $fields = $this->stat('summary_fields');

     // if fields were passed in numeric array,
     // convert to an associative array
     if($fields && array_key_exists(0, $fields)) {
         $fields = array_combine(array_values($fields), array_values($fields));
     }

     if (!$fields) {
         $fields = array();
         // try to scaffold a couple of usual suspects
         if ($this->hasField('Name')) $fields['Name'] = 'Name';
         if ($this->hasDatabaseField('Title')) $fields['Title'] = 'Title';
         if ($this->hasField('Description')) $fields['Description'] = 'Description';
         if ($this->hasField('FirstName')) $fields['FirstName'] = 'First Name';
     }
     $this->extend("updateSummaryFields", $fields);

     // Final fail-over, just list ID field
     if(!$fields) $fields['ID'] = 'ID';

     // Localize fields (if possible)
     foreach($this->fieldLabels(false) as $name => $label) {
         // only attempt to localize if the label definition is the same as the field name.
         // this will preserve any custom labels set in the summary_fields configuration
         if(isset($fields[$name]) && $name === $fields[$name]) {
             $fields[$name] = $label;
         }
     }

     return $fields;
 }

Upvotes: 1

Related Questions