Author: Chris Pollett
Inheritance: extends Model
 /**
  * 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;
 }
Exemple #2
0
/**
 * If the locale data of Yioop! in the work directory is older than the
 * currently running Yioop! then this function is called to at least
 * try to copy the new strings into the old profile.
 */
function upgradeLocales()
{
    if (!PROFILE) {
        return;
    }
    $locale = new LocaleModel();
    $locale->initialize(DEFAULT_LOCALE);
    $force_folders = array();
    /*
        if we're upgrading version2 to 3 we want to make sure stemmer becomes
        tokenizer, version3 to 4 pushes out stopwordsRemover used for
        summarization. version 6 to 7 adds stemmers for french, english, german.
        version 7 to 8 adds stemmers for russian and spanish
    */
    if (!isset($locale->configure['strings']["view_locale_version10"])) {
        $force_folders = array("resources");
    }
    $locale->extractMergeLocales($force_folders);
}
 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;
 }
 /**
  * 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();
 }
 /**
  * @inheritDoc IElementType::getTableAttributeHtml()
  *
  * @param BaseElementModel $element
  * @param string           $attribute
  *
  * @return string
  */
 public function getTableAttributeHtml(BaseElementModel $element, $attribute)
 {
     // First give plugins a chance to set this
     $pluginAttributeHtml = craft()->plugins->callFirst('getUserTableAttributeHtml', array($element, $attribute), true);
     if ($pluginAttributeHtml !== null) {
         return $pluginAttributeHtml;
     }
     switch ($attribute) {
         case 'email':
             $email = $element->email;
             if ($email) {
                 return HtmlHelper::encodeParams('<a href="mailto:{email}">{email}</a>', array('email' => $email));
             } else {
                 return '';
             }
         case 'preferredLocale':
             $localeId = $element->preferredLocale;
             if ($localeId) {
                 $locale = new LocaleModel($localeId);
                 return $locale->getName();
             } else {
                 return '';
             }
         default:
             return parent::getTableAttributeHtml($element, $attribute);
     }
 }
 public function _Settings($Sender, $Args)
 {
     $Sender->Form = $this->Form;
     // Grab the existing locale packs.
     $LocaleModel = new LocaleModel();
     $LocalePacks = $LocaleModel->AvailableLocalePacks();
     $LocalArray = array();
     foreach ($LocalePacks as $Key => $Info) {
         $LocaleArray[$Key] = GetValue('Name', $Info, $Key);
     }
     $Sender->SetData('LocalePacks', $LocaleArray);
     if ($this->Form->IsPostBack()) {
         if ($this->Form->GetFormValue('Save')) {
             $Values = ArrayTranslate($this->Form->FormValues(), array('Key', 'Name', 'Locale', 'CaptureDefinitions'));
             $SaveValues = array();
             foreach ($Values as $Key => $Value) {
                 $SaveValues['Plugins.LocaleDeveloper.' . $Key] = $Value;
             }
             // Save the settings.
             SaveToConfig($SaveValues, '', array('RemoveEmpty' => TRUE));
             $Sender->StatusMessage = T('Your changes have been saved.');
         } elseif ($this->Form->GetFormValue('GenerateChanges')) {
             $Key = $this->Form->GetFormValue('LocalePackForChanges');
             if (!$Key) {
                 $this->Form->AddError('ValidateRequired', 'Locale Pack');
             }
             $Path = PATH_ROOT . '/locales/' . $Key;
             if (!file_exists($Path)) {
                 $this->Form->AddError('Could not find the selected locale pack.');
             }
             if ($this->Form->ErrorCount() == 0) {
                 try {
                     $LocaleModel->GenerateChanges($Path, $this->LocalePath);
                     $Sender->StatusMessage = T('Your changes have been saved.');
                 } catch (Exception $Ex) {
                     $this->Form->AddError($Ex);
                 }
             }
         } elseif ($this->Form->GetFormValue('Copy')) {
             $Key = $this->Form->GetFormValue('LocalePackForCopy');
             if (!$Key) {
                 $this->Form->AddError('ValidateRequired', 'Locale Pack');
             }
             $Path = PATH_ROOT . '/locales/' . $Key;
             if (!file_exists($Path)) {
                 $this->Form->AddError('Could not find the selected locale pack.');
             }
             if ($this->Form->ErrorCount() == 0) {
                 try {
                     $LocaleModel->CopyDefinitions($Path, $this->LocalePath . '/copied.php');
                     $Sender->StatusMessage = T('Your changes have been saved.');
                 } catch (Exception $Ex) {
                     $this->Form->AddError($Ex);
                 }
             }
         } elseif ($this->Form->GetFormValue('Remove')) {
             $Files = SafeGlob($this->LocalePath . '/*');
             foreach ($Files as $File) {
                 $Result = unlink($File);
                 if (!$Result) {
                     $this->Form->AddError('@' . sprintf(T('Could not remove %s.'), $File));
                 }
             }
             if ($this->Form->ErrorCount() == 0) {
                 $Sender->StatusMessage = T('Your changes have been saved.');
             }
         }
     } else {
         $Values = C('Plugins.LocaleDeveloper');
         foreach ($Values as $Key => $Value) {
             $this->Form->SetFormValue($Key, $Value);
         }
     }
     $Sender->SetData('LocalePath', $this->LocalePath);
     $Sender->Render('', '', 'plugins/LocaleDeveloper');
 }
 /**
  * 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();
 }
Exemple #11
0
/**
 * Sets the language to be used for locale settings
 *
 * @param string $locale_tag the tag of the language to use to determine
 *     locale settings
 */
function setLocaleObject($locale_tag)
{
    global $locale;
    $locale = new LocaleModel();
    $locale->initialize($locale_tag);
}