SilverStripe翻译fieldlabels

时间:2016-07-26 13:37:48

标签: silverstripe

我只是使用_t()来翻译DataObject中的CMS字段:TextField::create('Title', _t('cms.TitleField', 'Title'));。我认为翻译$summary_fields同样简单,但事实并非如此。

我没有尝试单独翻译Fields及其附带的summary_fields,而是相信我注意到使用FieldLabels中使用的函数SiteTree更好地翻译这些字段的方式。

我是否可以在一个地方翻译这两个字段(DRY原则)并通过调用var轻松应用于这两个字段?

1 个答案:

答案 0 :(得分:1)

是的我肯定会说FieldLabels用于本地化/翻译是因为评论"本地化字段(如果可能)" DataObject代码中的here ...

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;
 }