From 56977a0c4979418ca2d88dc2337a5027f5b98287 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Sun, 17 May 2009 05:38:08 +0000 Subject: [PATCH] MINOR Merged from trunk git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.3@77008 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/control/ModelAsController.php | 2 +- core/model/Translatable.php | 57 +++++++++------------ search/SearchForm.php | 4 +- tasks/MigrateTranslatableTask.php | 4 +- tests/model/TranslatableTest.php | 44 ++++++++-------- tests/search/TranslatableSearchFormTest.php | 2 +- 6 files changed, 52 insertions(+), 61 deletions(-) diff --git a/core/control/ModelAsController.php b/core/control/ModelAsController.php index de51f8849..6ae617d7b 100644 --- a/core/control/ModelAsController.php +++ b/core/control/ModelAsController.php @@ -66,7 +66,7 @@ class ModelAsController extends Controller implements NestedController { if(isset($_REQUEST['debug'])) Debug::message("Using record #$child->ID of type $child->class with URL {$this->urlParams['URLSegment']}"); // set language - if($child->Locale) Translatable::set_reading_locale($child->Locale); + if($child->Locale) Translatable::set_current_locale($child->Locale); $controllerClass = "{$child->class}_Controller"; diff --git a/core/model/Translatable.php b/core/model/Translatable.php index bb08910fa..42bce1b91 100755 --- a/core/model/Translatable.php +++ b/core/model/Translatable.php @@ -58,13 +58,13 @@ * $translatedObj = $obj->getTranslation('de_DE'); * * - * Getting translations through {@link Translatable::set_reading_locale()}. + * Getting translations through {@link Translatable::set_current_locale()}. * This is *not* a recommended approach, but sometimes inavoidable (e.g. for {@link Versioned} methods). * - * $origLocale = Translatable::get_reading_locale(); - * Translatable::set_reading_locale('de_DE'); + * $origLocale = Translatable::get_current_locale(); + * Translatable::set_current_locale('de_DE'); * $obj = Versioned::get_one_by_stage('MyObject', "ID = 99"); - * Translatable::set_reading_locale($origLocale); + * Translatable::set_current_locale($origLocale); * * * Creating a translation: @@ -87,11 +87,11 @@ * through the {@link Versioned} extension. * * Note: You can't get Children() for a parent page in a different language - * through set_reading_locale(). Get the translated parent first. + * through set_current_locale(). Get the translated parent first. * * * // wrong - * Translatable::set_reading_lang('de_DE'); + * Translatable::set_current_locale('de_DE'); * $englishParent->Children(); * // right * $germanParent = $englishParent->getTranslation('de_DE'); @@ -159,13 +159,7 @@ class Translatable extends DataObjectDecorator { * @see Director::get_site_mode() * @var string */ - protected static $reading_locale = null; - - /** - * Indicates if the start language has been determined using choose_site_locale() - * @var boolean - */ - protected static $language_decided = false; + protected static $current_locale = null; /** * A cached list of existing tables @@ -213,25 +207,22 @@ class Translatable extends DataObjectDecorator { * * @uses Director::get_site_mode() * @param $langsAvailable array A numerical array of languages which are valid choices (optional) - * @return string Selected language (also saved in $reading_locale). + * @return string Selected language (also saved in $current_locale). */ static function choose_site_locale($langsAvailable = array()) { $siteMode = Director::get_site_mode(); // either 'cms' or 'site' - if(self::$reading_locale) { - self::$language_decided = true; - return self::$reading_locale; + if(self::$current_locale) { + return self::$current_locale; } if((isset($_GET['locale']) && !$langsAvailable) || (isset($_GET['locale']) && in_array($_GET['locale'], $langsAvailable))) { // get from GET parameter - self::set_reading_locale($_GET['locale']); + self::set_current_locale($_GET['locale']); } else { - self::set_reading_locale(self::default_locale()); + self::set_current_locale(self::default_locale()); } - - self::$language_decided = true; - return self::$reading_locale; + return self::$current_locale; } /** @@ -263,11 +254,12 @@ class Translatable extends DataObjectDecorator { /** * Get the current reading language. + * If its not chosen, call {@link choose_site_locale()}. + * * @return string */ static function get_current_locale() { - if (!self::$language_decided) self::choose_site_locale(); - return self::$reading_locale; + return (self::$current_locale) ? self::$current_locale : self::choose_site_locale(); } /** @@ -278,9 +270,8 @@ class Translatable extends DataObjectDecorator { * * @param string $lang New reading language. */ - static function set_reading_locale($locale) { - self::$reading_locale = $locale; - self::$language_decided = true; + static function set_current_locale($locale) { + self::$current_locale = $locale; } /** @@ -294,9 +285,9 @@ class Translatable extends DataObjectDecorator { */ static function get_one_by_locale($class, $locale, $filter = '', $cache = false, $orderby = "") { $orig = Translatable::get_current_locale(); - Translatable::set_reading_locale($locale); + Translatable::set_current_locale($locale); $do = DataObject::get_one($class, $filter, $cache, $orderby); - Translatable::set_reading_locale($orig); + Translatable::set_current_locale($orig); return $do; } @@ -315,9 +306,9 @@ class Translatable extends DataObjectDecorator { */ static function get_by_locale($class, $locale, $filter = '', $sort = '', $join = "", $limit = "", $containerClass = "DataObjectSet", $having = "") { $oldLang = self::get_current_locale(); - self::set_reading_locale($locale); + self::set_current_locale($locale); $result = DataObject::get($class, $filter, $sort, $join, $limit, $containerClass, $having); - self::set_reading_locale($oldLang); + self::set_current_locale($oldLang); return $result; } @@ -1200,10 +1191,10 @@ class Translatable extends DataObjectDecorator { } /** - * @deprecated 2.4 Use set_reading_locale() + * @deprecated 2.4 Use set_current_locale() */ static function set_reading_lang($lang) { - self::set_reading_locale(i18n::get_locale_from_lang($lang)); + self::set_current_locale(i18n::get_locale_from_lang($lang)); } /** diff --git a/search/SearchForm.php b/search/SearchForm.php index 9b3f9309a..e1719b04c 100755 --- a/search/SearchForm.php +++ b/search/SearchForm.php @@ -106,7 +106,7 @@ class SearchForm extends Form { // set language (if present) if(singleton('SiteTree')->hasExtension('Translatable') && isset($data['locale'])) { $origLocale = Translatable::get_current_locale(); - Translatable::set_reading_locale($data['locale']); + Translatable::set_current_locale($data['locale']); } $keywords = $data['Search']; @@ -138,7 +138,7 @@ class SearchForm extends Form { // reset locale if(singleton('SiteTree')->hasExtension('Translatable') && isset($data['locale'])) { - Translatable::set_reading_locale($origLocale); + Translatable::set_current_locale($origLocale); } return $results; diff --git a/tasks/MigrateTranslatableTask.php b/tasks/MigrateTranslatableTask.php index 33463e3f3..e86b9afec 100644 --- a/tasks/MigrateTranslatableTask.php +++ b/tasks/MigrateTranslatableTask.php @@ -153,9 +153,9 @@ class MigrateTranslatableTask extends BuildTask { // Write the new translation to the database $sitelang = Translatable::get_current_locale(); - Translatable::set_reading_locale($newtrans->Locale); + Translatable::set_current_locale($newtrans->Locale); $newtrans->writeToStage($stage); - Translatable::set_reading_locale($sitelang); + Translatable::set_current_locale($sitelang); $newtrans->addTranslationGroup($original->getTranslationGroup(), true); diff --git a/tests/model/TranslatableTest.php b/tests/model/TranslatableTest.php index 41a950273..77fd39dbc 100644 --- a/tests/model/TranslatableTest.php +++ b/tests/model/TranslatableTest.php @@ -55,7 +55,7 @@ class TranslatableTest extends FunctionalTest { if(!self::$origTranslatableSettings['has_extension']) Object::remove_extension('SiteTree', 'Translatable'); Translatable::set_default_locale(self::$origTranslatableSettings['default_locale']); - Translatable::set_reading_locale(self::$origTranslatableSettings['default_locale']); + Translatable::set_current_locale(self::$origTranslatableSettings['default_locale']); self::kill_temp_db(); self::create_temp_db(); @@ -239,7 +239,7 @@ class TranslatableTest extends FunctionalTest { $this->assertNotContains($translatedPage->ID, $resultPagesDefaultLang->column('ID')); // test in custom language - Translatable::set_reading_locale('de_DE'); + Translatable::set_current_locale('de_DE'); $resultPagesCustomLang = DataObject::get( 'Page', sprintf("`SiteTree`.`MenuTitle` = '%s'", 'A Testpage') @@ -250,7 +250,7 @@ class TranslatableTest extends FunctionalTest { // casting as a workaround for types not properly set on duplicated dataobjects from createTranslation() $this->assertContains((string)$translatedPage->ID, $resultPagesCustomLang->column('ID')); - Translatable::set_reading_locale('en_US'); + Translatable::set_current_locale('en_US'); } function testDataObjectGetByIdWithReadingLanguage() { @@ -270,7 +270,7 @@ class TranslatableTest extends FunctionalTest { $translatedPage = $origPage->createTranslation('de_DE'); // running the same query twice with different - Translatable::set_reading_locale('de_DE'); + Translatable::set_current_locale('de_DE'); $compareTranslatedPage = DataObject::get_one( 'Page', sprintf("`SiteTree`.`Title` = '%s'", $translatedPage->Title) @@ -283,14 +283,14 @@ class TranslatableTest extends FunctionalTest { ); // reset language to default - Translatable::set_reading_locale('en_US'); + Translatable::set_current_locale('en_US'); } function testModifyTranslationWithDefaultReadingLang() { $origPage = $this->objFromFixture('Page', 'testpage_en'); $translatedPage = $origPage->createTranslation('de_DE'); - Translatable::set_reading_locale('en_US'); + Translatable::set_current_locale('en_US'); $translatedPage->Title = 'De Modified'; $translatedPage->write(); $savedTranslatedPage = $origPage->getTranslation('de_DE'); @@ -310,7 +310,7 @@ class TranslatableTest extends FunctionalTest { $origPage = $this->objFromFixture('Page', 'testpage_en'); $translatedPage = $origPage->createTranslation('de_DE'); - Translatable::set_reading_locale('en_US'); + Translatable::set_current_locale('en_US'); $origPage->Title = 'En Modified'; $origPage->write(); // modifying a record in language which is not the reading language should still write the record correctly @@ -351,7 +351,7 @@ class TranslatableTest extends FunctionalTest { $child4PageTranslated->ParentID = $parentPageTranslated->ID; $child4PageTranslated->write(); - Translatable::set_reading_locale('en_US'); + Translatable::set_current_locale('en_US'); $this->assertEquals( $parentPage->Children()->column('ID'), array( @@ -362,7 +362,7 @@ class TranslatableTest extends FunctionalTest { "Showing Children() in default language doesnt show children in other languages" ); - Translatable::set_reading_locale('de_DE'); + Translatable::set_current_locale('de_DE'); $parentPage->flushCache(); $this->assertEquals( $parentPageTranslated->Children()->column('ID'), @@ -371,7 +371,7 @@ class TranslatableTest extends FunctionalTest { ); // reset language - Translatable::set_reading_locale('en_US'); + Translatable::set_current_locale('en_US'); } function testHierarchyLiveStageChildren() { @@ -395,7 +395,7 @@ class TranslatableTest extends FunctionalTest { $child5PageTranslated->ParentID = $parentPageTranslated->ID; $child5PageTranslated->write(); - Translatable::set_reading_locale('en_US'); + Translatable::set_current_locale('en_US'); $this->assertNotNull($parentPage->liveChildren()); $this->assertEquals( $parentPage->liveChildren()->column('ID'), @@ -415,7 +415,7 @@ class TranslatableTest extends FunctionalTest { "Showing stageChildren() in default language doesnt show children in other languages" ); - Translatable::set_reading_locale('de_DE'); + Translatable::set_current_locale('de_DE'); $parentPage->flushCache(); $this->assertNotNull($parentPageTranslated->liveChildren()); $this->assertEquals( @@ -434,7 +434,7 @@ class TranslatableTest extends FunctionalTest { ); // reset language - Translatable::set_reading_locale('en_US'); + Translatable::set_current_locale('en_US'); } function testTranslatablePropertiesOnSiteTree() { @@ -509,7 +509,7 @@ class TranslatableTest extends FunctionalTest { $translatedPageWithoutOriginal->Locale = 'de_DE'; $translatedPageWithoutOriginal->write(); - Translatable::set_reading_locale('de_DE'); + Translatable::set_current_locale('de_DE'); $this->assertEquals( $translatedParentPage->stageChildren()->column('ID'), array( @@ -518,7 +518,7 @@ class TranslatableTest extends FunctionalTest { "Children() still works on a translated page even if no translation group is set" ); - Translatable::set_reading_locale('en_US'); + Translatable::set_current_locale('en_US'); } function testCreateTranslationTranslatesUntranslatedParents() { @@ -593,7 +593,7 @@ class TranslatableTest extends FunctionalTest { $child3PageID = $child3Page->ID; // on original parent in default language - Translatable::set_reading_locale('en_US'); + Translatable::set_current_locale('en_US'); SiteTree::flush_and_destroy_cache(); $parentPage = $this->objFromFixture('Page', 'parent'); $children = $parentPage->AllChildrenIncludingDeleted(); @@ -608,7 +608,7 @@ class TranslatableTest extends FunctionalTest { ); // on original parent in translation mode - Translatable::set_reading_locale('de_DE'); + Translatable::set_current_locale('de_DE'); SiteTree::flush_and_destroy_cache(); $parentPage = $this->objFromFixture('Page', 'parent'); $this->assertEquals( @@ -620,7 +620,7 @@ class TranslatableTest extends FunctionalTest { "Showing AllChildrenIncludingDeleted() in translation mode with parent page in translated language shows children in translated language" ); - Translatable::set_reading_locale('de_DE'); + Translatable::set_current_locale('de_DE'); SiteTree::flush_and_destroy_cache(); $parentPage = $this->objFromFixture('Page', 'parent'); $this->assertEquals( @@ -630,7 +630,7 @@ class TranslatableTest extends FunctionalTest { ); // reset language - Translatable::set_reading_locale('en_US'); + Translatable::set_current_locale('en_US'); } function testRootUrlDefaultsToTranslatedUrlSegment() { @@ -642,7 +642,7 @@ class TranslatableTest extends FunctionalTest { $translationDe->publish('Stage', 'Live'); // test with translatable - Translatable::set_reading_locale('de_DE'); + Translatable::set_current_locale('de_DE'); $this->assertEquals( RootURLController::get_homepage_urlsegment(), 'heim', @@ -661,7 +661,7 @@ class TranslatableTest extends FunctionalTest { // Object::add_extension('Page', 'Translatable'); // setting back to default - Translatable::set_reading_locale('en_US'); + Translatable::set_current_locale('en_US'); } function testSiteTreeChangePageTypeInMaster() { @@ -686,7 +686,7 @@ class TranslatableTest extends FunctionalTest { ); // re-fetch the translation with new instance - Translatable::set_reading_locale('de_DE'); + Translatable::set_current_locale('de_DE'); $translatedPageChanged = DataObject::get_by_id('RedirectorPage', $translatedPageID); $translatedPageChanged = $origPageChanged->getTranslation('de_DE'); $this->assertEquals($translatedPageChanged->ClassName, 'RedirectorPage', diff --git a/tests/search/TranslatableSearchFormTest.php b/tests/search/TranslatableSearchFormTest.php index 3aec62908..0894535d8 100644 --- a/tests/search/TranslatableSearchFormTest.php +++ b/tests/search/TranslatableSearchFormTest.php @@ -80,7 +80,7 @@ class TranslatableSearchFormTest extends FunctionalTest { $translatedPublishedPage->write(); $translatedPublishedPage->publish('Stage', 'Live'); - // Translatable::set_reading_locale() can't be used because the context + // Translatable::set_current_locale() can't be used because the context // from the holder is not present here - we set the language explicitly // through a pseudo GET variable in getResults()