BUGFIX Collecting i18n entities for decorators separately from the decorated classes, as decorated properties like $db have to be stored in the module of the decorated, not in the module of the decorated class.

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@65057 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2008-11-01 23:16:45 +00:00
parent fd7bc64107
commit d102a902b9
9 changed files with 70 additions and 9 deletions

View File

@ -12,6 +12,7 @@
*
* @package sapphire
* @subpackage i18n
* @uses i18nTextCollector->collectFromEntityProviders()
*/
interface i18nEntityProvider {

View File

@ -188,6 +188,9 @@ class i18nTextCollector extends Object {
return $entitiesArr;
}
/**
* @uses i18nEntityProvider
*/
function collectFromEntityProviders($filePath) {
$entitiesArr = array();

View File

@ -2843,7 +2843,7 @@ class DataObject extends ViewableData implements DataObjectInterface,i18nEntityP
public function provideI18nEntities() {
$entities = array();
$db = $this->uninherited('db', true);
$db = eval("return {$this->class}::\$db;");
if($db) foreach($db as $name => $type) {
$entities["{$this->class}.db_{$name}"] = array(
$name,
@ -2852,7 +2852,7 @@ class DataObject extends ViewableData implements DataObjectInterface,i18nEntityP
);
}
$has_many = $this->uninherited('has_many', true);
$has_many = eval("return {$this->class}::\$has_many;");
if($has_many) foreach($has_many as $name => $class) {
$entities["{$this->class}.has_many_{$name}"] = array(
$name,
@ -2861,7 +2861,7 @@ class DataObject extends ViewableData implements DataObjectInterface,i18nEntityP
);
}
$many_many = $this->uninherited('many_many', true);
$many_many = eval("return {$this->class}::\$many_many;");
if($many_many) foreach($many_many as $name => $class) {
$entities["{$this->class}.many_many_{$name}"] = array(
$name,

View File

@ -6,7 +6,7 @@
* @package sapphire
* @subpackage model
*/
abstract class DataObjectDecorator extends Extension {
abstract class DataObjectDecorator extends Extension implements i18nEntityProvider {
/**
* Statics on a {@link DataObject} subclass
@ -150,6 +150,21 @@ abstract class DataObjectDecorator extends Extension {
if($field_labels) $lables = array_merge($lables, $field_labels);
}
}
function provideI18nEntities() {
$entities = array();
$fields = $this->extraDBFields();
$translatableAttributes = array('db','has_one','has_many','many_many');
foreach($fields as $att => $spec) {
if(!in_array($att, $translatableAttributes)) continue;
foreach($spec as $name => $type) {
$entities["{$this->class}.{$att}_{$name}"] = array($name);
}
}
return $entities;
}
}

View File

@ -0,0 +1,3 @@
<?php
Object::extend('i18nTestModule', 'i18nTestModuleDecorator');
?>

View File

@ -1,12 +1,10 @@
<?php
class i18nOtherModule extends Object {
function __construct() {
function mymethod() {
_t(
'i18nOtherModule.ENTITY',
'Other Module Entity',
);
parent::__construct();
}
}
?>

View File

@ -0,0 +1,11 @@
<?php
class i18nTestModuleDecorator extends DataObjectDecorator {
function extraDBFields() {
return array(
'db' => array(
'MyExtraField' => 'Varchar'
)
);
}
}
?>

View File

@ -1,5 +1,10 @@
<?php
class i18nTestModule extends Object {
class i18nTestModule extends DataObject implements TestOnly {
static $db = array(
'MyField' => 'Varchar',
);
function myMethod() {
_t(
'i18nTestModule.ENTITY',

View File

@ -29,10 +29,12 @@ class i18nTextCollectorTest extends SapphireTest {
global $_CLASS_MANIFEST;
$_CLASS_MANIFEST['i18nTestModule'] = $this->alternateBasePath . '/i18ntestmodule/code/i18nTestModule.php';
$_CLASS_MANIFEST['i18nTestModule_Addition'] = $this->alternateBasePath . '/i18ntestmodule/code/i18nTestModule.php';
$_CLASS_MANIFEST['i18nTestModuleDecorator'] = $this->alternateBasePath . '/i18nothermodule/code/i18nTestModuleDecorator.php';
global $_ALL_CLASSES;
$_ALL_CLASSES['parents']['i18nTestModule'] = array('Object'=>'Object');
$_ALL_CLASSES['parents']['i18nTestModule'] = array('DataObject'=>'DataObject','Object'=>'Object');
$_ALL_CLASSES['parents']['i18nTestModule_Addition'] = array('Object'=>'Object');
$_ALL_CLASSES['parents']['i18nTestModuleDecorator'] = array('DataObjectDecorator'=>'DataObjectDecorator','Object'=>'Object');
global $_TEMPLATE_MANIFEST;
$_TEMPLATE_MANIFEST['i18nTestModule.ss'] = array(
@ -452,6 +454,29 @@ PHP;
$matches['i18nTextCollectorTestMySubObject.SINGULARNAME'][0]
);
}
function testCollectDecoratedFields() {
$c = new i18nTextCollector();
$c->basePath = $this->alternateBasePath;
$c->baseSavePath = $this->alternateBaseSavePath;
$c->run();
$moduleLangFile = "{$this->alternateBaseSavePath}/i18ntestmodule/lang/" . $c->getDefaultLocale() . '.php';
$moduleLangFileContent = file_get_contents($moduleLangFile);
$this->assertNotContains(
"\$lang['en_US']['i18nTestModuleDecorator']['db_MyExtraField'] = 'MyExtraField';",
$moduleLangFileContent,
'Decorated fields are not stored in the module of the decorated file if the decorator is located in another module'
);
$otherModuleLangFile = "{$this->alternateBaseSavePath}/i18nothermodule/lang/" . $c->getDefaultLocale() . '.php';
$otherModuleLangFileContent = file_get_contents($otherModuleLangFile);
$this->assertContains(
"\$lang['en_US']['i18nTestModuleDecorator']['db_MyExtraField'] = 'MyExtraField';",
$otherModuleLangFileContent,
'Decorated fields are stored in the module in which the decorator is placed'
);
}
}
?>