/** * Form constructor for the translation import screen. */ public function buildForm(array $form, FormStateInterface $form_state) { $languages = $this->languageManager->getLanguages(); // Initialize a language list to the ones available, including English if we // are to translate Drupal to English as well. $existing_languages = array(); foreach ($languages as $langcode => $language) { if (locale_is_translatable($langcode)) { $existing_languages[$langcode] = $language->getName(); } } // If we have no languages available, present the list of predefined // languages only. If we do have already added languages, set up two option // groups with the list of existing and then predefined languages. if (empty($existing_languages)) { $language_options = $this->languageManager->getStandardLanguageListWithoutConfigured(); $default = key($language_options); } else { $default = key($existing_languages); $language_options = array((string) $this->t('Existing languages') => $existing_languages, (string) $this->t('Languages not yet added') => $this->languageManager->getStandardLanguageListWithoutConfigured()); } $validators = array('file_validate_extensions' => array('po'), 'file_validate_size' => array(file_upload_max_size())); $form['file'] = array('#type' => 'file', '#title' => $this->t('Translation file'), '#description' => array('#theme' => 'file_upload_help', '#description' => $this->t('A Gettext Portable Object file.'), '#upload_validators' => $validators), '#size' => 50, '#upload_validators' => $validators, '#attributes' => array('class' => array('file-import-input'))); $form['langcode'] = array('#type' => 'select', '#title' => $this->t('Language'), '#options' => $language_options, '#default_value' => $default, '#attributes' => array('class' => array('langcode-input'))); $form['customized'] = array('#title' => $this->t('Treat imported strings as custom translations'), '#type' => 'checkbox'); $form['overwrite_options'] = array('#type' => 'container', '#tree' => TRUE); $form['overwrite_options']['not_customized'] = array('#title' => $this->t('Overwrite non-customized translations'), '#type' => 'checkbox', '#states' => array('checked' => array(':input[name="customized"]' => array('checked' => TRUE)))); $form['overwrite_options']['customized'] = array('#title' => $this->t('Overwrite existing customized translations'), '#type' => 'checkbox'); $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array('#type' => 'submit', '#value' => $this->t('Import')); return $form; }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $languages = $this->languageManager->getLanguages(); $language_options = array(); foreach ($languages as $langcode => $language) { if (locale_is_translatable($langcode)) { $language_options[$langcode] = $language->getName(); } } $language_default = $this->languageManager->getDefaultLanguage(); if (empty($language_options)) { $form['langcode'] = array('#type' => 'value', '#value' => LanguageInterface::LANGCODE_SYSTEM); $form['langcode_text'] = array('#type' => 'item', '#title' => $this->t('Language'), '#markup' => $this->t('No language available. The export will only contain source strings.')); } else { $form['langcode'] = array('#type' => 'select', '#title' => $this->t('Language'), '#options' => $language_options, '#default_value' => $language_default->getId(), '#empty_option' => $this->t('Source text only, no translations'), '#empty_value' => LanguageInterface::LANGCODE_SYSTEM); $form['content_options'] = array('#type' => 'details', '#title' => $this->t('Export options'), '#collapsed' => TRUE, '#tree' => TRUE, '#states' => array('invisible' => array(':input[name="langcode"]' => array('value' => LanguageInterface::LANGCODE_SYSTEM)))); $form['content_options']['not_customized'] = array('#type' => 'checkbox', '#title' => $this->t('Include non-customized translations'), '#default_value' => TRUE); $form['content_options']['customized'] = array('#type' => 'checkbox', '#title' => $this->t('Include customized translations'), '#default_value' => TRUE); $form['content_options']['not_translated'] = array('#type' => 'checkbox', '#title' => $this->t('Include untranslated text'), '#default_value' => TRUE); } $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array('#type' => 'submit', '#value' => $this->t('Export')); return $form; }
/** * Lists locale translation filters that can be applied. */ protected function translateFilters() { $filters = array(); // Get all languages, except English. $this->languageManager->reset(); $languages = $this->languageManager->getLanguages(); $language_options = array(); foreach ($languages as $langcode => $language) { if (locale_is_translatable($langcode)) { $language_options[$langcode] = $language->getName(); } } // Pick the current interface language code for the filter. $default_langcode = $this->languageManager->getCurrentLanguage()->getId(); if (!isset($language_options[$default_langcode])) { $available_langcodes = array_keys($language_options); $default_langcode = array_shift($available_langcodes); } $filters['string'] = array('title' => $this->t('String contains'), 'description' => $this->t('Leave blank to show all strings. The search is case sensitive.'), 'default' => ''); $filters['langcode'] = array('title' => $this->t('Translation language'), 'options' => $language_options, 'default' => $default_langcode); $filters['translation'] = array('title' => $this->t('Search in'), 'options' => array('all' => $this->t('Both translated and untranslated strings'), 'translated' => $this->t('Only translated strings'), 'untranslated' => $this->t('Only untranslated strings')), 'default' => 'all'); $filters['customized'] = array('title' => $this->t('Translation type'), 'options' => array('all' => $this->t('All'), LOCALE_NOT_CUSTOMIZED => $this->t('Non-customized translation'), LOCALE_CUSTOMIZED => $this->t('Customized translation')), 'states' => array('visible' => array(':input[name=translation]' => array('value' => 'translated'))), 'default' => 'all'); return $filters; }
/** * Update locale storage based on configuration translations. * * @param \Drupal\Core\Config\StorableConfigBase $config * Active configuration or configuration translation override. * @param string $langcode * The language code of $config. * @param array $reference_config * (Optional) Reference configuration to check against if $config was an * override. This allows us to update locale keys for data not in the * override but still in the active configuration. */ protected function updateLocaleStorage(StorableConfigBase $config, $langcode, array $reference_config = array()) { $name = $config->getName(); if ($this->localeConfigManager->isSupported($name) && locale_is_translatable($langcode)) { $translatables = $this->localeConfigManager->getTranslatableDefaultConfig($name); $this->processTranslatableData($name, $config->get(), $translatables, $langcode, $reference_config); } }
/** * Updates all configuration translations for the names / languages provided. * * To be used when interface translation changes result in the need to update * configuration translations to keep them in sync. * * @param array $names * Array of names of configuration objects to update. * @param array $langcodes * (optional) Array of language codes to update. Defaults to all * configurable languages. * * @return int * Total number of configuration override and active configuration objects * updated (saved or removed). */ public function updateConfigTranslations(array $names, array $langcodes = array()) { $langcodes = $langcodes ? $langcodes : array_keys($this->languageManager->getLanguages()); $count = 0; foreach ($names as $name) { $translatable = $this->getTranslatableDefaultConfig($name); if (empty($translatable)) { // If there is nothing translatable in this configuration or not // supported, skip it. continue; } $active_langcode = $this->getActiveConfigLangcode($name); $active = $this->configStorage->read($name); foreach ($langcodes as $langcode) { $processed = $this->processTranslatableData($name, $active, $translatable, $langcode); // If the language code is not the same as the active storage // language, we should update the configuration override. if ($langcode != $active_langcode) { $override = $this->languageManager->getLanguageConfigOverride($langcode, $name); // Filter out locale managed configuration keys so that translations // removed from Locale will be reflected in the config override. $data = $this->filterOverride($override->get(), $translatable); if (!empty($processed)) { // Merge in the Locale managed translations with existing data. $data = NestedArray::mergeDeepArray(array($data, $processed), TRUE); } if (empty($data) && !$override->isNew()) { // The configuration override contains Locale overrides that no // longer exist. $this->deleteTranslationOverride($name, $langcode); $count++; } elseif (!empty($data)) { // Update translation data in configuration override. $this->saveTranslationOverride($name, $langcode, $data); $count++; } } elseif (locale_is_translatable($langcode)) { // If the language code is the active storage language, we should // update. If it is English, we should only update if English is also // translatable. $active = NestedArray::mergeDeepArray(array($active, $processed), TRUE); $this->saveTranslationActive($name, $active); $count++; } } } return $count; }
/** * Updates all configuration translations for the names / languages provided. * * To be used when interface translation changes result in the need to update * configuration translations to keep them in sync. * * @param array $names * Array of names of configuration objects to update. * @param array $langcodes * (optional) Array of language codes to update. Defaults to all * configurable languages. * * @return int * Total number of configuration override and active configuration objects * updated (saved or removed). */ public function updateConfigTranslations(array $names, array $langcodes = array()) { $langcodes = $langcodes ? $langcodes : array_keys($this->languageManager->getLanguages()); $count = 0; foreach ($names as $name) { $translatable = $this->getTranslatableDefaultConfig($name); if (empty($translatable)) { // If there is nothing translatable in this configuration or not // supported, skip it. continue; } $active_langcode = $this->getActiveConfigLangcode($name); $active = $this->configStorage->read($name); foreach ($langcodes as $langcode) { $processed = $this->processTranslatableData($name, $active, $translatable, $langcode); if ($langcode != $active_langcode) { // If the language code is not the same as the active storage // language, we should update a configuration override. if (!empty($processed)) { // Update translation data in configuration override. $this->saveTranslationOverride($name, $langcode, $processed); $count++; } else { $override = $this->languageManager->getLanguageConfigOverride($langcode, $name); if (!$override->isNew()) { $data = $this->filterOverride($override->get(), $translatable); if (empty($data)) { // Delete language override if there is no data left at all. // This means all prior translations in the override were locale // managed. $this->deleteTranslationOverride($name, $langcode); $count++; } else { // If there were translatable elements besides locale managed // items, save with only those, and remove the ones managed // by locale only. $this->saveTranslationOverride($name, $langcode, $data); $count++; } } } } elseif (locale_is_translatable($langcode)) { // If the language code is the active storage language, we should // update. If it is English, we should only update if English is also // translatable. $active = NestedArray::mergeDeepArray(array($active, $processed), TRUE); $this->saveTranslationActive($name, $active); $count++; } } } return $count; }