MINOR Merged from trunk

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.3@77008 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2009-05-17 05:38:08 +00:00 committed by Sam Minnee
parent 59c1f0668e
commit 56977a0c49
6 changed files with 52 additions and 61 deletions

View File

@ -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']}"); if(isset($_REQUEST['debug'])) Debug::message("Using record #$child->ID of type $child->class with URL {$this->urlParams['URLSegment']}");
// set language // set language
if($child->Locale) Translatable::set_reading_locale($child->Locale); if($child->Locale) Translatable::set_current_locale($child->Locale);
$controllerClass = "{$child->class}_Controller"; $controllerClass = "{$child->class}_Controller";

View File

@ -58,13 +58,13 @@
* $translatedObj = $obj->getTranslation('de_DE'); * $translatedObj = $obj->getTranslation('de_DE');
* </code> * </code>
* *
* 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). * This is *not* a recommended approach, but sometimes inavoidable (e.g. for {@link Versioned} methods).
* <code> * <code>
* $origLocale = Translatable::get_reading_locale(); * $origLocale = Translatable::get_current_locale();
* Translatable::set_reading_locale('de_DE'); * Translatable::set_current_locale('de_DE');
* $obj = Versioned::get_one_by_stage('MyObject', "ID = 99"); * $obj = Versioned::get_one_by_stage('MyObject', "ID = 99");
* Translatable::set_reading_locale($origLocale); * Translatable::set_current_locale($origLocale);
* </code> * </code>
* *
* Creating a translation: * Creating a translation:
@ -87,11 +87,11 @@
* through the {@link Versioned} extension. * through the {@link Versioned} extension.
* *
* Note: You can't get Children() for a parent page in a different language * 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.
* *
* <code> * <code>
* // wrong * // wrong
* Translatable::set_reading_lang('de_DE'); * Translatable::set_current_locale('de_DE');
* $englishParent->Children(); * $englishParent->Children();
* // right * // right
* $germanParent = $englishParent->getTranslation('de_DE'); * $germanParent = $englishParent->getTranslation('de_DE');
@ -159,13 +159,7 @@ class Translatable extends DataObjectDecorator {
* @see Director::get_site_mode() * @see Director::get_site_mode()
* @var string * @var string
*/ */
protected static $reading_locale = null; protected static $current_locale = null;
/**
* Indicates if the start language has been determined using choose_site_locale()
* @var boolean
*/
protected static $language_decided = false;
/** /**
* A cached list of existing tables * A cached list of existing tables
@ -213,25 +207,22 @@ class Translatable extends DataObjectDecorator {
* *
* @uses Director::get_site_mode() * @uses Director::get_site_mode()
* @param $langsAvailable array A numerical array of languages which are valid choices (optional) * @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()) { static function choose_site_locale($langsAvailable = array()) {
$siteMode = Director::get_site_mode(); // either 'cms' or 'site' $siteMode = Director::get_site_mode(); // either 'cms' or 'site'
if(self::$reading_locale) { if(self::$current_locale) {
self::$language_decided = true; return self::$current_locale;
return self::$reading_locale;
} }
if((isset($_GET['locale']) && !$langsAvailable) || (isset($_GET['locale']) && in_array($_GET['locale'], $langsAvailable))) { if((isset($_GET['locale']) && !$langsAvailable) || (isset($_GET['locale']) && in_array($_GET['locale'], $langsAvailable))) {
// get from GET parameter // get from GET parameter
self::set_reading_locale($_GET['locale']); self::set_current_locale($_GET['locale']);
} else { } else {
self::set_reading_locale(self::default_locale()); self::set_current_locale(self::default_locale());
} }
return self::$current_locale;
self::$language_decided = true;
return self::$reading_locale;
} }
/** /**
@ -263,11 +254,12 @@ class Translatable extends DataObjectDecorator {
/** /**
* Get the current reading language. * Get the current reading language.
* If its not chosen, call {@link choose_site_locale()}.
*
* @return string * @return string
*/ */
static function get_current_locale() { static function get_current_locale() {
if (!self::$language_decided) self::choose_site_locale(); return (self::$current_locale) ? self::$current_locale : self::choose_site_locale();
return self::$reading_locale;
} }
/** /**
@ -278,9 +270,8 @@ class Translatable extends DataObjectDecorator {
* *
* @param string $lang New reading language. * @param string $lang New reading language.
*/ */
static function set_reading_locale($locale) { static function set_current_locale($locale) {
self::$reading_locale = $locale; self::$current_locale = $locale;
self::$language_decided = true;
} }
/** /**
@ -294,9 +285,9 @@ class Translatable extends DataObjectDecorator {
*/ */
static function get_one_by_locale($class, $locale, $filter = '', $cache = false, $orderby = "") { static function get_one_by_locale($class, $locale, $filter = '', $cache = false, $orderby = "") {
$orig = Translatable::get_current_locale(); $orig = Translatable::get_current_locale();
Translatable::set_reading_locale($locale); Translatable::set_current_locale($locale);
$do = DataObject::get_one($class, $filter, $cache, $orderby); $do = DataObject::get_one($class, $filter, $cache, $orderby);
Translatable::set_reading_locale($orig); Translatable::set_current_locale($orig);
return $do; return $do;
} }
@ -315,9 +306,9 @@ class Translatable extends DataObjectDecorator {
*/ */
static function get_by_locale($class, $locale, $filter = '', $sort = '', $join = "", $limit = "", $containerClass = "DataObjectSet", $having = "") { static function get_by_locale($class, $locale, $filter = '', $sort = '', $join = "", $limit = "", $containerClass = "DataObjectSet", $having = "") {
$oldLang = self::get_current_locale(); $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); $result = DataObject::get($class, $filter, $sort, $join, $limit, $containerClass, $having);
self::set_reading_locale($oldLang); self::set_current_locale($oldLang);
return $result; 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) { 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));
} }
/** /**

View File

@ -106,7 +106,7 @@ class SearchForm extends Form {
// set language (if present) // set language (if present)
if(singleton('SiteTree')->hasExtension('Translatable') && isset($data['locale'])) { if(singleton('SiteTree')->hasExtension('Translatable') && isset($data['locale'])) {
$origLocale = Translatable::get_current_locale(); $origLocale = Translatable::get_current_locale();
Translatable::set_reading_locale($data['locale']); Translatable::set_current_locale($data['locale']);
} }
$keywords = $data['Search']; $keywords = $data['Search'];
@ -138,7 +138,7 @@ class SearchForm extends Form {
// reset locale // reset locale
if(singleton('SiteTree')->hasExtension('Translatable') && isset($data['locale'])) { if(singleton('SiteTree')->hasExtension('Translatable') && isset($data['locale'])) {
Translatable::set_reading_locale($origLocale); Translatable::set_current_locale($origLocale);
} }
return $results; return $results;

View File

@ -153,9 +153,9 @@ class MigrateTranslatableTask extends BuildTask {
// Write the new translation to the database // Write the new translation to the database
$sitelang = Translatable::get_current_locale(); $sitelang = Translatable::get_current_locale();
Translatable::set_reading_locale($newtrans->Locale); Translatable::set_current_locale($newtrans->Locale);
$newtrans->writeToStage($stage); $newtrans->writeToStage($stage);
Translatable::set_reading_locale($sitelang); Translatable::set_current_locale($sitelang);
$newtrans->addTranslationGroup($original->getTranslationGroup(), true); $newtrans->addTranslationGroup($original->getTranslationGroup(), true);

View File

@ -55,7 +55,7 @@ class TranslatableTest extends FunctionalTest {
if(!self::$origTranslatableSettings['has_extension']) Object::remove_extension('SiteTree', 'Translatable'); if(!self::$origTranslatableSettings['has_extension']) Object::remove_extension('SiteTree', 'Translatable');
Translatable::set_default_locale(self::$origTranslatableSettings['default_locale']); 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::kill_temp_db();
self::create_temp_db(); self::create_temp_db();
@ -239,7 +239,7 @@ class TranslatableTest extends FunctionalTest {
$this->assertNotContains($translatedPage->ID, $resultPagesDefaultLang->column('ID')); $this->assertNotContains($translatedPage->ID, $resultPagesDefaultLang->column('ID'));
// test in custom language // test in custom language
Translatable::set_reading_locale('de_DE'); Translatable::set_current_locale('de_DE');
$resultPagesCustomLang = DataObject::get( $resultPagesCustomLang = DataObject::get(
'Page', 'Page',
sprintf("`SiteTree`.`MenuTitle` = '%s'", 'A Testpage') 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() // casting as a workaround for types not properly set on duplicated dataobjects from createTranslation()
$this->assertContains((string)$translatedPage->ID, $resultPagesCustomLang->column('ID')); $this->assertContains((string)$translatedPage->ID, $resultPagesCustomLang->column('ID'));
Translatable::set_reading_locale('en_US'); Translatable::set_current_locale('en_US');
} }
function testDataObjectGetByIdWithReadingLanguage() { function testDataObjectGetByIdWithReadingLanguage() {
@ -270,7 +270,7 @@ class TranslatableTest extends FunctionalTest {
$translatedPage = $origPage->createTranslation('de_DE'); $translatedPage = $origPage->createTranslation('de_DE');
// running the same query twice with different // running the same query twice with different
Translatable::set_reading_locale('de_DE'); Translatable::set_current_locale('de_DE');
$compareTranslatedPage = DataObject::get_one( $compareTranslatedPage = DataObject::get_one(
'Page', 'Page',
sprintf("`SiteTree`.`Title` = '%s'", $translatedPage->Title) sprintf("`SiteTree`.`Title` = '%s'", $translatedPage->Title)
@ -283,14 +283,14 @@ class TranslatableTest extends FunctionalTest {
); );
// reset language to default // reset language to default
Translatable::set_reading_locale('en_US'); Translatable::set_current_locale('en_US');
} }
function testModifyTranslationWithDefaultReadingLang() { function testModifyTranslationWithDefaultReadingLang() {
$origPage = $this->objFromFixture('Page', 'testpage_en'); $origPage = $this->objFromFixture('Page', 'testpage_en');
$translatedPage = $origPage->createTranslation('de_DE'); $translatedPage = $origPage->createTranslation('de_DE');
Translatable::set_reading_locale('en_US'); Translatable::set_current_locale('en_US');
$translatedPage->Title = 'De Modified'; $translatedPage->Title = 'De Modified';
$translatedPage->write(); $translatedPage->write();
$savedTranslatedPage = $origPage->getTranslation('de_DE'); $savedTranslatedPage = $origPage->getTranslation('de_DE');
@ -310,7 +310,7 @@ class TranslatableTest extends FunctionalTest {
$origPage = $this->objFromFixture('Page', 'testpage_en'); $origPage = $this->objFromFixture('Page', 'testpage_en');
$translatedPage = $origPage->createTranslation('de_DE'); $translatedPage = $origPage->createTranslation('de_DE');
Translatable::set_reading_locale('en_US'); Translatable::set_current_locale('en_US');
$origPage->Title = 'En Modified'; $origPage->Title = 'En Modified';
$origPage->write(); $origPage->write();
// modifying a record in language which is not the reading language should still write the record correctly // 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->ParentID = $parentPageTranslated->ID;
$child4PageTranslated->write(); $child4PageTranslated->write();
Translatable::set_reading_locale('en_US'); Translatable::set_current_locale('en_US');
$this->assertEquals( $this->assertEquals(
$parentPage->Children()->column('ID'), $parentPage->Children()->column('ID'),
array( array(
@ -362,7 +362,7 @@ class TranslatableTest extends FunctionalTest {
"Showing Children() in default language doesnt show children in other languages" "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(); $parentPage->flushCache();
$this->assertEquals( $this->assertEquals(
$parentPageTranslated->Children()->column('ID'), $parentPageTranslated->Children()->column('ID'),
@ -371,7 +371,7 @@ class TranslatableTest extends FunctionalTest {
); );
// reset language // reset language
Translatable::set_reading_locale('en_US'); Translatable::set_current_locale('en_US');
} }
function testHierarchyLiveStageChildren() { function testHierarchyLiveStageChildren() {
@ -395,7 +395,7 @@ class TranslatableTest extends FunctionalTest {
$child5PageTranslated->ParentID = $parentPageTranslated->ID; $child5PageTranslated->ParentID = $parentPageTranslated->ID;
$child5PageTranslated->write(); $child5PageTranslated->write();
Translatable::set_reading_locale('en_US'); Translatable::set_current_locale('en_US');
$this->assertNotNull($parentPage->liveChildren()); $this->assertNotNull($parentPage->liveChildren());
$this->assertEquals( $this->assertEquals(
$parentPage->liveChildren()->column('ID'), $parentPage->liveChildren()->column('ID'),
@ -415,7 +415,7 @@ class TranslatableTest extends FunctionalTest {
"Showing stageChildren() in default language doesnt show children in other languages" "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(); $parentPage->flushCache();
$this->assertNotNull($parentPageTranslated->liveChildren()); $this->assertNotNull($parentPageTranslated->liveChildren());
$this->assertEquals( $this->assertEquals(
@ -434,7 +434,7 @@ class TranslatableTest extends FunctionalTest {
); );
// reset language // reset language
Translatable::set_reading_locale('en_US'); Translatable::set_current_locale('en_US');
} }
function testTranslatablePropertiesOnSiteTree() { function testTranslatablePropertiesOnSiteTree() {
@ -509,7 +509,7 @@ class TranslatableTest extends FunctionalTest {
$translatedPageWithoutOriginal->Locale = 'de_DE'; $translatedPageWithoutOriginal->Locale = 'de_DE';
$translatedPageWithoutOriginal->write(); $translatedPageWithoutOriginal->write();
Translatable::set_reading_locale('de_DE'); Translatable::set_current_locale('de_DE');
$this->assertEquals( $this->assertEquals(
$translatedParentPage->stageChildren()->column('ID'), $translatedParentPage->stageChildren()->column('ID'),
array( array(
@ -518,7 +518,7 @@ class TranslatableTest extends FunctionalTest {
"Children() still works on a translated page even if no translation group is set" "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() { function testCreateTranslationTranslatesUntranslatedParents() {
@ -593,7 +593,7 @@ class TranslatableTest extends FunctionalTest {
$child3PageID = $child3Page->ID; $child3PageID = $child3Page->ID;
// on original parent in default language // on original parent in default language
Translatable::set_reading_locale('en_US'); Translatable::set_current_locale('en_US');
SiteTree::flush_and_destroy_cache(); SiteTree::flush_and_destroy_cache();
$parentPage = $this->objFromFixture('Page', 'parent'); $parentPage = $this->objFromFixture('Page', 'parent');
$children = $parentPage->AllChildrenIncludingDeleted(); $children = $parentPage->AllChildrenIncludingDeleted();
@ -608,7 +608,7 @@ class TranslatableTest extends FunctionalTest {
); );
// on original parent in translation mode // on original parent in translation mode
Translatable::set_reading_locale('de_DE'); Translatable::set_current_locale('de_DE');
SiteTree::flush_and_destroy_cache(); SiteTree::flush_and_destroy_cache();
$parentPage = $this->objFromFixture('Page', 'parent'); $parentPage = $this->objFromFixture('Page', 'parent');
$this->assertEquals( $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" "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(); SiteTree::flush_and_destroy_cache();
$parentPage = $this->objFromFixture('Page', 'parent'); $parentPage = $this->objFromFixture('Page', 'parent');
$this->assertEquals( $this->assertEquals(
@ -630,7 +630,7 @@ class TranslatableTest extends FunctionalTest {
); );
// reset language // reset language
Translatable::set_reading_locale('en_US'); Translatable::set_current_locale('en_US');
} }
function testRootUrlDefaultsToTranslatedUrlSegment() { function testRootUrlDefaultsToTranslatedUrlSegment() {
@ -642,7 +642,7 @@ class TranslatableTest extends FunctionalTest {
$translationDe->publish('Stage', 'Live'); $translationDe->publish('Stage', 'Live');
// test with translatable // test with translatable
Translatable::set_reading_locale('de_DE'); Translatable::set_current_locale('de_DE');
$this->assertEquals( $this->assertEquals(
RootURLController::get_homepage_urlsegment(), RootURLController::get_homepage_urlsegment(),
'heim', 'heim',
@ -661,7 +661,7 @@ class TranslatableTest extends FunctionalTest {
// Object::add_extension('Page', 'Translatable'); // Object::add_extension('Page', 'Translatable');
// setting back to default // setting back to default
Translatable::set_reading_locale('en_US'); Translatable::set_current_locale('en_US');
} }
function testSiteTreeChangePageTypeInMaster() { function testSiteTreeChangePageTypeInMaster() {
@ -686,7 +686,7 @@ class TranslatableTest extends FunctionalTest {
); );
// re-fetch the translation with new instance // 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 = DataObject::get_by_id('RedirectorPage', $translatedPageID);
$translatedPageChanged = $origPageChanged->getTranslation('de_DE'); $translatedPageChanged = $origPageChanged->getTranslation('de_DE');
$this->assertEquals($translatedPageChanged->ClassName, 'RedirectorPage', $this->assertEquals($translatedPageChanged->ClassName, 'RedirectorPage',

View File

@ -80,7 +80,7 @@ class TranslatableSearchFormTest extends FunctionalTest {
$translatedPublishedPage->write(); $translatedPublishedPage->write();
$translatedPublishedPage->publish('Stage', 'Live'); $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 // from the holder is not present here - we set the language explicitly
// through a pseudo GET variable in getResults() // through a pseudo GET variable in getResults()