Skip to content

Commit

Permalink
BUGFIX Reverted auto-detection of i18n statics like $db in DataObject…
Browse files Browse the repository at this point in the history
… through provideI18nEntities() - was getting too complicated with decorated properties. Overload DataObject->fieldLabels() or DataObjectDecorator->updateFieldLabels() instead

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@65071 467b73ca-7a2a-4603-9d3b-597d59a354a9
  • Loading branch information
chillu committed Nov 2, 2008
1 parent 30f8c74 commit eb4c950
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 136 deletions.
28 changes: 1 addition & 27 deletions core/model/DataObject.php
Expand Up @@ -2543,6 +2543,7 @@ public function fieldLabels() {
}

$labels = array_merge((array)$autoLabels, (array)$customLabels);

$this->extend('updateFieldLabels', $labels);

return $labels;
Expand Down Expand Up @@ -2832,33 +2833,6 @@ static function enable_subclass_access() {
public function provideI18nEntities() {
$entities = array();

$db = eval("return {$this->class}::\$db;");
if($db) foreach($db as $name => $type) {
$entities["{$this->class}.db_{$name}"] = array(
$name,
PR_MEDIUM,
'Name of the object property, mainly used for automatically generating forms'
);
}

$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,
PR_MEDIUM,
'Name of an object relation, mainly used for automatically generating forms'
);
}

$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,
PR_MEDIUM,
'Name of an object relation, mainly used for automatically generating forms'
);
}

