Retrieves the default locale
public static default_locale ( mixed $domain = null ) : string | ||
$domain | mixed | Domain to determine the default locale for. If null, the global default will be returned. If true, then the current domain will be used. |
return | string |
public function updateRelativeLink(&$base, &$action) { // Don't inject locale to subpages if ($this->owner->ParentID && SiteTree::config()->nested_urls) { return; } // For blank/temp pages such as Security controller fallback to querystring $locale = Fluent::current_locale(); if (!$this->owner->exists()) { $base = Controller::join_links($base, '?' . Fluent::config()->query_param . '=' . urlencode($locale)); return; } // Check if this locale is the default for its own domain $domain = Fluent::domain_for_locale($locale); if ($locale === Fluent::default_locale($domain)) { // For home page in the default locale, do not alter home url if ($base === null) { return; } // For all pages on a domain where there is only a single locale, // then the domain itself is sufficient to distinguish that domain // See https://github.com/tractorcow/silverstripe-fluent/issues/75 $domainLocales = Fluent::locales($domain); if (count($domainLocales) === 1) { return; } } // Simply join locale root with base relative URL $localeURL = Fluent::alias($locale); $base = Controller::join_links($localeURL, $base); }
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; }
public function augmentWrite(&$manipulation) { // Bypass augment write if requested if (!self::$_enable_write_augmentation) { return; } // Get locale and translation zone to use $locale = $this->owner->getSourceQueryParam('Fluent.Locale') ?: Fluent::current_locale(); $defaultLocale = Fluent::default_locale(); // Get all tables to translate fields for, and their respective field names $includedTables = $this->getTranslatedTables(); // Iterate through each select clause, replacing each with the translated version foreach ($manipulation as $class => $updates) { // If this table doesn't have translated fields then skip if (empty($includedTables[$class])) { continue; } foreach ($includedTables[$class] as $field) { // Skip translated field if not updated in this request if (!isset($updates['fields'][$field])) { continue; } // Copy the updated value to the locale specific field // (Title => Title_fr_FR) $updateField = Fluent::db_field_for_locale($field, $locale); $updates['fields'][$updateField] = $updates['fields'][$field]; // If not on the default locale, write the stored default field back to the main field // (if Title_en_NZ then Title_en_NZ => Title) // If the default subfield has no value, then save using the current locale if ($locale !== $defaultLocale) { $defaultField = Fluent::db_field_for_locale($field, $defaultLocale); // Note that null may be DB escaped as a string here. @see DBField::prepValueForDB if (!empty($updates['fields'][$defaultField]) && $updates['fields'][$defaultField] != DB::getConn()->prepStringForDB('') && strtolower($updates['fields'][$defaultField]) != 'null') { $updates['fields'][$field] = $updates['fields'][$defaultField]; } } } // Save back modifications to the manipulation $manipulation[$class] = $updates; } }
/** * Test output for helpers in domain mode */ public function testDomainsHelpers() { Config::inst()->update('Fluent', 'force_domain', true); // Test Fluent::domains $this->assertEquals(array('www.example.com', 'www.example.ca', 'www.example.co.nz'), array_keys(Fluent::domains())); // Test Fluent::default_locale $usDefault = $this->withURL('www.example.com', '/', '/', function ($test) { return Fluent::default_locale(true); }); $this->assertEquals('en_US', $usDefault); $this->assertEquals('en_US', Fluent::default_locale('www.example.com')); $this->assertEquals('fr_CA', Fluent::default_locale()); // Test Fluent::domain_for_locale $this->assertEquals(null, Fluent::domain_for_locale('nl_NL')); $this->assertEquals('www.example.com', Fluent::domain_for_locale('en_US')); $this->assertEquals('www.example.com', Fluent::domain_for_locale('es_ES')); $this->assertEquals('www.example.ca', Fluent::domain_for_locale('fr_CA')); $this->assertEquals('www.example.co.nz', Fluent::domain_for_locale('en_NZ')); // Test Fluent::locales $usLocales = $this->withURL('www.example.com', '/', '/', function ($test) { return Fluent::locales(true); }); $this->assertEquals(array('es_ES', 'en_US'), $usLocales); $this->assertEquals(array('es_ES', 'en_US'), Fluent::locales('www.example.com')); $this->assertEquals(array('fr_CA', 'en_NZ', 'en_US', 'es_ES'), Fluent::locales()); Config::inst()->update('Fluent', 'force_domain', false); }
public function run($request) { // Extend time limit set_time_limit(100000); // we may need some proivileges for this to work // without this, running under sake is a problem // maybe sake could take care of it ... Security::findAnAdministrator()->login(); $this->checkInstalled(); $this->withTransaction(function ($task) { Versioned::reading_stage('Stage'); $classes = $task->fluentClasses(); $tables = DB::tableList(); $deleteQueue = array(); foreach ($classes as $class) { // Ensure that a translationgroup table exists for this class $groupTable = strtolower($class . "_translationgroups"); if (isset($tables[$groupTable])) { $groupTable = $tables[$groupTable]; } else { Debug::message("Ignoring class without _translationgroups table {$class}", false); continue; } // Disable filter if ($class::has_extension('FluentFilteredExtension')) { $class::remove_extension('FluentFilteredExtension'); } // Select all instances of this class in the default locale $instances = DataObject::get($class, sprintf('"Locale" = \'%s\'', Convert::raw2sql(Fluent::default_locale()))); foreach ($instances as $instance) { $isPublished = false; if ($instance->hasMethod('isPublished')) { $isPublished = $instance->isPublished(); } if ($instance->ObsoleteClassName) { Debug::message("Skipping {$instance->ClassName} with ID {$instanceID} because it from an obsolete class", false); continue; } $instanceID = $instance->ID; $translatedFields = $task->getTranslatedFields($instance->ClassName); Debug::message("Updating {$instance->ClassName} {$instance->MenuTitle} ({$instanceID})", false); $changed = false; // Select all translations for this $translatedItems = DataObject::get($class, sprintf('"Locale" != \'%1$s\' AND "ID" IN ( SELECT "OriginalID" FROM "%2$s" WHERE "TranslationGroupID" IN ( SELECT "TranslationGroupID" FROM "%2$s" WHERE "OriginalID" = %3$d ) )', Convert::raw2sql(Fluent::default_locale()), $groupTable, $instanceID)); foreach ($translatedItems as $translatedItem) { $locale = DB::query(sprintf('SELECT "Locale" FROM "%s" WHERE "ID" = %d', $class, $translatedItem->ID))->value(); // since we are going to delete the stuff // anyway, no need bothering validating it DataObject::config()->validation_enabled = false; // Unpublish and delete translated record if ($translatedItem->hasMethod('doUnpublish')) { Debug::message(" -- Unpublishing {$locale}", false); if ($translatedItem->doUnpublish() === false) { throw new ConvertTranslatableException("Failed to unpublish"); } } Debug::message(" -- Adding {$translatedItem->ID} ({$locale})", false); foreach ($translatedFields as $field) { $trField = Fluent::db_field_for_locale($field, $locale); if ($translatedItem->{$field}) { Debug::message(" -- Adding {$trField}", false); $instance->{$trField} = $translatedItem->{$field}; $changed = true; } } // for some reason, deleting items here has disruptive effects // as too much stuff gets removed, so lets wait with this until the end of the migration $deleteQueue[] = $translatedItem; } if ($changed) { if (!$isPublished) { $instance->write(); } elseif ($instance->doPublish() === false) { Debug::message(" -- Publishing FAILED", false); throw new ConvertTranslatableException("Failed to publish"); } else { Debug::message(" -- Published", false); } } } } foreach ($deleteQueue as $delItem) { Debug::message(" -- Removing {$delItem->ID}", false); $delItem->delete(); } }); }
public function testResetTranslations() { $page = $this->objFromFixture('FluentTest_TranslatedPage', 'translated1'); $defaultField = 'Title_' . Fluent::default_locale(); $defaultValue = $page->{$defaultField}; $page->resetTranslations(); $this->assertEquals($page->Title_fr_CA, $defaultValue); $this->assertEquals($page->Title_en_NZ, $defaultValue); $this->assertEquals($page->Title_en_US, $defaultValue); $this->assertEquals($page->Title_es_ES, $defaultValue); }
/** * Adds a UI message to indicate whether you're editing in the default locale or not * * @param FieldList $fields * @return $this */ protected function addLocaleIndicatorMessage(FieldList $fields) { if (Fluent::config()->disable_current_locale_message) { return $this; } // If the field is already present, don't add it a second time if ($fields->fieldByName('CurrentLocaleMessage')) { return $this; } $localeNames = Fluent::locale_names(); $isDefaultLocale = Fluent::default_locale() === Fluent::current_locale(); $messageClass = $isDefaultLocale ? 'good' : 'notice'; $message = $isDefaultLocale ? _t('Fluent.DefaultLocale', 'This is the default locale') : _t('Fluent.DefaultLocaleIs', 'The default locale is') . ' ' . $localeNames[Fluent::default_locale()]; $fields->unshift(LiteralField::create('CurrentLocaleMessage', sprintf('<p class="message %s">' . _t('Fluent.EditingIn', 'Please note: You are editing in') . ' %s. %s.</p>', $messageClass, $localeNames[Fluent::current_locale()], $message))); return $this; }
/** * Resets all translated fields to their value in the default locale */ public function resetTranslations() { $translated = $this->getTranslatedTables(); foreach ($translated as $table => $fields) { foreach ($fields as $field) { $defaultField = Fluent::db_field_for_locale($field, Fluent::default_locale()); $defaultValue = $this->owner->{$defaultField}; foreach (Fluent::locales() as $locale) { if ($locale === Fluent::default_locale()) { continue; } $localeField = Fluent::db_field_for_locale($field, $locale); $originalValue = $this->owner->{$localeField}; $this->owner->{$localeField} = $defaultValue; // If these values differ, but a change isn't detected, then force a change if ($this->owner->exists() && $originalValue != $defaultValue && !$this->owner->isChanged($field)) { $this->owner->forceChange(); } $this->owner->write(); } } } }
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)); }