Gets the current locale
public static current_locale ( boolean $persist = true ) : string | ||
$persist | boolean | Attempt to persist any detected locale within session / cookies |
return | string | i18n locale code |
public function fix_fluent_menu() { if (!class_exists('Fluent')) { return; } $conf = SiteConfig::current_site_config(); $localesNames = Fluent::locale_names(); if ($conf->hasExtension('ActiveLocalesExtension') && $conf->ActiveLocales) { $localesNames = $conf->ActiveLocalesNames(); } $locales = json_encode($localesNames); $locale = json_encode(Fluent::current_locale()); // If we have only one locale, set this one as default if (count($localesNames) === 1) { $locale = json_encode(key($localesNames)); } $param = json_encode(Fluent::config()->query_param); $buttonTitle = json_encode(_t('Fluent.ChangeLocale', 'Change Locale')); Requirements::block('FluentHeadScript'); Requirements::insertHeadTags(<<<EOT <script type="text/javascript"> //<![CDATA[ \tvar fluentLocales = {$locales}; \tvar fluentLocale = {$locale}; \tvar fluentParam = {$param}; \tvar fluentButtonTitle = {$buttonTitle}; //]]> </script> EOT , 'FluentHeadScriptSubsite'); }
public function augmentSQL(SQLQuery &$query, DataQuery &$dataQuery = null) { // Actives locales defined on a SiteConfig are there as a global setting if ($this->owner instanceof SiteConfig) { return; } // In admin, show everthing anyway if ($this->isAdminBackend()) { return; } // Find in set is only compatible with MySql $c = DB::getConn(); if (!$c instanceof MySQLDatabase) { return; } $locale = $dataQuery->getQueryParam('Fluent.Locale') ?: Fluent::current_locale(); $from = $query->getFrom(); $where = $query->getWhere(); $column = 'ActiveLocales'; $table = null; // Check on which table is the ActiveLocales field foreach ($from as $fromTable => $conditions) { if ($table === null) { $table = $fromTable; } $db = DataObject::custom_database_fields($fromTable); if ($db && isset($db[$column])) { $table = $fromTable; break; } } $identifier = "\"{$table}\".\"{$column}\""; $where[] = "{$identifier} IS NULL OR FIND_IN_SET ('{$locale}', {$identifier}) > 0"; $query->setWhere($where); }
public function augmentSQL(SQLQuery &$query, DataQuery &$dataQuery = null) { // When filtering my menu, swap out condition for locale specific condition $locale = Fluent::current_locale(); $field = Fluent::db_field_for_locale("ShowInMenus", $locale); $query->replaceText("\"{$this->ownerBaseClass}\".\"ShowInMenus\"", "\"{$this->ownerBaseClass}\".\"{$field}\""); }
/** * Retrieves information about this object in the CURRENT locale * * @param string $locale The locale information to request, or null to use the default locale * @return ArrayData Mapped list of locale properties */ public function CurrentLocaleInformation() { $locale = Fluent::current_locale(); // Store basic locale information $data = array('Locale' => $locale, 'LocaleRFC1766' => i18n::convert_rfc1766($locale), 'Alias' => Fluent::alias($locale), 'Title' => i18n::get_locale_name($locale), 'LanguageNative' => i18n::get_language_name(i18n::get_lang_from_locale($locale), true)); return new ArrayData($data); }
/** * Get the current locale. * Tries to get the locale from Translatable, Fluent or the default i18n (depending on what is installed) * @return string the locale in use */ public static function get_current_locale() { if (class_exists('Translatable')) { return Translatable::get_current_locale(); } if (class_exists('Fluent')) { return Fluent::current_locale(); } return i18n::get_locale(); }
public function updateCMSFields(FieldList $fields) { parent::updateCMSFields($fields); // Fix URLSegment field issue for root pages if (!SiteTree::config()->nested_urls || empty($this->owner->ParentID)) { $baseLink = Director::absoluteURL(Controller::join_links(Director::baseURL(), Fluent::alias(Fluent::current_locale()), '/')); $urlsegment = $fields->dataFieldByName('URLSegment'); $urlsegment->setURLPrefix($baseLink); } }
public function init() { $dirName = basename(dirname(dirname(dirname(__FILE__)))); $locales = json_encode(Fluent::locale_names()); $locale = json_encode(Fluent::current_locale()); $param = json_encode(Fluent::config()->query_param); $buttonTitle = json_encode(_t('Fluent.ChangeLocale', 'Change Locale')); // Force the variables to be written to the head, to ensure these are available for other scripts to pick up. Requirements::insertHeadTags(<<<EOT <script type="text/javascript"> //<![CDATA[ \tvar fluentLocales = {$locales}; \tvar fluentLocale = {$locale}; \tvar fluentParam = {$param}; \tvar fluentButtonTitle = {$buttonTitle}; //]]> </script> EOT , 'FluentHeadScript'); Requirements::javascript("{$dirName}/javascript/fluent.js"); Requirements::css("{$dirName}/css/fluent.css"); }
/** * Add 'Hidden' flag to the SiteTree object if the page is not present in this locale * * @param array $flags */ public function updateStatusFlags(&$flags) { if (!$this->owner->{Fluent::db_field_for_locale("LocaleFilter", Fluent::current_locale())}) { $flags['fluenthidden'] = array('text' => _t('Fluent.BadgeHiddenShort', 'Hidden'), 'title' => _t('Fluent.BadgeHiddenHelp', 'Page is hidden in this locale')); } }
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; } }
/** * Determine the baseurl within a specified $locale. * * @param string $locale Locale, or null to use current locale * @return string */ public static function locale_baseurl($locale = null) { if (empty($locale)) { $locale = Fluent::current_locale(); } // Build domain-specific base url $base = Director::baseURL(); if ($domain = Fluent::domain_for_locale($locale)) { $base = Controller::join_links(Director::protocol() . $domain, $base); } // Don't append locale to home page for default locale if ($locale === self::default_locale()) { return $base; } // Append locale otherwise return Controller::join_links($base, self::alias($locale), '/'); }
public function updateCMSFields(FieldList $fields) { // get all fields to translate and remove $translated = $this->getTranslatedTables(); foreach ($translated as $table => $translatedFields) { foreach ($translatedFields as $translatedField) { // Find field matching this translated field // If the translated field has an ID suffix also check for the non-suffixed version // E.g. UploadField() $field = $fields->dataFieldByName($translatedField); if (!$field && preg_match('/^(?<field>\\w+)ID$/', $translatedField, $matches)) { $field = $fields->dataFieldByName($matches['field']); } // Highlight any translated field if ($field && !$field->hasClass('LocalisedField')) { // Add a language indicator next to the fluent icon $locale = Fluent::current_locale(); $title = $field->Title(); $field->setTitle('<span class="fluent-locale-label">' . strtok($locale, '_') . '</span>' . $title); $field->addExtraClass('LocalisedField'); } // Remove translation DBField from automatic scaffolded fields foreach (Fluent::locales() as $locale) { $fieldName = Fluent::db_field_for_locale($translatedField, $locale); $fields->removeByName($fieldName, true); } } } }
public function Link() { return Controller::join_links(Director::baseURL(), Fluent::current_locale(), 'link', '/'); }
/** * Rewrites the WHERE fragment of a query * * @param string $class * @param array $translatedColumns Translated columns * @param SQLQUery $query * @param string $keywords SQL escaped keywords * @param string $keywordsHTML HTML escaped keywords */ public function augmentFilter($class, $translatedColumns, SQLQuery $query, $keywords, $keywordsHTML, $booleanMode) { // Augment the search section $locale = Fluent::current_locale(); $wherePattern = self::$where_replacements[$class]; $translatedPattern = $wherePattern; $searchColumns = self::$search_columns[$class]; foreach (array_intersect($searchColumns, $translatedColumns) as $column) { $replacement = Fluent::db_field_for_locale($column, $locale); $translatedPattern = preg_replace('/\\b' . preg_quote($column) . '\\b/', $replacement, $translatedPattern); } // If no fields were translated, then don't modify if ($translatedPattern === $wherePattern) { return; } // Inject keywords into patterns $search = array('/\\$keywords/i', '/\\$htmlEntityKeywords/i', '/\\$boolean/i'); $replace = array($keywords, $keywordsHTML, $booleanMode ? 'IN BOOLEAN MODE' : ''); $whereOriginal = preg_replace($search, $replace, $wherePattern); $whereTranslated = preg_replace($search, $replace, $translatedPattern); $where = $query->getWhere(); $newWhere = array(); foreach ($query->getWhere() as $where) { // Remove excessive whitespace which breaks string replacement $where = preg_replace('/\\s+/im', ' ', $where); $whereOriginal = preg_replace('/\\s+/im', ' ', $whereOriginal); $newWhere[] = str_replace($whereOriginal, "{$whereOriginal} + {$whereTranslated}", $where); } $query->setWhere($newWhere); }
/** * * Return a MenuTitle depending on if we have the Fluent module enabled, and the * current selected locale. * * It is also obviously contingent on each of the MenuTitle_<locale> fields * having been configured correctly. * * This method obviously _only_ deals with the MenuTitle_<locale> field. It is * therefore not at all DRY in the sense that there is no answer to the following * question: What do we do for other properties-as-fields, configured and * translated via Fluent? * * @return string */ public function MenuTitle() { if ($this->hasFluent()) { $localeCurrent = Fluent::current_locale(); $fieldCurrent = 'MenuTitle_' . $localeCurrent; if (isset($this->{$fieldCurrent}) && ($title = $this->{$fieldCurrent})) { return $title; } } return $this->MenuTitle; }
/** * 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; }