$entities["{$this->class}.SINGULARNAME"] = array(
$this->singular_name(),
PR_MEDIUM,
Expand Down
17 changes: 1 addition & 16 deletions core/model/DataObjectDecorator.php
Expand Up @@ -6,7 +6,7 @@
* @package sapphire
* @subpackage model
*/
abstract class DataObjectDecorator extends Extension implements i18nEntityProvider {
abstract class DataObjectDecorator extends Extension {

/**
* Statics on a {@link DataObject} subclass
Expand Down Expand Up @@ -162,21 +162,6 @@ function updateFieldLabels(&$lables){
if($field_labels) $lables = array_merge($lables, $field_labels);
}
}

function provideI18nEntities() {
$entities = array();
$fields = $this->extraStatics();
$translatableAttributes = array('db','has_one','has_many','many_many');
if($fields) foreach($fields as $att => $spec) {
if(!in_array($att, $translatableAttributes)) continue;

if($spec) foreach($spec as $name => $type) {
$entities["{$this->class}.{$att}_{$name}"] = array($name);
}
}

return $entities;
}

}

Expand Down
34 changes: 10 additions & 24 deletions tests/i18n/i18nTest.php
Expand Up @@ -16,36 +16,15 @@ function testDataObjectFieldLabels() {
i18n::set_locale('de_DE');
$obj = new i18nTest_DataObject();

$lang['en_US']['i18nTest_DataObject']['db_MyProperty'] = 'MyProperty';
$lang['de_DE']['i18nTest_DataObject']['db_MyProperty'] = 'Mein Attribut';
$lang['en_US']['i18nTest_DataObject']['MyProperty'] = 'MyProperty';
$lang['de_DE']['i18nTest_DataObject']['MyProperty'] = 'Mein Attribut';

$this->assertEquals(
$obj->fieldLabel('MyProperty'),
'Mein Attribut'
);

$lang['en_US']['i18nTest_DataObject']['has_one_HasOneRelation'] = 'HasOneRelation';
$lang['de_DE']['i18nTest_DataObject']['has_one_HasOneRelation'] = 'Eins zu eins';
$this->assertEquals(
$obj->fieldLabel('HasOneRelation'),
'Eins zu eins'
);

$lang['en_US']['i18nTest_DataObject']['has_many_HasManyRelation'] = 'HasManyRelation';
$lang['de_DE']['i18nTest_DataObject']['has_many_HasManyRelation'] = 'Viel zu eins';
$this->assertEquals(
$obj->fieldLabel('HasManyRelation'),
'Viel zu eins'
);

$lang['en_US']['i18nTest_DataObject']['many_many_ManyManyRelation'] = 'ManyManyRelation';
$lang['de_DE']['i18nTest_DataObject']['many_many_ManyManyRelation'] = 'Viel zu viel';
$this->assertEquals(
$obj->fieldLabel('ManyManyRelation'),
'Viel zu viel'
);

$lang['en_US']['i18nTest_DataObject']['db_MyUntranslatedProperty'] = 'MyUntranslatedProperty';
$lang['en_US']['i18nTest_DataObject']['MyUntranslatedProperty'] = 'MyUntranslatedProperty';
$this->assertEquals(
$obj->fieldLabel('MyUntranslatedProperty'),
'My Untranslated Property'
Expand Down Expand Up @@ -106,6 +85,13 @@ class i18nTest_DataObject extends DataObject implements TestOnly {
'ManyManyRelation' => 'Member'
);

function fieldLabels() {
$labels = parent::fieldLabels();
$labels['MyProperty'] = _t('i18nTest_DataObject.MyProperty', 'My Property');

return $labels;
}

}

class i18nTest_Object extends Object implements TestOnly, i18nEntityProvider {
Expand Down
68 changes: 0 additions & 68 deletions tests/i18n/i18nTextCollectorTest.php
Expand Up @@ -380,10 +380,6 @@ function testCollectFromFilesystemAndWriteMasterTables() {
"\$lang['en_US']['i18nTestModule']['WITHNAMESPACE'] = 'Include Entity with Namespace';",
$moduleLangFileContent
);
$this->assertContains(
"\$lang['en_US']['i18nTestModule']['db_MyField']",
$moduleLangFileContent
);
$this->assertContains(
"\$lang['en_US']['i18nTestModuleInclude.ss']['NONAMESPACE'] = 'Include Entity without Namespace';",
$moduleLangFileContent
Expand Down Expand Up @@ -416,77 +412,13 @@ function testCollectFromEntityProvidersInCustomObject() {
array(
'i18nTextCollectorTestMyObject.PLURALNAME',
'i18nTextCollectorTestMyObject.SINGULARNAME',
'i18nTextCollectorTestMyObject.db_FirstProperty',
'i18nTextCollectorTestMyObject.db_SecondProperty',
'i18nTextCollectorTestMyObject.has_many_Relation',
)
);
$this->assertEquals(
'FirstProperty',
$matches['i18nTextCollectorTestMyObject.db_FirstProperty'][0]
);
$this->assertEquals(
'My Object',
$matches['i18nTextCollectorTestMyObject.SINGULARNAME'][0]
);
}

function testCollectFromEntityProvidersInCustomSubClass() {
$c = new i18nTextCollector();

$filePath = Director::baseFolder() . '/sapphire/tests/i18n/i18nTextCollectorTestMySubObject.php';
$matches = $c->collectFromEntityProviders($filePath);
$this->assertEquals(
array_keys($matches),
array(
'i18nTextCollectorTestMySubObject.PLURALNAME',
'i18nTextCollectorTestMySubObject.SINGULARNAME',
'i18nTextCollectorTestMySubObject.db_SubProperty',
'i18nTextCollectorTestMySubObject.has_many_SubRelation',
)
);
$this->assertEquals(
'SubProperty',
$matches['i18nTextCollectorTestMySubObject.db_SubProperty'][0]
);
$this->assertEquals(
'My Sub Object',
$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'
);
$this->assertNotContains(
"\$lang['en_US']['i18nTestModuleDecorator']['has_one_Page'] = 'Page';",
$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'
);
$this->assertContains(
"\$lang['en_US']['i18nTestModuleDecorator']['has_one_Page'] = 'Page';",
$otherModuleLangFileContent,
'Decorated fields are stored in the module in which the decorator is placed'
);
}

}
?>
1 change: 0 additions & 1 deletion tests/i18n/i18nTextCollectorTestMySubObject.php
Expand Up @@ -15,6 +15,5 @@ class i18nTextCollectorTestMySubObject extends i18nTextCollectorTestMyObject imp
static $singular_name = "My Sub Object";

static $plural_name = "My Sub Objects";

}
?>

0 comments on commit eb4c950

Please sign in to comment.