From c1c920cca0d11faa759d47c310f19e5becd70ad2 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Thu, 30 Apr 2009 07:37:09 +0000 Subject: [PATCH] ENHANCEMENT Added DBLocale class for Translatable extension API CHANGE Deprecated Translatable::get_langs_by_id() - use getTranslations() MINOR Removed Translatable::is_default_locale() - no deprecation necessary, was recently added MINOR Removed commented out methods in Translatable: augmentNumChildrenCountQuery(), augmentAllChildrenIncludingDeleted() and augmentStageChildren() MINOR Removed fieldsInExtraTables() and extendWithSuffix() git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@75742 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/model/Translatable.php | 96 ++++-------------------------- core/model/fieldtypes/DBLocale.php | 33 ++++++++++ tests/model/DBLocaleTest.php | 12 ++++ 3 files changed, 57 insertions(+), 84 deletions(-) create mode 100644 core/model/fieldtypes/DBLocale.php create mode 100644 tests/model/DBLocaleTest.php diff --git a/core/model/Translatable.php b/core/model/Translatable.php index 35575171e..b1870be2b 100755 --- a/core/model/Translatable.php +++ b/core/model/Translatable.php @@ -247,14 +247,6 @@ class Translatable extends DataObjectDecorator { self::$default_locale = $locale; } - /** - * Check whether the default and current reading language are the same. - * @return boolean Return true if both default and reading language are the same. - */ - static function is_default_locale() { - return (!self::current_locale() || self::$default_locale == self::current_locale()); - } - /** * Get the current reading language. * @return string @@ -361,7 +353,9 @@ class Translatable extends DataObjectDecorator { } /** - * Get a list of languages in which a given element has been translated + * Get a list of languages in which a given element has been translated. + * + * @deprecated 2.4 Use {@link getTranslations()} * * @param string $class Name of the class of the element * @param int $id ID of the element @@ -436,7 +430,7 @@ class Translatable extends DataObjectDecorator { if(get_class($this->owner) == ClassInfo::baseDataClass(get_class($this->owner))) { return array( "db" => array( - "Locale" => "Varchar(12)", + "Locale" => "DBLocale", //"TranslationMasterID" => "Int" // optional relation to a "translation master" ), "defaults" => array( @@ -474,7 +468,7 @@ class Translatable extends DataObjectDecorator { //&& !$query->filtersOnFK() ) { $qry = "\"Locale\" = '$lang'"; - if(Translatable::is_default_locale()) { + if(self::$default_locale == self::current_locale()) { $qry .= " OR \"Locale\" = '' "; $qry .= " OR \"Locale\" IS NULL "; } @@ -611,14 +605,6 @@ class Translatable extends DataObjectDecorator { ); } - /* - function augmentNumChildrenCountQuery(SQLQuery $query) { - if($this->isTranslation()) { - $query->where[0] = '"ParentID" = '.$this->getOriginalPage()->ID; - } - } - */ - /** * Determine if a table needs Versioned support * This is called at db/build time @@ -879,17 +865,7 @@ class Translatable extends DataObjectDecorator { $langDropdown->addExtraClass('languageDropdown'); $createButton->addExtraClass('createTranslationButton'); } - - /** - * Get a list of fields from the tables created by this extension - * - * @param string $table Name of the table - * @return array Map where the keys are db, indexes and the values are the table fields - */ - function fieldsInExtraTables($table){ - return array('db'=>null,'indexes'=>null); - } - + /** * Get the names of all translatable fields on this class * as a numeric array. @@ -910,11 +886,7 @@ class Translatable extends DataObjectDecorator { $baseClass = array_shift($tableClasses); return (!$stage || $stage == $this->defaultStage) ? $baseClass : $baseClass . "_$stage"; } - - function extendWithSuffix($table) { - return $table; - } - + /** * Gets all related translations for the current object, * excluding itself. See {@link getTranslation()} to retrieve @@ -1035,14 +1007,6 @@ class Translatable extends DataObjectDecorator { function hasTranslation($locale) { return (array_search($locale, $this->getTranslatedLangs()) !== false); } - - /* - function augmentStageChildren(DataObjectSet $children, $showall = false) { - if($this->isTranslation()) { - $children->merge($this->getOriginalPage()->stageChildren($showall)); - } - } - */ function AllChildrenIncludingDeleted($context = null) { $children = $this->owner->doAllChildrenIncludingDeleted($context); @@ -1072,43 +1036,6 @@ class Translatable extends DataObjectDecorator { } } - /** - * If called with default language, doesn't affect the results. - * Otherwise (called in translation mode) the method tries to find translations - * for each page in its original language and replace the original. - * The result will contain a mixture of translated and untranslated pages. - * - * Caution: We also create a method AllChildrenIncludingDeleted() dynamically in the class constructor. - * - * @param DataObjectSet $untranslatedChildren - * @param Object $context - */ - /* - function augmentAllChildrenIncludingDeleted(DataObjectSet $children, $context) { - $find = array(); - $replace = array(); - - if($context && $context->Locale && $context->Locale != Translatable::default_locale()) { - - if($children) { - foreach($children as $child) { - if($child->hasTranslation($context->Locale)) { - $trans = $child->getTranslation($context->Locale); - if($trans) { - $find[] = $child; - $replace[] = $trans; - } - } - } - foreach($find as $i => $found) { - $children->replace($found, $replace[$i]); - } - } - - } - } - */ - /** * Get a list of languages with at least one element translated in (including the default language) * @@ -1123,8 +1050,9 @@ class Translatable extends DataObjectDecorator { $returnMap = array(); $allCodes = array_merge(i18n::$all_locales, i18n::$common_locales); foreach ($langlist as $langCode) { - if($langCode) - $returnMap[$langCode] = (is_array($allCodes[$langCode]) ? $allCodes[$langCode][0] : $allCodes[$langCode]); + if($langCode && isset($allCodes[$langCode])) { + $returnMap[$langCode] = (is_array($allCodes[$langCode])) ? $allCodes[$langCode][0] : $allCodes[$langCode]; + } } return $returnMap; } @@ -1162,10 +1090,10 @@ class Translatable extends DataObjectDecorator { } /** - * @deprecated 2.4 Use is_default_locale() + * @deprecated 2.4 Use custom check: self::$default_locale == self::current_locale() */ static function is_default_lang() { - return self::is_default_locale(); + return (self::$default_locale == self::current_locale()); } /** diff --git a/core/model/fieldtypes/DBLocale.php b/core/model/fieldtypes/DBLocale.php new file mode 100644 index 000000000..27740ab7b --- /dev/null +++ b/core/model/fieldtypes/DBLocale.php @@ -0,0 +1,33 @@ +getShortName(); + } + + function RFC1766() { + return i18n::convert_rfc1766($this->value); + } + + function getShortName() { + $common_names = i18n::get_common_locales(); + return (isset($common_names[$this->value])) ? $common_names[$this->value] : false; + } + + function getLongName() { + return i18n::get_locale_name($this->value); + } +} +?> \ No newline at end of file diff --git a/tests/model/DBLocaleTest.php b/tests/model/DBLocaleTest.php new file mode 100644 index 000000000..3b6d88137 --- /dev/null +++ b/tests/model/DBLocaleTest.php @@ -0,0 +1,12 @@ +assertEquals($l->Nice(), 'German (Germany)'); + } +} +?> \ No newline at end of file