diff --git a/core/control/RootURLController.php b/core/control/RootURLController.php
index b141b98b7..c0ed8a277 100755
--- a/core/control/RootURLController.php
+++ b/core/control/RootURLController.php
@@ -50,6 +50,7 @@ class RootURLController extends Controller {
/**
* Return the URL segment for the current HTTP_HOST value
*
+ * @param string $locale
* @return string
*/
static function get_homepage_urlsegment() {
@@ -65,8 +66,8 @@ class RootURLController extends Controller {
$homePageOBJ = null;
}
- if(singleton('SiteTree')->hasExtension('Translatable')) {
- $urlSegment = Translatable::get_homepage_urlsegment_by_language(Translatable::current_locale());
+ if(singleton('SiteTree')->hasExtension('Translatable') && !$homePageOBJ) {
+ $urlSegment = Translatable::get_homepage_urlsegment_by_locale(Translatable::current_locale());
} elseif($homePageOBJ) {
$urlSegment = $homePageOBJ->URLSegment;
}
@@ -76,14 +77,34 @@ class RootURLController extends Controller {
/**
* Returns true if we're currently on the root page and should be redirecting to the root
- * Doesn't take into account actions, post vars, or get vars
+ * Doesn't take into account actions, post vars, or get vars.
+ *
+ * @param SiteTree $currentPage
+ * @return boolean
*/
static function should_be_on_root(SiteTree $currentPage) {
- if(!self::$is_at_root) return self::get_homepage_urlsegment() == $currentPage->URLSegment;
- else return false;
+ if(self::$is_at_root) return false;
+
+ $matchesHomepageSegment = (self::get_homepage_urlsegment() == $currentPage->URLSegment);
+ // Don't redirect translated homepage segments,
+ // as the redirection target '/' will show the default locale
+ // instead of the translation.
+ $isTranslatedHomepage = (
+ singleton('SiteTree')->hasExtension('Translatable')
+ && $currentPage->Locale
+ && $currentPage->Locale != Translatable::default_locale()
+ );
+ if($matchesHomepageSegment && !$isTranslatedHomepage) return true;
+
+ return false;
}
/**
+ * Returns the (untranslated) hardcoded URL segment that will
+ * show when the website is accessed without a URL segment (http://mysite.com/).
+ * It is also the base for any redirections to '/' for the homepage,
+ * see {@link should_be_on_root()}.
+ *
* @return string
*/
static function get_default_homepage_urlsegment() {
diff --git a/core/model/Translatable.php b/core/model/Translatable.php
index 61eea69fa..422ae4279 100755
--- a/core/model/Translatable.php
+++ b/core/model/Translatable.php
@@ -10,20 +10,23 @@
*
Configuration
*
* Enabling Translatable in the $extension array of a DataObject
- *
+ *
* class MyClass extends DataObject {
* static $extensions = array(
* "Translatable"
* );
* }
- *
+ *
*
* Enabling Translatable through {@link Object::add_extension()} in your _config.php:
*
+ * Translatable::set_default_locale('en_US');
* Object::add_extension('MyClass', 'Translatable');
*
*
* Make sure to rebuild the database through /dev/build after enabling translatable.
+ * Use the correct {@link set_default_locale()} before building the database
+ * for the first time, as this locale will be written on all new records.
*
* Usage
*
@@ -210,7 +213,9 @@ class Translatable extends DataObjectDecorator {
}
/**
- * Set default language.
+ * Set default language. Please set this value *before* creating
+ * any database records (like pages), as this locale will be attached
+ * to all new records.
*
* @param $locale String
*/
@@ -663,9 +668,9 @@ class Translatable extends DataObjectDecorator {
*/
function alternateGetByUrl($urlSegment, $extraFilter, $cache = null, $orderby = null) {
$SQL_URLSegment = Convert::raw2sql($urlSegment);
- Translatable::disable();
- $record = DataObject::get_one('SiteTree', "\"URLSegment\" = '{$SQL_URLSegment}'");
- Translatable::enable();
+ self::$enable_lang_filter = false;
+ $record = DataObject::get_one('SiteTree', "\"URLSegment\" = '{$SQL_URLSegment}'", false);
+ self::$enable_lang_filter = true;
return $record;
}
@@ -1023,14 +1028,14 @@ class Translatable extends DataObjectDecorator {
* @param string $locale
* @return string|boolean URLSegment (e.g. "home")
*/
- static function get_homepage_urlsegment_by_language($locale) {
+ static function get_homepage_urlsegment_by_locale($locale) {
$origHomepageObj = Translatable::get_one_by_locale(
'SiteTree',
Translatable::default_locale(),
sprintf('"URLSegment" = \'%s\'', RootUrlController::get_default_homepage_urlsegment())
);
if($origHomepageObj) {
- $translatedHomepageObj = $origHomepageObj->getTranslation(Translatable::current_locale());
+ $translatedHomepageObj = $origHomepageObj->getTranslation($locale);
if($translatedHomepageObj) {
return $translatedHomepageObj->URLSegment;
}
@@ -1039,6 +1044,13 @@ class Translatable extends DataObjectDecorator {
return null;
}
+ /**
+ * @deprecated 2.4 Use get_homepage_urlsegment_by_locale()
+ */
+ static function get_homepage_urlsegment_by_language($locale) {
+ return self::get_homepage_urlsegment_by_locale($locale);
+ }
+
/**
* @deprecated 2.4 Use is_default_locale()
*/
diff --git a/tests/control/RootURLControllerTest.php b/tests/control/RootURLControllerTest.php
index 31a273b83..1b1885cdf 100644
--- a/tests/control/RootURLControllerTest.php
+++ b/tests/control/RootURLControllerTest.php
@@ -37,7 +37,11 @@ class RootURLControllerTest extends SapphireTest {
foreach($tests as $domain => $urlSegment) {
$_SERVER['HTTP_HOST'] = $domain;
- $this->assertEquals($urlSegment, RootURLController::get_homepage_urlsegment(), "Testing $domain matches $urlSegment");
+ $this->assertEquals(
+ $urlSegment,
+ RootURLController::get_homepage_urlsegment(Translatable::default_locale()),
+ "Testing $domain matches $urlSegment"
+ );
}
$_SERVER['HTTP_HOST'] = $originalHost;
diff --git a/tests/model/TranslatableTest.php b/tests/model/TranslatableTest.php
index fcad087b6..894db1c17 100644
--- a/tests/model/TranslatableTest.php
+++ b/tests/model/TranslatableTest.php
@@ -622,8 +622,6 @@ class TranslatableTest extends FunctionalTest {
}
function testRootUrlDefaultsToTranslatedUrlSegment() {
- $_originalHost = $_SERVER['HTTP_HOST'];
-
$origPage = $this->objFromFixture('Page', 'homepage_en');
$origPage->publish('Stage', 'Live');
$translationDe = $origPage->createTranslation('de_DE');
@@ -631,9 +629,8 @@ class TranslatableTest extends FunctionalTest {
$translationDe->write();
$translationDe->publish('Stage', 'Live');
- // test with translatable enabled
- $_SERVER['HTTP_HOST'] = '/?locale=de';
- Translatable::set_reading_locale('de_DE');
+ // test with translatable
+ Translatable::set_reading_locale('de_DE');
$this->assertEquals(
RootURLController::get_homepage_urlsegment(),
'heim',
@@ -653,7 +650,6 @@ class TranslatableTest extends FunctionalTest {
// setting back to default
Translatable::set_reading_locale('en_US');
- $_SERVER['HTTP_HOST'] = $_originalHost;
}
function testSiteTreeChangePageTypeInMaster() {