function locale_translation_download_source

Same name and namespace in other branches
  1. 9 core/modules/locale/locale.batch.inc \locale_translation_download_source()
  2. 8.9.x core/modules/locale/locale.batch.inc \locale_translation_download_source()
  3. 10 core/modules/locale/locale.batch.inc \locale_translation_download_source()

Downloads a translation file from a remote server.

Parameters

object $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

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

2 calls to locale_translation_download_source()
LocaleTranslationDownloadTest::testUpdateImportSourceRemote in core/modules/locale/tests/src/Functional/LocaleTranslationDownloadTest.php
Tests translation download from remote sources.
locale_translation_batch_fetch_download in core/modules/locale/locale.batch.inc
Implements callback_batch_operation().

File

core/modules/locale/locale.batch.inc, line 342

Code

function locale_translation_download_source($source_file, $directory = 'temporary://') {
    try {
        $data = (string) \Drupal::httpClient()->get($source_file->uri)
            ->getBody();
        
        /** @var \Drupal\Core\File\FileSystemInterface $fileSystem */
        $fileSystem = \Drupal::service('file_system');
        $filename = $fileSystem->basename($source_file->uri);
        if ($uri = $fileSystem->saveData($data, $directory . $filename, FileExists::Replace)) {
            $file = clone $source_file;
            $file->type = LOCALE_TRANSLATION_LOCAL;
            $file->uri = $uri;
            $file->directory = $directory;
            $file->timestamp = filemtime($uri);
            return $file;
        }
    } catch (ClientExceptionInterface $exception) {
        \Drupal::messenger()->addError(t('Failed to fetch file due to error "%error"', [
            '%error' => $exception->getMessage(),
        ]));
    } catch (FileException|InvalidStreamWrapperException $e) {
        \Drupal::messenger()->addError(t('Failed to save file due to error "%error"', [
            '%error' => $e->getMessage(),
        ]));
    }
    \Drupal::logger('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.