Merge pull request #1724 from open-sausages/pulls/4.0/i18n-symfony

Upgrade cms for new i18n backend
This commit is contained in:
Daniel Hensby 2017-01-26 09:06:16 +00:00 committed by GitHub
commit 554bbc90a2
9 changed files with 148 additions and 71 deletions

View File

@ -597,8 +597,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
$result = new ArrayList(); $result = new ArrayList();
foreach($classes as $class) { foreach($classes as $class) {
$instance = singleton($class); $instance = SiteTree::singleton($class);
if($instance instanceof HiddenClass) { if($instance instanceof HiddenClass) {
continue; continue;
} }
@ -608,27 +607,16 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
continue; continue;
} }
$addAction = $instance->i18n_singular_name(); $singularName = $instance->i18n_singular_name();
$description = $instance->i18n_description();
// Get description (convert 'Page' to 'SiteTree' for correct localization lookups)
$i18nClass = ($class == 'Page') ? 'SilverStripe\\CMS\\Model\\SiteTree' : $class;
$description = _t($i18nClass . '.DESCRIPTION');
if(!$description) {
$description = $instance->uninherited('description');
}
if($class == 'Page' && !$description) {
$description = SiteTree::singleton()->uninherited('description');
}
$result->push(new ArrayData(array( $result->push(new ArrayData(array(
'ClassName' => $class, 'ClassName' => $class,
'AddAction' => $addAction, 'AddAction' => $singularName,
'Description' => $description, 'Description' => $description,
// TODO Sprite support // TODO Sprite support
'IconURL' => $instance->stat('icon'), 'IconURL' => $instance->stat('icon'),
'Title' => singleton($class)->i18n_singular_name(), 'Title' => $singularName,
))); )));
} }
@ -931,7 +919,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
// Don't allow navigating into children nodes on filtered lists // Don't allow navigating into children nodes on filtered lists
$fields = array( $fields = array(
'getTreeTitle' => _t('SiteTree.PAGETITLE', 'Page Title'), 'getTreeTitle' => _t('SiteTree.PAGETITLE', 'Page Title'),
'singular_name' => _t('SiteTree.PAGETYPE'), 'singular_name' => _t('SiteTree.PAGETYPE', 'Page Type'),
'LastEdited' => _t('SiteTree.LASTUPDATED', 'Last Updated'), 'LastEdited' => _t('SiteTree.LASTUPDATED', 'Last Updated'),
); );
/** @var GridFieldSortableHeader $sortableHeader */ /** @var GridFieldSortableHeader $sortableHeader */

View File

@ -2,7 +2,9 @@
namespace SilverStripe\CMS\Controllers; namespace SilverStripe\CMS\Controllers;
use Page;
use SilverStripe\Admin\AddToCampaignHandler; use SilverStripe\Admin\AddToCampaignHandler;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Controller; use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse; use SilverStripe\Control\HTTPResponse;
@ -86,14 +88,14 @@ class CMSPageEditController extends CMSMain {
public function getAddToCampaignForm($id) public function getAddToCampaignForm($id)
{ {
// Get record-specific fields // Get record-specific fields
$record = \Page::get()->byID($id); $record = SiteTree::get()->byID($id);
if (!$record) { if (!$record) {
$this->httpError(404, _t( $this->httpError(404, _t(
'AssetAdmin.ErrorNotFound', 'AssetAdmin.ErrorNotFound',
'That {Type} couldn\'t be found', 'That {Type} couldn\'t be found',
'', '',
['Type' => _t('SiteTree.SINGULARNAME')] ['Type' => Page::singleton()->i18n_singular_name()]
)); ));
return null; return null;
} }
@ -102,7 +104,7 @@ class CMSPageEditController extends CMSMain {
'AssetAdmin.ErrorItemPermissionDenied', 'AssetAdmin.ErrorItemPermissionDenied',
'It seems you don\'t have the necessary permissions to add {ObjectTitle} to a campaign', 'It seems you don\'t have the necessary permissions to add {ObjectTitle} to a campaign',
'', '',
['ObjectTitle' => _t('SiteTree.SINGULARNAME')] ['ObjectTitle' => Page::singleton()->i18n_singular_name()]
)); ));
return null; return null;
} }

View File

