/**
  * Create a new LanguageDropdownField
  * @param string $name
  * @param string $title
  * @param array $excludeLocales List of locales that won't be included
  * @param string $translatingClass Name of the class with translated instances where to look for used languages
  * @param string $list Indicates the source language list. Can be either Common-English, Common-Native, Locale-English, Locale-Native
  */
 function __construct($name, $title, $excludeLocales = array(), $translatingClass = 'SiteTree', $list = 'Common-English', $instance = null)
 {
     $usedLocalesWithTitle = Translatable::get_existing_content_languages($translatingClass);
     $usedLocalesWithTitle = array_diff_key($usedLocalesWithTitle, $excludeLocales);
     if ('Common-English' == $list) {
         $allLocalesWithTitle = i18n::get_common_languages();
     } else {
         if ('Common-Native' == $list) {
             $allLocalesWithTitle = i18n::get_common_languages(true);
         } else {
             if ('Locale-English' == $list) {
                 $allLocalesWithTitle = i18n::get_common_locales();
             } else {
                 if ('Locale-Native' == $list) {
                     $allLocalesWithTitle = i18n::get_common_locales(true);
                 } else {
                     $allLocalesWithTitle = i18n::get_locale_list();
                 }
             }
         }
     }
     if (isset($allLocales[Translatable::default_locale()])) {
         unset($allLocales[Translatable::default_locale()]);
     }
     // Limit to allowed locales if defined
     // Check for canTranslate() if an $instance is given
     $allowedLocales = Translatable::get_allowed_locales();
     foreach ($allLocalesWithTitle as $locale => $localeTitle) {
         if ($allowedLocales && !in_array($locale, $allowedLocales) || $excludeLocales && in_array($locale, $excludeLocales) || $usedLocalesWithTitle && array_key_exists($locale, $usedLocalesWithTitle)) {
             unset($allLocalesWithTitle[$locale]);
         }
     }
     // instance specific permissions
     foreach ($allLocalesWithTitle as $locale => $localeTitle) {
         if ($instance && !$instance->canTranslate(null, $locale)) {
             unset($allLocalesWithTitle[$locale]);
         }
     }
     foreach ($usedLocalesWithTitle as $locale => $localeTitle) {
         if ($instance && !$instance->canTranslate(null, $locale)) {
             unset($usedLocalesWithTitle[$locale]);
         }
     }
     // Sort by title (array value)
     asort($allLocalesWithTitle);
     if (count($usedLocalesWithTitle)) {
         asort($usedLocalesWithTitle);
         $source = array(_t('Form.LANGAVAIL', "Available languages") => $usedLocalesWithTitle, _t('Form.LANGAOTHER', "Other languages") => $allLocalesWithTitle);
     } else {
         $source = $allLocalesWithTitle;
     }
     parent::__construct($name, $title, $source);
 }
Esempio n. 2
0
 /**
  * Returns the localized name based on the field's value.
  * Example: "de_DE" returns "Deutsch".
  *
  * @return String
  */
 public function getNativeName()
 {
     $common_names = i18n::get_common_locales(true);
     return isset($common_names[$this->value]) ? $common_names[$this->value] : false;
 }
 /**
  *
  * Fetch a native language string from the `i18n` class via a passed locale
  * in the format "XX_xx". In the event a match cannot be found in any framework
  * resource, an empty string is returned.
  *
  * @param string $locale e.g. "pt_BR"
  * @return string The native language string for that locale e.g. "português (Brazil)"
  */
 public static function locale_native_name($locale)
 {
     // Attempts to fetch the native language string via the `i18n::$common_languages` array
     if ($native = i18n::get_language_name(i18n::get_lang_from_locale($locale), true)) {
         return $native;
     }
     // Attempts to fetch the native language string via the `i18n::$common_locales` array
     $commonLocales = i18n::get_common_locales(true);
     if (!empty($commonLocales[$locale])) {
         return $commonLocales[$locale];
     }
     // Nothing else to go on, so return an empty string for a consistent API
     return '';
 }
