/** * Return the enabled locales suitable for local choosing. * * @return array Returns an array in the form `[locale => localeName]`. */ public static function enabledLocales() { $defaultLocale = Gdn_Locale::Canonicalize(C('Garden.Locale')); $localeModel = new LocaleModel(); if (class_exists('Locale')) { $localePacks = $localeModel->EnabledLocalePacks(false); $locales = array(); foreach ($localePacks as $locale) { if (isset(static::$overrides[$locale]['Name'])) { $locales[$locale] = static::$overrides[$locale]['Name']; } else { $locales[$locale] = ucwords(Locale::getDisplayName($locale, $locale)); } } $defaultName = Locale::getDisplayName($defaultLocale, $defaultLocale); } else { $locales = $localeModel->EnabledLocalePacks(true); $locales = array_column($locales, 'Name', 'Locale'); $defaultName = $defaultLocale === 'en' ? 'English' : $defaultLocale; } asort($locales); if (!array_key_exists($defaultLocale, $locales)) { $locales = array_merge(array($defaultLocale => $defaultName), $locales); } return $locales; }
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 locales. $AvailableLocales = $LocaleModel->AvailableLocalePacks(); // Get the enabled locales. $EnabledLocales = $LocaleModel->EnabledLocalePacks(); // Check to enable/disable a plugin. if ($Op && Gdn::Session()->ValidateTransientKey($TransientKey)) { $Refresh = FALSE; switch (strtolower($Op)) { case 'enable': $Locale = GetValue($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; } if ($Refresh) { Gdn::Locale()->Refresh(); } } $this->SetData('AvailableLocales', $AvailableLocales); $this->SetData('EnabledLocales', $EnabledLocales); $this->Render(); }
/** * * * @param bool $Enabled * @return array */ public function getAddons($Enabled = false) { $Addons = array(); // Get the core. self::_AddAddon(array('AddonKey' => 'vanilla', 'AddonType' => 'core', 'Version' => APPLICATION_VERSION, 'Folder' => '/'), $Addons); // Get a list of all of the applications. $ApplicationManager = new Gdn_ApplicationManager(); if ($Enabled) { $Applications = $ApplicationManager->AvailableApplications(); } else { $Applications = $ApplicationManager->EnabledApplications(); } foreach ($Applications as $Key => $Info) { // Exclude core applications. if (in_array(strtolower($Key), array('conversations', 'dashboard', 'skeleton', 'vanilla'))) { continue; } $Addon = array('AddonKey' => $Key, 'AddonType' => 'application', 'Version' => val('Version', $Info, '0.0'), 'Folder' => '/applications/' . GetValue('Folder', $Info, strtolower($Key))); self::_AddAddon($Addon, $Addons); } // Get a list of all of the plugins. $PluginManager = Gdn::pluginManager(); if ($Enabled) { $Plugins = $PluginManager->EnabledPlugins(); } else { $Plugins = $PluginManager->AvailablePlugins(); } foreach ($Plugins as $Key => $Info) { // Exclude core plugins. if (in_array(strtolower($Key), array())) { continue; } $Addon = array('AddonKey' => $Key, 'AddonType' => 'plugin', 'Version' => val('Version', $Info, '0.0'), 'Folder' => '/applications/' . GetValue('Folder', $Info, $Key)); self::_AddAddon($Addon, $Addons); } // Get a list of all the themes. $ThemeManager = new Gdn_ThemeManager(); if ($Enabled) { $Themes = $ThemeManager->EnabledThemeInfo(true); } else { $Themes = $ThemeManager->AvailableThemes(); } foreach ($Themes as $Key => $Info) { // Exclude core themes. if (in_array(strtolower($Key), array('default'))) { continue; } $Addon = array('AddonKey' => $Key, 'AddonType' => 'theme', 'Version' => val('Version', $Info, '0.0'), 'Folder' => '/themes/' . GetValue('Folder', $Info, $Key)); self::_AddAddon($Addon, $Addons); } // Get a list of all locales. $LocaleModel = new LocaleModel(); if ($Enabled) { $Locales = $LocaleModel->EnabledLocalePacks(true); } else { $Locales = $LocaleModel->AvailableLocalePacks(); } foreach ($Locales as $Key => $Info) { // Exclude core themes. if (in_array(strtolower($Key), array('skeleton'))) { continue; } $Addon = array('AddonKey' => $Key, 'AddonType' => 'locale', 'Version' => val('Version', $Info, '0.0'), 'Folder' => '/locales/' . GetValue('Folder', $Info, $Key)); self::_AddAddon($Addon, $Addons); } return $Addons; }
/** * 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 (Gdn::Session()->ValidateTransientKey($TransientKey) || $this->Form->AuthenticatedPostBack()) { if ($Op) { $Refresh = FALSE; switch (strtolower($Op)) { case 'enable': $Locale = GetValue($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', C('Garden.Locale', 'en-CA')); } 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(); } } elseif (!$this->Form->IsPostBack()) { $this->Form->SetValue('Locale', C('Garden.Locale', 'en-CA')); } // Check for the default locale warning. $DefaultLocale = C('Garden.Locale'); if ($DefaultLocale != 'en-CA') { $LocaleFound = FALSE; $MatchingLocales = array(); foreach ($AvailableLocales as $Key => $LocaleInfo) { $Locale = GetValue('Locale', $LocaleInfo); if ($Locale == $DefaultLocale) { $MatchingLocales[] = GetValue('Name', $LocaleInfo, $Key); } if (GetValue($Key, $EnabledLocales) == $DefaultLocale) { $LocaleFound = TRUE; } } $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(); }
/** * Confirm selected locale is valid and available. * * @param string $Locale Locale code. * @return $Locale or FALSE. */ protected function ValidateLocale($Locale) { $LocaleModel = new LocaleModel(); $Options = $LocaleModel->EnabledLocalePacks(); $Options['English'] = 'en-CA'; // Hackily include the default return in_array($Locale, $Options) ? $Locale : FALSE; }