public function Data::createFileTranslation

Creates a translation for a source file.

Parameters

\Drupal\file\FileInterface $source_file: The source file.

string $target_langcode: The language code of the translation.

string $translation_content: The transation content.

string|NULL $destination: The destination, defaults to the same directory and filename, with a langcode suffix.

Return value

\Drupal\file\FileInterface The translated file entity.

File

src/Data.php, line 313

Class

Data
All data-related functions.

Namespace

Drupal\tmgmt

Code

public function createFileTranslation(FileInterface $source_file, string $target_langcode, string $translation_content, string $destination = NULL) : FileInterface {

  /** @var \Drupal\file\FileRepositoryInterface $file_repository */
  $file_repository = \Drupal::service('file.repository');

  /** @var \Drupal\Core\File\FileSystemInterface $file_system */
  $file_system = \Drupal::service('file_system');
  if (!$destination) {
    $filename = pathinfo($source_file
      ->getFilename(), PATHINFO_FILENAME);
    $extension = pathinfo($source_file
      ->getFilename(), PATHINFO_EXTENSION);
    $destination = $file_system
      ->dirname($source_file
      ->getFileUri()) . '/' . $filename . '_' . $target_langcode . '.' . $extension;
    $destination = \Drupal::service('stream_wrapper_manager')
      ->normalizeUri($destination);
  }
  return $file_repository
    ->writeData($translation_content, $destination);
}