/** * Manage list of locales. * * @since 2.0.0 * @access public * @param string $Op 'enable' or 'disable' * @param string $LocaleKey Unique ID of locale to be modified. * @param string $TransientKey Security token. */ public function locales($Op = null, $LocaleKey = null, $TransientKey = null) { $this->permission('Garden.Settings.Manage'); $this->title(t('Locales')); $this->addSideMenu('dashboard/settings/locales'); $this->addJsFile('addons.js'); $LocaleModel = new LocaleModel(); // Get the available locale packs. $AvailableLocales = $LocaleModel->availableLocalePacks(); // Get the enabled locale packs. $EnabledLocales = $LocaleModel->enabledLocalePacks(); // Check to enable/disable a locale. if ($TransientKey && Gdn::session()->validateTransientKey($TransientKey) || $this->Form->authenticatedPostBack()) { if ($Op) { $Refresh = false; switch (strtolower($Op)) { case 'enable': $Locale = val($LocaleKey, $AvailableLocales); if (!is_array($Locale)) { $this->Form->addError('@' . sprintf(t('The %s locale pack does not exist.'), htmlspecialchars($LocaleKey)), 'LocaleKey'); } elseif (!isset($Locale['Locale'])) { $this->Form->addError('ValidateRequired', 'Locale'); } else { saveToConfig("EnabledLocales.{$LocaleKey}", $Locale['Locale']); $EnabledLocales[$LocaleKey] = $Locale['Locale']; $Refresh = true; } break; case 'disable': RemoveFromConfig("EnabledLocales.{$LocaleKey}"); unset($EnabledLocales[$LocaleKey]); $Refresh = true; break; } // Set default locale field if just doing enable/disable $this->Form->setValue('Locale', Gdn_Locale::canonicalize(c('Garden.Locale', 'en'))); } elseif ($this->Form->authenticatedPostBack()) { // Save the default locale. saveToConfig('Garden.Locale', $this->Form->getFormValue('Locale')); $Refresh = true; $this->informMessage(t("Your changes have been saved.")); } if ($Refresh) { Gdn::locale()->refresh(); redirect('/settings/locales'); } } elseif (!$this->Form->isPostBack()) { $this->Form->setValue('Locale', Gdn_Locale::canonicalize(c('Garden.Locale', 'en'))); } // Check for the default locale warning. $DefaultLocale = Gdn_Locale::canonicalize(c('Garden.Locale')); if ($DefaultLocale !== 'en') { $LocaleFound = false; $MatchingLocales = array(); foreach ($AvailableLocales as $Key => $LocaleInfo) { $Locale = val('Locale', $LocaleInfo); if ($Locale == $DefaultLocale) { $MatchingLocales[] = val('Name', $LocaleInfo, $Key); } if (val($Key, $EnabledLocales) == $DefaultLocale) { $LocaleFound = true; } } $this->setData('DefaultLocale', $DefaultLocale); $this->setData('DefaultLocaleWarning', !$LocaleFound); $this->setData('MatchingLocalePacks', htmlspecialchars(implode(', ', $MatchingLocales))); } $this->setData('AvailableLocales', $AvailableLocales); $this->setData('EnabledLocales', $EnabledLocales); $this->setData('Locales', $LocaleModel->availableLocales()); $this->render(); }
/** * Manage list of locales. * * @since 2.0.0 * @access public * @param string $Op 'enable' or 'disable' * @param string $LocaleKey Unique ID of locale to be modified. * @param string $TransientKey Security token. */ public function locales($Op = null, $LocaleKey = null) { $this->permission('Garden.Settings.Manage'); $this->title(t('Locales')); $this->setHighlightRoute('dashboard/settings/locales'); $this->addJsFile('addons.js'); $LocaleModel = new LocaleModel(); // Get the available locale packs. $AvailableLocales = $LocaleModel->availableLocalePacks(); // Get the enabled locale packs. $EnabledLocales = $LocaleModel->enabledLocalePacks(); // Check to enable/disable a locale. if ($this->Form->authenticatedPostBack() && !$Op) { // Save the default locale. saveToConfig('Garden.Locale', $this->Form->getFormValue('Locale')); $this->informMessage(t("Your changes have been saved.")); Gdn::locale()->refresh(); redirect('/settings/locales'); } else { $this->Form->setValue('Locale', Gdn_Locale::canonicalize(c('Garden.Locale', 'en'))); } if ($Op) { switch (strtolower($Op)) { case 'enable': $this->enableLocale($LocaleKey, val($LocaleKey, $AvailableLocales), $EnabledLocales); break; case 'disable': $this->disableLocale($LocaleKey, val($LocaleKey, $AvailableLocales), $EnabledLocales); } } // Check for the default locale warning. $DefaultLocale = Gdn_Locale::canonicalize(c('Garden.Locale')); if ($DefaultLocale !== 'en') { $LocaleFound = false; $MatchingLocales = array(); foreach ($AvailableLocales as $Key => $LocaleInfo) { $Locale = val('Locale', $LocaleInfo); if ($Locale == $DefaultLocale) { $MatchingLocales[] = val('Name', $LocaleInfo, $Key); } if (val($Key, $EnabledLocales) == $DefaultLocale) { $LocaleFound = true; } } $this->setData('DefaultLocale', $DefaultLocale); $this->setData('DefaultLocaleWarning', !$LocaleFound); $this->setData('MatchingLocalePacks', htmlspecialchars(implode(', ', $MatchingLocales))); } // Remove all hidden locales, unless they are enabled. $AvailableLocales = array_filter($AvailableLocales, function ($locale) use($EnabledLocales) { return !val('Hidden', $locale) || isset($EnabledLocales[val('Index', $locale)]); }); $this->setData('AvailableLocales', $AvailableLocales); $this->setData('EnabledLocales', $EnabledLocales); $this->setData('Locales', $LocaleModel->availableLocales()); $this->render(); }