/** * @param $locale * @return mixed */ public function getLanguageDropdownField($locale) { $locale = Translatable::get_current_locale(); $translation = _t('CMSMain.LANGUAGEDROPDOWNLABEL', 'Language'); $field = LanguageDropdownField::create('Locale', $translation, array(), 'SiteTree', 'Locale-English', singleton('SiteTree')); $field->setValue($locale); return $field; }
/** * Presents a form to select a language to translate the pages selected with batch actions into. * * @param string $pageIDs | null * @return Form $form */ public function TranslatePagesForm($pageIDs = null) { Versioned::reading_stage('Stage'); // Needs this for changes to effect draft tree $languages = Translatable::get_allowed_locales(); $action = FormAction::create('doTranslatePages', 'Translate')->setUseButtonTag('true')->addExtraClass('ss-ui-button ss-ui-action-constructive batch-form-actions')->setUseButtonTag(true); $actions = FieldList::create($action); $allFields = new CompositeField(); $allFields->addExtraClass('batch-form-body'); if ($pageIDs == null) { $pageIDs = $this->getRequest()->getVar('PageIDs'); } else { $allFields->push(new LiteralField("ErrorParent", '<p class="message bad">Invalid parent selected, please choose another</p>')); } $allFields->push(new HiddenField("PageIDs", "PageIDs", $pageIDs)); $allFields->push(LanguageDropdownField::create("NewTransLang", _t('Translatable.NEWLANGUAGE', 'New language'), $languages)); $headings = new CompositeField(new LiteralField('Heading', sprintf('<h3 class="">%s</h3>', _t('HtmlEditorField.MOVE', 'Choose Language...')))); $headings->addExtraClass('cms-content-header batch-pages'); $fields = new FieldList($headings, $allFields); $form = Form::create($this, 'TranslatePagesForm', $fields, $actions); return $form; }
/** * If the record is not shown in the default language, this method * will try to autoselect a master language which is shown alongside * the normal formfields as a readonly representation. * This gives translators a powerful tool for their translation workflow * without leaving the translated page interface. * Translatable also adds a new tab "Translation" which shows existing * translations, as well as a formaction to create new translations based * on a dropdown with available languages. * * This method can be called multiple times on the same FieldList * because it checks which fields have already been added or modified. * * @todo This is specific to SiteTree and CMSMain * @todo Implement a special "translation mode" which triggers display of the * readonly fields, so you can translation INTO the "default language" while * seeing readonly fields as well. */ public function updateCMSFields(FieldList $fields) { $this->addTranslatableFields($fields); // Show a dropdown to create a new translation. // This action is possible both when showing the "default language" // and a translation. Include the current locale (record might not be saved yet). $alreadyTranslatedLocales = $this->getTranslatedLocales(); $alreadyTranslatedLocales[$this->owner->Locale] = $this->owner->Locale; $alreadyTranslatedLocales = array_combine($alreadyTranslatedLocales, $alreadyTranslatedLocales); // Check if fields exist already to avoid adding them twice on repeat invocations $tab = $fields->findOrMakeTab('Root.Translations', _t('Translatable.TRANSLATIONS', 'Translations')); if (!$tab->fieldByName('CreateTransHeader')) { $tab->push(new HeaderField('CreateTransHeader', _t('Translatable.CREATE', 'Create new translation'), 2)); } if (!$tab->fieldByName('NewTransLang') && !$tab->fieldByName('AllTransCreated')) { $langDropdown = LanguageDropdownField::create("NewTransLang", _t('Translatable.NEWLANGUAGE', 'New language'), $alreadyTranslatedLocales, 'SiteTree', 'Locale-English', $this->owner)->addExtraClass('languageDropdown no-change-track'); $tab->push($langDropdown); $canAddLocale = count($langDropdown->getSource()) > 0; if ($canAddLocale) { // Only add create button if new languages are available $tab->push($createButton = InlineFormAction::create('createtranslation', _t('Translatable.CREATEBUTTON', 'Create'))->addExtraClass('createTranslationButton')); $createButton->includeDefaultJS(false); // not fluent API... } else { $tab->removeByName('NewTransLang'); $tab->push(new LiteralField('AllTransCreated', _t('Translatable.ALLCREATED', 'All allowed translations have been created.'))); } } if ($alreadyTranslatedLocales) { if (!$tab->fieldByName('ExistingTransHeader')) { $tab->push(new HeaderField('ExistingTransHeader', _t('Translatable.EXISTING', 'Existing translations'), 3)); if (!$tab->fieldByName('existingtrans')) { $existingTransHTML = '<ul>'; if ($existingTranslations = $this->getTranslations()) { foreach ($existingTranslations as $existingTranslation) { if ($existingTranslation && $existingTranslation->hasMethod('CMSEditLink')) { $existingTransHTML .= sprintf('<li><a href="%s">%s</a></li>', Controller::join_links($existingTranslation->CMSEditLink(), '?Locale=' . $existingTranslation->Locale), i18n::get_locale_name($existingTranslation->Locale)); } } } $existingTransHTML .= '</ul>'; $tab->push(new LiteralField('existingtrans', $existingTransHTML)); } } } }