Not to be confused with the temporary locale assigned with {@see \Fluent::with_locale} .
public static set_persist_locale ( string $locale, string $key = null ) | ||
$locale | string | Locale to assign |
$key | string | ID to set the locale against. Will automatically detect if omitted. Either Fluent:::config()->persist_id or Fluent::::config()->persist_id_cms. |
public function handleRequest(SS_HTTPRequest $request, DataModel $model = null) { self::$is_at_root = true; $this->setDataModel($model); $this->pushCurrent(); $this->init(); $this->setRequest($request); // Check for existing routing parameters, redirecting to another locale automatically if necessary $locale = Fluent::get_request_locale(); if (empty($locale)) { // Determine if this user should be redirected $locale = $this->getRedirectLocale(); $this->extend('updateRedirectLocale', $locale); // Check if the user should be redirected $domainDefault = Fluent::default_locale(true); if (Fluent::is_locale($locale) && $locale !== $domainDefault) { // Check new traffic with detected locale return $this->redirect(Fluent::locale_baseurl($locale)); } // Reset parameters to act in the default locale $locale = $domainDefault; Fluent::set_persist_locale($locale); $params = $request->routeParams(); $params[Fluent::config()->query_param] = $locale; $request->setRouteParams($params); } if (!DB::isActive() || !ClassInfo::hasTable('SiteTree')) { $this->response = new SS_HTTPResponse(); $this->response->redirect(Director::absoluteBaseURL() . 'dev/build?returnURL=' . (isset($_GET['url']) ? urlencode($_GET['url']) : null)); return $this->response; } $localeURL = Fluent::alias($locale); $request->setUrl(self::fluent_homepage_link($localeURL)); $request->match($localeURL . '/$URLSegment//$Action', true); $controller = new ModelAsController(); $result = $controller->handleRequest($request, $model); $this->popCurrent(); return $result; }
/** * Set/Install the given locale. * This does set the i18n locale as well as the Translatable or Fluent locale (if any of these modules is installed) * @param string $locale the locale to install * @throws Zend_Locale_Exception @see Zend_Locale_Format::getDateFormat and @see Zend_Locale_Format::getTimeFormat */ public static function install_locale($locale) { // If the locale isn't given, silently fail (there might be carts that still have locale set to null) if (empty($locale)) { return; } if (class_exists('Translatable')) { Translatable::set_current_locale($locale); } else { if (class_exists('Fluent')) { Fluent::set_persist_locale($locale); } } // Do something like Fluent does to install the locale i18n::set_locale($locale); // LC_NUMERIC causes SQL errors for some locales (comma as decimal indicator) so skip foreach (array(LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_TIME) as $category) { setlocale($category, "{$locale}.UTF-8", $locale); } // Get date/time formats from Zend require_once 'Zend/Date.php'; i18n::config()->date_format = Zend_Locale_Format::getDateFormat($locale); i18n::config()->time_format = Zend_Locale_Format::getTimeFormat($locale); }
/** * Test versioning of localised objects */ public function testPublish() { // == Setup == Fluent::set_persist_locale('fr_CA'); Versioned::reading_stage('Stage'); // Create new record in non-default locale $id = Fluent::with_locale('es_ES', function () { $page = new Page(); $page->Title = 'ES Title'; $page->MenuTitle = 'ES Title'; $page->write(); return $page->ID; }); // == Check stage == // Check that the record has a title in the default locale $page = Versioned::get_one_by_stage("SiteTree", "Stage", "\"SiteTree\".\"ID\" = {$id}"); $this->assertEquals('ES Title', $page->Title); $this->assertEquals('ES Title', $page->MenuTitle); // Check that the record has a title in the foreign locale $record = Fluent::with_locale('es_ES', function () use($id) { $page = Versioned::get_one_by_stage("SiteTree", "Stage", "\"SiteTree\".\"ID\" = {$id}"); return $page->toMap(); }); $this->assertEquals('ES Title', $record['Title']); $this->assertEquals('ES Title', $record['MenuTitle']); // == Publish == // Save title in default locale $page = Versioned::get_one_by_stage("SiteTree", "Stage", "\"SiteTree\".\"ID\" = {$id}"); $page->Title = 'Default Title'; $page->MenuTitle = 'Custom Title'; $page->write(); // Publish this record in the custom locale Fluent::with_locale('es_ES', function () use($id) { $page = Versioned::get_one_by_stage("SiteTree", "Stage", "\"SiteTree\".\"ID\" = {$id}"); $page->doPublish(); }); // == Check live == // Check the live record has the correct title in the default locale $page = Versioned::get_one_by_stage("SiteTree", "Live", "\"SiteTree\".\"ID\" = {$id}"); $this->assertEquals('Default Title', $page->Title); $this->assertEquals('Custom Title', $page->MenuTitle); // Check the live record has the correct title in the custom locale $record = Fluent::with_locale('es_ES', function () use($id) { $page = Versioned::get_one_by_stage("SiteTree", "Live", "\"SiteTree\".\"ID\" = {$id}"); return $page->toMap(); }); $this->assertEquals('ES Title', $record['Title']); $this->assertEquals('ES Title', $record['MenuTitle']); }
/** * Test that records created in non-default locale don't have missing values for default fields */ public function testCreateInNonDefaultLocale() { Fluent::set_persist_locale('es_ES'); // Create a record in this locale $record = new FluentTest_TranslatedObject(); $record->Title = 'es title'; $record->Description = 'es description'; $record->write(); $recordID = $record->ID; $row = DB::query(sprintf("SELECT * FROM \"FluentTest_TranslatedObject\" WHERE ID = %d", $recordID))->first(); // Check that the necessary fields are assigned $this->assertEquals('es title', $row['Title']); $this->assertEquals('es title', $row['Title_es_ES']); $this->assertEquals('es title', $row['Title_fr_CA']); $this->assertEmpty($row['Title_en_NZ']); $this->assertEquals('es description', $row['Description']); $this->assertEquals('es description', $row['Description_es_ES']); $this->assertEquals('es description', $row['Description_fr_CA']); $this->assertEmpty($row['Description_en_NZ']); // modify locale in default locale Fluent::with_locale('fr_CA', function () use($recordID) { $record = FluentTest_TranslatedObject::get()->byID($recordID); $record->Title = 'new ca title'; $record->write(); }); // Check that the necessary fields are assigned $row = DB::query(sprintf("SELECT * FROM \"FluentTest_TranslatedObject\" WHERE ID = %d", $recordID))->first(); $this->assertEquals('new ca title', $row['Title']); $this->assertEquals('es title', $row['Title_es_ES']); $this->assertEquals('new ca title', $row['Title_fr_CA']); $this->assertEmpty($row['Title_en_NZ']); $this->assertEquals('es description', $row['Description']); $this->assertEquals('es description', $row['Description_es_ES']); $this->assertEquals('es description', $row['Description_fr_CA']); $this->assertEmpty($row['Description_en_NZ']); // modify in another locale Fluent::with_locale('en_NZ', function () use($recordID) { $record = FluentTest_TranslatedObject::get()->byID($recordID); $record->Title = 'nz title'; $record->Description = 'nz description'; $record->write(); }); // Check that the necessary fields are assigned $row = DB::query(sprintf("SELECT * FROM \"FluentTest_TranslatedObject\" WHERE ID = %d", $recordID))->first(); $this->assertEquals('new ca title', $row['Title']); $this->assertEquals('es title', $row['Title_es_ES']); $this->assertEquals('new ca title', $row['Title_fr_CA']); $this->assertEquals('nz title', $row['Title_en_NZ']); $this->assertEquals('es description', $row['Description']); $this->assertEquals('es description', $row['Description_es_ES']); $this->assertEquals('es description', $row['Description_fr_CA']); $this->assertEquals('nz description', $row['Description_en_NZ']); }
public function init() { parent::init(); // Reset any session locale to use the default locale as the standard 'base' locale Fluent::set_persist_locale(Fluent::default_locale(true)); }