@ -447,7 +447,7 @@ class CMSPageHistoryController extends CMSMain {
public function Breadcrumbs($unlinked = false) { public function Breadcrumbs($unlinked = false) {
$crumbs = parent::Breadcrumbs($unlinked); $crumbs = parent::Breadcrumbs($unlinked);
$crumbs[0]->Title = _t('CMSPagesController.MENUTITLE'); $crumbs[0]->Title = _t('CMSPagesController.MENUTITLE', 'Pages');
return $crumbs; return $crumbs;
} }

View File

@ -20,7 +20,7 @@ class CMSPageSettingsController extends CMSMain {
public function Breadcrumbs($unlinked = false) { public function Breadcrumbs($unlinked = false) {
$crumbs = parent::Breadcrumbs($unlinked); $crumbs = parent::Breadcrumbs($unlinked);
$crumbs[0]->Title = _t('CMSPagesController.MENUTITLE'); $crumbs[0]->Title = _t('CMSPagesController.MENUTITLE', 'Pages');
return $crumbs; return $crumbs;
} }

View File

@ -355,7 +355,7 @@ HTML;
$dateObj = DBField::create_field('Datetime', $date); $dateObj = DBField::create_field('Datetime', $date);
// $dateObj->setVal($date); // $dateObj->setVal($date);
return "<div id=\"SilverStripeNavigatorMessage\">" . return "<div id=\"SilverStripeNavigatorMessage\">" .
_t('ContentController.ARCHIVEDSITEFROM') . _t('ContentController.ARCHIVEDSITEFROM', 'Archived site from') .
"<br>" . $dateObj->Nice() . "</div>"; "<br>" . $dateObj->Nice() . "</div>";
} }
} }

View File

