DataObject + TranslatableDataObject + ModelAdmin

时间:2015-08-16 10:02:56

标签: internationalization silverstripe data-objects modeladmin

设置:SS 3.1.13,Translatable 2.0.8和TranslatableDataObject dev-master。

我有两个DataObject类:“Facility”($ has_one)和“FacilityType”($ has_many),我选择在ModelAdmin中管理它们(因为它们是网站所有者的全局属性)。然而,尽管仔细阅读了设置说明here,我仍然无法获得TranslatableDataObject扩展来实际为它们启用翻译。

这是我的_config.php:

// Set the site locale
i18n::set_locale('en_US');

SiteConfig::add_extension('Translatable');

// Set allowed locales
Translatable::set_allowed_locales(array('en_US', 'pl_PL'));
TranslatableDataObject::set_locales(array('en_US', 'pl_PL'));
Facility::add_extension("Translatable");
Facility::add_extension("TranslatableDataObject"); // tried "TranslatableDataObject('Title')" as well
FacilityCategory::add_extension("Translatable");
FacilityCategory::add_extension("TranslatableDataObject");

有人可以确认这个设置应该或不适合我吗?

FacilityCategory.php

class FacilityCategory extends DataObject {
    private static $db = array(
      'Title' => 'Varchar'
    );

    private static $has_many = array(
      'Facilities' => 'Facility.FacilityCategory'
    );

    public function getCMSFields(){
        $fields = FieldList::create(
            TextField::create('Title')
        );
        return $fields;
    }
}

class FacilityCategoryAdmin extends ModelAdmin {
    private static $menu_title = 'Facility Categories';
    private static $url_segment = 'facility-categories';
    private static $managed_models = array(
        'FacilityCategory'
    );
}

Facility.php

class Facility extends DataObject {
    private static $db = array(
        'Title' => 'Varchar',
    );

    private static $has_one = array(
        'Category' => 'FacilityCategory'
    );

    private static $summary_fields = array(
        'Title' => 'Name',
        'Category.Title' => 'Type'
    );

    public function getCMSFields(){
        $fields = FieldList::create(
            TextField::create('Title'),
            DropdownField::create('CategoryID', 'Category')
                ->setSource(FacilityCategory::get()->map()->toArray())
                ->setEmptyString('-- select a category --')
        );
        return $fields;
    }
}

class FacilityAdmin extends ModelAdmin {
    private static $menu_title = 'Facilities';
    private static $url_segment = 'facilities';
    private static $managed_models = array(
        'Facility'
    );
}

1 个答案:

答案 0 :(得分:2)

我已经为你研究了这个。

我使用以下方法安装了新的SS网站:

composer create-project silverstripe/installer

然后我添加了可翻译:

composer require silverstripe/translatable ~2.0.8

我的_config.php看起来像这样:

<?php

global $project;
$project = 'mysite';

global $database;
$database = '';

require_once('conf/ConfigureFromEnv.php');

// Set the site locale
i18n::set_locale('en_GB');
Translatable::set_default_locale('en_GB');
MyObj::add_extension('Translatable');

我创建了一个ModelAdmin。

我在浏览器中导航到/ dev / build?flush = all

然后我导航到CMS中的ModelAdmin,我看到了:

Translatable tab showing

Translatable tab contents

我认为问题的一部分是你一次使用两个可翻译的模块,你只需要一个。

除此之外,我不知道为什么你不会看到可翻译的标签。您是否收到任何PHP错误,请您刷新或完成/ dev / build?