【问题标题】:SilverStripe translate fieldlabelsSilverStripe 翻译字段标签
【发布时间】:2016-07-26 13:37:48
【问题描述】:

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

我相信我没有尝试单独翻译字段及其随附的 summary_fields,而是使用SiteTree 中使用的函数FieldLabels 来更好地翻译这些字段。

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

【问题讨论】:

    标签: silverstripe


    【解决方案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;
     }
    

    【讨论】:

    • 多么有趣的发现。我将在接下来的一个小时里重新编码我在我的 cms 中命名所有字段名的方式。
    • 我要在我的自定义 PageType 中定义一个 public function fieldLabels(),在填充 summary_fields 时,DataObject 类会自动选择它吗?
    • 函数 fieldLabels 正在寻找静态 field_labels 来获取此信息,我不会创建该函数
    猜你喜欢
    • 1970-01-01
    • 2011-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多