Esempio n. 4
0
 /**
  * Gets all locales that a member can access
  * as defined by {@link $allowed_locales}
  * and {@link canTranslate()}.
  * If {@link $allowed_locales} is not set and
  * the user has the `TRANSLATE_ALL` permission,
  * the method will return all available locales in the system.
  * 
  * @param Member $member
  * @return array Map of locales
  */
 function getAllowedLocalesForMember($member)
 {
     $locales = self::get_allowed_locales();
     if (!$locales) {
         $locales = i18n::get_common_locales();
     }
     if ($locales) {
         foreach ($locales as $k => $locale) {
             if (!$this->canTranslate($member, $locale)) {
                 unset($locales[$k]);
             }
         }
     }
     return $locales;
 }
 /**
  * Show the configuration fields for each subsite
  */
 function getCMSFields()
 {
     $domainTable = new TableField("Domains", "SubsiteDomain", array("Domain" => "Domain (use * as a wildcard)", "IsPrimary" => "Primary domain?"), array("Domain" => "TextField", "IsPrimary" => "CheckboxField"), "SubsiteID", $this->ID);
     $languageSelector = new DropdownField('Language', 'Language', i18n::get_common_locales());
     $fields = new FieldSet(new TabSet('Root', new Tab('Configuration', new HeaderField($this->getClassName() . ' configuration', 2), new TextField('Title', 'Name of subsite:', $this->Title), new HeaderField("Domains for this subsite"), $domainTable, $languageSelector, new CheckboxField('DefaultSite', 'Default site', $this->DefaultSite), new CheckboxField('IsPublic', 'Enable public access', $this->IsPublic), new DropdownField('Theme', 'Theme', $this->allowedThemes(), $this->Theme))), new HiddenField('ID', '', $this->ID), new HiddenField('IsSubsite', '', 1));
     $this->extend('updateCMSFields', $fields);
     return $fields;
 }
Esempio n. 6
0
 /**
  * Show the configuration fields for each subsite
  * 
  * @return FieldList
  */
 public function getCMSFields()
 {
     if ($this->ID != 0) {
         $domainTable = new GridField("Domains", _t('Subsite.DomainsListTitle', "Domains"), $this->Domains(), GridFieldConfig_RecordEditor::create(10));
     } else {
         $domainTable = new LiteralField('Domains', '<p>' . _t('Subsite.DOMAINSAVEFIRST', 'You can only add domains after saving for the first time') . '</p>');
     }
     $languageSelector = new DropdownField('Language', $this->fieldLabel('Language'), i18n::get_common_locales());
     $pageTypeMap = array();
     $pageTypes = SiteTree::page_type_classes();
     foreach ($pageTypes as $pageType) {
         $pageTypeMap[$pageType] = singleton($pageType)->i18n_singular_name();
     }
     asort($pageTypeMap);
     $fields = new FieldList($subsiteTabs = new TabSet('Root', new Tab('Configuration', _t('Subsite.TabTitleConfig', 'Configuration'), new HeaderField($this->getClassName() . ' configuration', 2), new TextField('Title', $this->fieldLabel('Title'), $this->Title), new HeaderField(_t('Subsite.DomainsHeadline', "Domains for this subsite")), $domainTable, $languageSelector, new CheckboxField('DefaultSite', $this->fieldLabel('DefaultSite'), $this->DefaultSite), new CheckboxField('IsPublic', $this->fieldLabel('IsPublic'), $this->IsPublic), new DropdownField('Theme', $this->fieldLabel('Theme'), $this->allowedThemes(), $this->Theme), new LiteralField('PageTypeBlacklistToggle', sprintf('<div class="field"><a href="#" id="PageTypeBlacklistToggle">%s</a></div>', _t('Subsite.PageTypeBlacklistField', 'Disallow page types?'))), new CheckboxSetField('PageTypeBlacklist', false, $pageTypeMap))), new HiddenField('ID', '', $this->ID), new HiddenField('IsSubsite', '', 1));
     $subsiteTabs->addExtraClass('subsite-model');
     $this->extend('updateCMSFields', $fields);
     return $fields;
 }
Esempio n. 7
0
 function getShortName()
 {
     $common_names = i18n::get_common_locales();
     return isset($common_names[$this->value]) ? $common_names[$this->value] : false;
 }
 /**
  * Returns the language name
  *
  * @param string $locale    Locale to get name for
  * @param string $in_locale Locale to display name in (when PHP intl is not installed, this is the indicator for native displaying)
  * 
  * @return string
  */
 public static function getDisplayLanguage($locale, $in_locale)
 {
     if (class_exists('Locale')) {
         $languageName = Locale::getDisplayLanguage($locale, $in_locale);
     } else {
         $native = false;
         if ($locale == $in_locale) {
             $native = true;
         }
         $languageName = i18n::get_language_name(substr($locale, 0, strpos($locale, '_')), $native);
         if (empty($languageName)) {
             $common_locales = i18n::get_common_locales($native);
             $all_locales = (array) Config::inst()->get('i18n', 'all_locales');
             if (array_key_exists($locale, $common_locales)) {
                 $languageName = $common_locales[$locale];
             } elseif (array_key_exists($locale, $all_locales)) {
                 $languageName = $all_locales[$locale];
             }
         }
     }
     return $languageName;
 }