@ -10,8 +10,10 @@ use SilverStripe\CMS\Controllers\ContentController;
use SilverStripe\CMS\Controllers\ModelAsController; use SilverStripe\CMS\Controllers\ModelAsController;
use SilverStripe\CMS\Controllers\RootURLController; use SilverStripe\CMS\Controllers\RootURLController;
use SilverStripe\CMS\Forms\SiteTreeURLSegmentField; use SilverStripe\CMS\Forms\SiteTreeURLSegmentField;
use SilverStripe\Control\ContentNegotiator;
use SilverStripe\Control\Controller; use SilverStripe\Control\Controller;
use SilverStripe\Control\Director; use SilverStripe\Control\Director;
use SilverStripe\Control\RequestHandler;
use SilverStripe\Core\ClassInfo; use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Config\Config; use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Convert; use SilverStripe\Core\Convert;
@ -106,7 +108,9 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
* @config * @config
* @var array * @var array
*/ */
private static $allowed_children = array("SilverStripe\\CMS\\Model\\SiteTree"); private static $allowed_children = [
self::class
];
/** /**
* The default child class for this page. * The default child class for this page.
@ -185,8 +189,8 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
); );
private static $many_many = array( private static $many_many = array(
"ViewerGroups" => "SilverStripe\\Security\\Group", "ViewerGroups" => Group::class,
"EditorGroups" => "SilverStripe\\Security\\Group", "EditorGroups" => Group::class,
); );
private static $has_many = array( private static $has_many = array(
@ -241,18 +245,11 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
*/ */
private static $icon = null; private static $icon = null;
/** private static $extensions = [
* @config Hierarchy::class,
* @var string Description of the class functionality, typically shown to a user Versioned::class,
* when selecting which page type to create. Translated through {@link provideI18nEntities()}. SiteTreeLinkTracking::class,
*/ ];
private static $description = 'Generic content page';
private static $extensions = array(
'SilverStripe\\ORM\\Hierarchy\\Hierarchy',
'SilverStripe\\ORM\\Versioning\\Versioned',
"SilverStripe\\CMS\\Model\\SiteTreeLinkTracking"
);
private static $searchable_fields = array( private static $searchable_fields = array(
'Title', 'Title',
@ -302,6 +299,46 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
protected $_cache_statusFlags = null; protected $_cache_statusFlags = null;
/**
* Plural form for SiteTree / Page classes. Not inherited by subclasses.
*
* @config
* @var string
*/
private static $base_plural_name = 'Pages';
/**
* Plural form for SiteTree / Page classes. Not inherited by subclasses.
*
* @config
* @var string
*/
private static $base_singular_name = 'Page';
/**
* Description of the class functionality, typically shown to a user
* when selecting which page type to create. Translated through {@link provideI18nEntities()}.
*
* @see SiteTree::description()
* @see SiteTree::i18n_description()
*
* @config
* @var string
*/
private static $description = null;
/**
* Description for Page and SiteTree classes, but not inherited by subclasses.
* override SiteTree::$description in subclasses instead.
*
* @see SiteTree::description()
* @see SiteTree::i18n_description()
*
* @config
* @var string
*/
private static $base_description = 'Generic content page';
/** /**
* Fetches the {@link SiteTree} object that maps to a link. * Fetches the {@link SiteTree} object that maps to a link.
* *
@ -315,7 +352,7 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
* @param bool $cache True (default) to use caching, false to force a fresh search from the database * @param bool $cache True (default) to use caching, false to force a fresh search from the database
* @return SiteTree * @return SiteTree
*/ */
static public function get_by_link($link, $cache = true) { public static function get_by_link($link, $cache = true) {
if(trim($link, '/')) { if(trim($link, '/')) {
$link = trim(Director::makeRelative($link), '/'); $link = trim(Director::makeRelative($link), '/');
} else { } else {
@ -819,7 +856,7 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
*/ */
public function getParent() { public function getParent() {
if ($parentID = $this->getField("ParentID")) { if ($parentID = $this->getField("ParentID")) {
return DataObject::get_by_id("SilverStripe\\CMS\\Model\\SiteTree", $parentID); return DataObject::get_by_id(self::class, $parentID);
} }
return null; return null;
} }
@ -1459,7 +1496,7 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
)); ));
} }
$charset = Config::inst()->get('SilverStripe\\Control\\ContentNegotiator', 'encoding'); $charset = ContentNegotiator::config()->get('encoding');
$tags[] = FormField::create_tag('meta', array( $tags[] = FormField::create_tag('meta', array(
'http-equiv' => 'Content-Type', 'http-equiv' => 'Content-Type',
'content' => 'text/html; charset=' . $charset, 'content' => 'text/html; charset=' . $charset,
@ -1573,6 +1610,7 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
// If there is no URLSegment set, generate one from Title // If there is no URLSegment set, generate one from Title
$defaultSegment = $this->generateURLSegment(_t( $defaultSegment = $this->generateURLSegment(_t(
'CMSMain.NEWPAGE', 'CMSMain.NEWPAGE',
'New {pagetype}',
array('pagetype' => $this->i18n_singular_name()) array('pagetype' => $this->i18n_singular_name())
)); ));
if((!$this->URLSegment || $this->URLSegment == $defaultSegment) && $this->Title) { if((!$this->URLSegment || $this->URLSegment == $defaultSegment) && $this->Title) {
@ -1707,7 +1745,9 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
} }
if(!self::config()->nested_urls || !$this->ParentID) { if(!self::config()->nested_urls || !$this->ParentID) {
if(class_exists($this->URLSegment) && is_subclass_of($this->URLSegment, 'SilverStripe\\Control\\RequestHandler')) return false; if(class_exists($this->URLSegment) && is_subclass_of($this->URLSegment, RequestHandler::class)) {
return false;
}
} }
// Filters by url, id, and parent // Filters by url, id, and parent
@ -1959,6 +1999,7 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
->setURLPrefix($baseLink) ->setURLPrefix($baseLink)
->setDefaultURL($this->generateURLSegment(_t( ->setDefaultURL($this->generateURLSegment(_t(
'CMSMain.NEWPAGE', 'CMSMain.NEWPAGE',
'New {pagetype}',
array('pagetype' => $this->i18n_singular_name()) array('pagetype' => $this->i18n_singular_name())
))); )));
$helpText = (self::config()->nested_urls && $this->Children()->count()) $helpText = (self::config()->nested_urls && $this->Children()->count())
@ -2844,18 +2885,55 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
} }
/** /**
* Return the translated Singular name. * Default singular name for page / sitetree
* *
* @return string * @return string
*/ */
public function i18n_singular_name() { public function singular_name() {
// Convert 'Page' to 'SiteTree' for correct localization lookups $base = in_array(static::class, [Page::class, self::class]);
/** @skipUpgrade */ if ($base) {
// @todo When we namespace translations, change 'SiteTree' to FQN of the class return $this->stat('base_singular_name');
$class = (static::class == 'Page' || static::class === self::class) }
? 'SiteTree' return parent::singular_name();
: static::class; }
return _t($class.'.SINGULARNAME', $this->singular_name());
/**
* Default plural name for page / sitetree
*
* @return string
*/
public function plural_name() {
$base = in_array(static::class, [Page::class, self::class]);
if ($base) {
return $this->stat('base_plural_name');
}
return parent::plural_name();
}
/**
* Get description for this page
*
* @return string|null
*/
public function description() {
$base = in_array(static::class, [Page::class, self::class]);
if ($base) {
return $this->stat('base_description');
}
return $this->stat('description');
}
/**
* Get localised description for this page
*
* @return string|null
*/
public function i18n_description() {
$description = $this->description();
if ($description) {
return _t(static::class.'.DESCRIPTION', $description);
}
return null;
} }
/** /**
@ -2867,17 +2945,11 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
public function provideI18nEntities() { public function provideI18nEntities() {
$entities = parent::provideI18nEntities(); $entities = parent::provideI18nEntities();
if(isset($entities['Page.SINGULARNAME'])) $entities['Page.SINGULARNAME'][3] = CMS_DIR; // Add optional description
if(isset($entities['Page.PLURALNAME'])) $entities['Page.PLURALNAME'][3] = CMS_DIR; $description = $this->description();
if ($description) {
$entities[static::class . '.DESCRIPTION'] = array( $entities[static::class . '.DESCRIPTION'] = $description;
$this->stat('description'), }
'Description of the page type (shown in the "add page" dialog)'
);
$entities['SiteTree.SINGULARNAME'][0] = 'Page';
$entities['SiteTree.PLURALNAME'][0] = 'Pages';
return $entities; return $entities;
} }
@ -2897,8 +2969,7 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
self::$cache_permissions = array(); self::$cache_permissions = array();
} }
static public function on_db_reset() { public static function on_db_reset() {
self::$cache_permissions = array(); self::$cache_permissions = array();
} }
} }

View File

@ -314,8 +314,8 @@ de:
SINGULARNAME: Weiterleitungsseite SINGULARNAME: Weiterleitungsseite
SilverStripe\CMS\Model\SiteTree: SilverStripe\CMS\Model\SiteTree:
DESCRIPTION: 'Allgemeine Inhaltsseite' DESCRIPTION: 'Allgemeine Inhaltsseite'
PLURALNAME: Seitenbäume PLURALNAME: Seiten
SINGULARNAME: Seitenbaum SINGULARNAME: Seite
SilverStripe\CMS\Model\VirtualPage: SilverStripe\CMS\Model\VirtualPage:
DESCRIPTION: 'Zeigt den Inhalt einer anderen Seite an' DESCRIPTION: 'Zeigt den Inhalt einer anderen Seite an'
PLURALNAME: 'Virtuelle Seiten' PLURALNAME: 'Virtuelle Seiten'

View File

@ -338,18 +338,30 @@ en:
SilverStripe\CMS\Model\ErrorPage: SilverStripe\CMS\Model\ErrorPage:
DESCRIPTION: 'Custom content for different error cases (e.g. "Page not found")' DESCRIPTION: 'Custom content for different error cases (e.g. "Page not found")'
PLURALNAME: 'Error Pages' PLURALNAME: 'Error Pages'
PLURALS:
one: 'An Error Page'
other: '{count} Error Pages'
SINGULARNAME: 'Error Page' SINGULARNAME: 'Error Page'
SilverStripe\CMS\Model\RedirectorPage: SilverStripe\CMS\Model\RedirectorPage:
DESCRIPTION: 'Redirects to an internal page or an external URL' DESCRIPTION: 'Redirects to an internal page or an external URL'
PLURALNAME: 'Redirector Pages' PLURALNAME: 'Redirector Pages'
PLURALS:
one: 'A Redirector Page'
other: '{count} Redirector Pages'
SINGULARNAME: 'Redirector Page' SINGULARNAME: 'Redirector Page'
SilverStripe\CMS\Model\SiteTree: SilverStripe\CMS\Model\SiteTree:
DESCRIPTION: 'Generic content page' DESCRIPTION: 'Generic content page'
PLURALNAME: 'Site Trees' PLURALNAME: Pages
SINGULARNAME: 'Site Tree' PLURALS:
one: 'A Page'
other: '{count} Pages'
SINGULARNAME: Page
SilverStripe\CMS\Model\VirtualPage: SilverStripe\CMS\Model\VirtualPage:
DESCRIPTION: 'Displays the content of another page' DESCRIPTION: 'Displays the content of another page'
PLURALNAME: 'Virtual Pages' PLURALNAME: 'Virtual Pages'
PLURALS:
one: 'A Virtual Page'
other: '{count} Virtual Pages'
SINGULARNAME: 'Virtual Page' SINGULARNAME: 'Virtual Page'
SiteConfig: SiteConfig:
DEFAULTTHEME: '(Use default theme)' DEFAULTTHEME: '(Use default theme)'

View File

@ -790,6 +790,7 @@ class SiteTreeTest extends SapphireTest {
$sitetree = new SiteTree(); $sitetree = new SiteTree();
$sitetree->Title = _t( $sitetree->Title = _t(
'CMSMain.NEWPAGE', 'CMSMain.NEWPAGE',
'New {pagetype}',
array('pagetype' => $sitetree->i18n_singular_name()) array('pagetype' => $sitetree->i18n_singular_name())
); );
$sitetree->write(); $sitetree->write();
@ -817,10 +818,13 @@ class SiteTreeTest extends SapphireTest {
$sitetree = new SiteTree(); $sitetree = new SiteTree();
$sitetree->Title = _t( $sitetree->Title = _t(
'CMSMain.NEWPAGE', 'CMSMain.NEWPAGE',
'New {pagetype}',
array('pagetype' => $sitetree->i18n_singular_name()) array('pagetype' => $sitetree->i18n_singular_name())
); );
$sitetree->write(); $sitetree->write();
$this->assertEquals($sitetree->URLSegment, 'neue-seite', $this->assertEquals(
'neue-seite',
$sitetree->URLSegment,
'Sets based on default title on first save' 'Sets based on default title on first save'
); );