Problem/Motivation

Be able to select Same as translation in the Xliff File provider so any existing translation can be included in the xlf file.

This is a variation of #3092262: Include existing translation content in file translator export file that tries to change the config to
- not copy at all (current behaviour of tmgmt file)
- copy the source in any case (current behaviour of tmgmt file)
- copy the translation in any case, with no fallback to the source
- copy the translation with a fallback to the source (3092262)

Then propose an integration with content moderation #2978341: Support pending revisions and accepting translation as a specific moderation state (should be a follow up).

Proposed resolution

Add Same as translation to the existing Empty and Same as source options,
so the current behaviour doesn't change on existing setups and plugins that are extending FileTranslator could provide a distinct configuration.

As it requires to choose between including the source or not in the initial translation, a more flexible approach could be to use checkboxes here. E.g. unchecked = Empty then Same as source and/or Same as translation. So if only Same as translation is checked it should mean that the initial translation might not be included in the source.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

colorfield created an issue. See original summary.

colorfield’s picture

Status: Active » Needs review
FileSize
7.96 KB

Here is a first approach for this, without tests. Presumes that Same as source is the fallback if no translation is available yet.

Status: Needs review » Needs work

The last submitted patch, 2: 3130344-2-configure-xlf-target-content.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

colorfield’s picture

Patch above requires at least to review SourcePluginInterface::getData or perhaps introduce getTranslationData will continue on this a bit later, currently reviewing how to integrate with #2978341: Support pending revisions and accepting translation as a specific moderation state

colorfield’s picture

Issue summary: View changes
colorfield’s picture

About the content moderation integration, if we continue with the initial approach, we could change the ContentEntitySource::loadMultiple() from https://www.drupal.org/project/tmgmt/issues/2978341#comment-13209387
like this to get the latest translated revision, using EntityRepository::getActiveMultiple() with a language context.

public static function loadMultiple($entity_type_id, array $entity_ids, $langcode = NULL) {
    /** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
    $storage = \Drupal::entityTypeManager()->getStorage($entity_type_id);
    // Load the latest revision if the entity type is revisionable.
    if ($storage->getEntityType()->isRevisionable() && $storage instanceof TranslatableRevisionableStorageInterface) {
      /** @var \Drupal\Core\Entity\EntityRepositoryInterface $entityRepository */
      $entityRepository = \Drupal::service('entity.repository');
      if (!is_null($langcode)) {
        $dataType = 'language';
        $contextIdPrefix = '@language.current_language_context:';
        $contexts = [
          $contextIdPrefix . LanguageInterface::TYPE_CONTENT => new Context(new ContextDefinition($dataType), $langcode),
          $contextIdPrefix . LanguageInterface::TYPE_INTERFACE => new Context(new ContextDefinition($dataType), $langcode),
        ];
        $activeVariants = $entityRepository->getActiveMultiple($entity_type_id, $entity_ids, $contexts);
      }
      else {
        $activeVariants = $entityRepository->getActiveMultiple($entity_type_id, $entity_ids);
      }
      foreach ($activeVariants as $variant) {
        $entities[$variant->id()] = $variant;
      }
    }
    else {
      $entities = $storage->loadMultiple($entity_ids);
    }
    return $entities;
}

I will also continue to look a the proposed implementation with checkboxes so the site builder can choose to include the source or not in the initial translation.

colorfield’s picture

Implementation with checkboxes: allows
- empty
- source
- translation
- translation with fallback to source

Also fixes the target configuration that was not displayed on the form after save.

Still requires
- configuration update from single valued to multi valued configuration for 'target' (copy old configuration and initialise new - update or post update hook of the tmgmt_file module)
- tests

colorfield’s picture

Issue summary: View changes
eyilmaz’s picture

Rerolled.
ContentEntitySource::getData ignored the new $langcode parameter. Adjusted that.
Also the schema file for tmgmt_file in tmgmt_file.schema.yml is updated to reflect the new possible values for format_configuration.target.

eyilmaz’s picture

Applied change to SourcePluginInterface::getData also to other inheriting classes.

Berdir’s picture

Status: Needs work » Needs review

Don't forget to set an issue to needs review when uploading a patch, so that tests can run.

That said, this is introducing an API change for several interfaces, that is not something we can do. There are some ways to work around it like not adding it to the interface, but that makes things quite complicated and ugly.

Will need to do a more detailed review.

The last submitted patch, 9: configure-xlf-target-content_3130344-9.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

Status: Needs review » Needs work

The last submitted patch, 10: configure-xlf-target-content_3130344-10.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

GuillaumeG’s picture

Using the patch in production without issues so far, thanks.