function LocaleFileManager::downloadTranslationSource

Downloads a translation file from a remote server.

Parameters

LocaleFile $source_file: Source file object with at least:

  • "uri": uri to download the file from.
  • "project": Project name.
  • "langcode": Translation language.
  • "version": Project version.
  • "filename": File name.

string $directory: Directory where the downloaded file will be saved. Defaults to the temporary file path.

Return value

LocaleFile|false File object if download was successful. FALSE on failure.

File

core/modules/locale/src/File/LocaleFileManager.php, line 194

Class

LocaleFileManager
Provide Locale File helper methods.

Namespace

Drupal\locale\File

Code

public function downloadTranslationSource(LocaleFile $source_file, string $directory = 'translations://') : LocaleFile|false {
  try {
    $data = (string) $this->httpClient
      ->request('get', $source_file->uri)
      ->getBody();
    $filename = basename($source_file->uri);
    if ($uri = $this->fileSystem
      ->saveData($data, $directory . $filename, FileExists::Replace)) {
      $hash = hash_file(LocaleSource::LOCAL_FILE_HASH_ALGO, $uri);
      $langcode = $source_file->langcode ?? LanguageInterface::LANGCODE_NOT_SPECIFIED;
      $project = $source_file->project ?? NULL;
      $version = $source_file->version ?? NULL;
      $file = new LocaleFile($filename, $uri, $hash, filemtime($uri), $langcode, $project, $version);
      $file->type = LOCALE_TRANSLATION_LOCAL;
      $file->directory = $directory;
      return $file;
    }
  } catch (ClientExceptionInterface $exception) {
    $this->messenger
      ->addError($this->t('Failed to fetch file due to error "%error"', [
      '%error' => $exception->getMessage(),
    ]));
  } catch (FileException|InvalidStreamWrapperException $e) {
    $this->messenger
      ->addError($this->t('Failed to save file due to error "%error"', [
      '%error' => $e->getMessage(),
    ]));
  }
  $this->loggerFactory
    ->get('locale')
    ->error('Unable to download translation file @uri.', [
    '@uri' => $source_file->uri,
  ]);
  return FALSE;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.