SettingsController::locales PHP Method

locales() public method

Manage list of locales.
Since: 2.0.0
public locales ( string $Op = null, string $LocaleKey = null )
$Op string 'enable' or 'disable'
$LocaleKey string Unique ID of locale to be modified.
    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();
    }