function testGetOneByLocale() { Translatable::disable_locale_filter(); $this->assertEquals(0, TranslatableTest_OneByLocaleDataObject::get()->count(), 'should not be any test objects yet'); Translatable::enable_locale_filter(); $obj = new TranslatableTest_OneByLocaleDataObject(); $obj->TranslatableProperty = 'test - en'; $obj->write(); Translatable::disable_locale_filter(); $this->assertEquals(1, TranslatableTest_OneByLocaleDataObject::get()->count(), 'should not be any test objects yet'); Translatable::enable_locale_filter(); $found = Translatable::get_one_by_locale('TranslatableTest_OneByLocaleDataObject', $obj->Locale); $this->assertNotNull($found, 'should have found one for ' . $obj->Locale); $this->assertEquals($obj->ID, $found->ID); $translated = $obj->createTranslation('de_DE'); $translated->write(); Translatable::disable_locale_filter(); $this->assertEquals(2, TranslatableTest_OneByLocaleDataObject::get()->count(), 'should not be any test objects yet'); Translatable::enable_locale_filter(); $found = Translatable::get_one_by_locale('TranslatableTest_OneByLocaleDataObject', $translated->Locale); $this->assertNotNull($found, 'should have found one for ' . $translated->Locale); $this->assertEquals($translated->ID, $found->ID); // test again to make sure that get_one_by_locale works when locale filter disabled Translatable::disable_locale_filter(); $found = Translatable::get_one_by_locale('TranslatableTest_OneByLocaleDataObject', $translated->Locale); $this->assertEquals($translated->ID, $found->ID); Translatable::enable_locale_filter(); }
/** * Returns a DataList containing Pages. * The provided links point to their translated pages. * You can use it in templates like this: * * <% loop LanguageChooser %> * $Title, $Current, and any other vars in your page instance * <% end_loop %> * * @return DataList */ public function LanguageChooser() { if (!Controller::curr()) { return; } if ($langs = Translatable::get_allowed_locales()) { $data = ArrayList::create(); foreach ($langs as $key => $code) { if ($code == Translatable::get_current_locale()) { $this->owner->Current = 'current'; $data->push($this->owner); } else { $translation = $this->owner->getTranslation($code); if ($translation) { $data->push($translation); } else { $page = Translatable::get_one_by_locale("SiteTree", $code, "URLSegment LIKE 'home%'"); if ($page) { $data->push($page); } } } } return $data; } return false; }
/** * PageByDefaultLocale * gets a page in the default locale * * @param string $pageURL url of a page in the default locale * @return DataObject requested page in the current locale, null if none exists. */ function PageByDefaultLocale($pageURL) { $defLoc = Translatable::default_locale(); if ($pg = Translatable::get_one_by_locale('Page', $defLoc, "URLSegment = '{$pageURL}'")) { return $pg; } return null; }
/** * Gets a URLSegment value for a homepage in another language. * The value is inferred by finding the homepage in default language * (as identified by RootURLController::$default_homepage_urlsegment). * Returns NULL if no translated page can be found. * * @param string $locale * @return string|boolean URLSegment (e.g. "home") */ 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($locale); if ($translatedHomepageObj) { return $translatedHomepageObj->URLSegment; } } return null; }
/** * Get the current sites SiteConfig, and creates a new one * through {@link make_site_config()} if none is found. * * @param string $locale * @return SiteConfig */ static function current_site_config($locale = null) { if (Object::has_extension('SiteConfig', "Translatable")) { $locale = isset($locale) ? $locale : Translatable::get_current_locale(); $siteConfig = Translatable::get_one_by_locale('SiteConfig', $locale); } else { $siteConfig = DataObject::get_one('SiteConfig'); } if (!$siteConfig) { $siteConfig = self::make_site_config($locale); } return $siteConfig; }