diff --git a/core/i18nEntityProvider.php b/core/i18nEntityProvider.php new file mode 100644 index 000000000..836821d1f --- /dev/null +++ b/core/i18nEntityProvider.php @@ -0,0 +1,52 @@ + + * class MyTestClass implements i18nEntityProvider { + * function provideI18nEntities() { + * $entities = array(); + * foreach($this->stat('my_static_array) as $key => $value) { + * $entities["MyTestClass.my_static_array_{$key}"] = array( + * $value, + * PR_MEDIUM, + * 'My context description' + * ); + * } + * return $entities; + * } + * + * static function my_static_array() { + * $t_my_static_array = array(); + * foreach(self::$my_static_array as $k => $v) { + * $t_my_static_array[$k] = _t("MyTestClass.my_static_array_{$key}", $v); + * } + * return $t_my_static_array; + * } + * } + * + * + * Example usage in {@link DataObject->provideI18nEntities()}. + * + * @return array All entites in an associative array, with + * entity name as the key, and a numerical array of pseudo-arguments + * for _t() as a value. + */ + function provideI18nEntities(); +} +?> \ No newline at end of file diff --git a/core/i18nTextCollector.php b/core/i18nTextCollector.php index a6c26665c..4587a21f9 100644 --- a/core/i18nTextCollector.php +++ b/core/i18nTextCollector.php @@ -1,9 +1,29 @@ * @author Ingo Schommer * @package sapphire - * @subpackage misc + * @subpackage i18n + * @uses i18nEntityProvider + * @uses i18n */ class i18nTextCollector extends Object { @@ -321,7 +341,9 @@ class i18nTextCollector extends Object { $classes = ClassInfo::classes_for_file($filePath); if($classes) foreach($classes as $class) { - if(class_exists($class) && method_exists($class, 'provideI18nEntities')) { + // Not all classes can be instanciated without mandatory arguments, + // so entity collection doesn't work for all SilverStripe classes currently + if(class_exists($class) && in_array('i18nEntityProvider', class_implements(new $class))) { $obj = singleton($class); $entitiesArr = array_merge($entitiesArr,(array)$obj->provideI18nEntities()); } diff --git a/core/model/DataObject.php b/core/model/DataObject.php index 7d1595ea8..e3e528804 100644 --- a/core/model/DataObject.php +++ b/core/model/DataObject.php @@ -53,7 +53,7 @@ * @package sapphire * @subpackage model */ -class DataObject extends ViewableData implements DataObjectInterface { +class DataObject extends ViewableData implements DataObjectInterface,i18nEntityProvider { /** * Data stored in this objects database record. An array indexed * by fieldname. diff --git a/tests/i18nTest.php b/tests/i18nTest.php index 8b179f0eb..1f7f747d6 100644 --- a/tests/i18nTest.php +++ b/tests/i18nTest.php @@ -14,37 +14,38 @@ class i18nTest extends SapphireTest { global $lang; $oldLocale = i18n::get_locale(); i18n::set_locale('de_DE'); - $obj = new i18nTest_Object(); + $obj = new i18nTest_DataObject(); - $lang['en_US']['i18nTest_Object']['db_MyProperty'] = 'MyProperty'; - $lang['de_DE']['i18nTest_Object']['db_MyProperty'] = 'Mein Attribut'; + $lang['en_US']['i18nTest_DataObject']['db_MyProperty'] = 'MyProperty'; + $lang['de_DE']['i18nTest_DataObject']['db_MyProperty'] = 'Mein Attribut'; + $this->assertEquals( $obj->fieldLabel('MyProperty'), 'Mein Attribut' ); - $lang['en_US']['i18nTest_Object']['has_one_HasOneRelation'] = 'HasOneRelation'; - $lang['de_DE']['i18nTest_Object']['has_one_HasOneRelation'] = 'Eins zu eins'; + $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_Object']['has_many_HasManyRelation'] = 'HasManyRelation'; - $lang['de_DE']['i18nTest_Object']['has_many_HasManyRelation'] = 'Viel 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_Object']['many_many_ManyManyRelation'] = 'ManyManyRelation'; - $lang['de_DE']['i18nTest_Object']['many_many_ManyManyRelation'] = 'Viel zu viel'; + $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_Object']['db_MyUntranslatedProperty'] = 'MyUntranslatedProperty'; + $lang['en_US']['i18nTest_DataObject']['db_MyUntranslatedProperty'] = 'MyUntranslatedProperty'; $this->assertEquals( $obj->fieldLabel('MyUntranslatedProperty'), 'My Untranslated Property' @@ -53,9 +54,40 @@ class i18nTest extends SapphireTest { i18n::set_locale($oldLocale); } + function testProvideI18nEntities() { + global $lang; + $oldLocale = i18n::get_locale(); + $lang['en_US']['i18nTest_Object']['my_translatable_property'] = 'Untranslated'; + $lang['de_DE']['i18nTest_Object']['my_translatable_property'] = 'Übersetzt'; + + i18n::set_locale('en_US'); + $this->assertEquals( + i18nTest_Object::$my_translatable_property, + 'Untranslated' + ); + $this->assertEquals( + i18nTest_Object::my_translatable_property(), + 'Untranslated' + ); + + i18n::set_locale('en_US'); + $this->assertEquals( + i18nTest_Object::my_translatable_property(), + 'Untranslated', + 'Getter returns original static value when called in default locale' + ); + + i18n::set_locale('de_DE'); + $this->assertEquals( + i18nTest_Object::my_translatable_property(), + 'Übersetzt', + 'Getter returns translated value when called in another locale' + ); + } + } -class i18nTest_Object extends DataObject implements TestOnly { +class i18nTest_DataObject extends DataObject implements TestOnly { static $db = array( 'MyProperty' => 'Varchar', @@ -75,4 +107,20 @@ class i18nTest_Object extends DataObject implements TestOnly { ); } + +class i18nTest_Object extends Object implements TestOnly, i18nEntityProvider { + static $my_translatable_property = "Untranslated"; + + static function my_translatable_property() { + return _t("i18nTest_Object.my_translatable_property", self::$my_translatable_property); + } + + function provideI18nEntities() { + return array( + "i18nTest_Object.my_translatable_property" => array( + self::$my_translatable_property + ) + ); + } +} ?>