function locale_translation_batch_fetch_import

Batch process: Import translation file.

This batch operation imports either a local gettext file or a downloaded remote gettext file. In case of a downloaded file the location of the temporary file is found in the $context['results']['sources']. The temporary file will be deleted after importing or will be moved to the local translations directory. In case of a local file the file will just be imported.

Parameters

object $project: Source object of the translatable project.

string $langcode: Language code.

array $options: Array of import options.

$context: Batch context array.

See also

locale_translate_batch_import_files()

locale_translation_batch_fetch_sources()

locale_translation_batch_fetch_download()

locale_translation_batch_fetch_update_status()

locale_translation_batch_status_compare()

1 string reference to 'locale_translation_batch_fetch_import'
_locale_translation_fetch_operations in drupal/core/modules/locale/locale.fetch.inc
Helper function to construct the batch operations to fetch translations.

File

drupal/core/modules/locale/locale.batch.inc, line 291
Batch process to check the availability of remote or local po files.

Code

function locale_translation_batch_fetch_import($project, $langcode, $options, &$context) {
  $sources = $context['results']['input'];
  if (isset($sources[$project . ':' . $langcode])) {
    $source = $sources[$project . ':' . $langcode];
    if (isset($source->type)) {
      if ($source->type == LOCALE_TRANSLATION_REMOTE || $source->type == LOCALE_TRANSLATION_LOCAL) {
        $t = get_t();

        // If we are working on a remote file we will import the downloaded
        // file. If the file was local just mark the result as such.
        if ($source->type == LOCALE_TRANSLATION_REMOTE) {
          if (isset($context['results']['sources'][$source->project][$source->langcode]->files[LOCALE_TRANSLATION_DOWNLOADED])) {
            $import_type = LOCALE_TRANSLATION_DOWNLOADED;
            $source_result = $context['results']['sources'][$source->project][$source->langcode];
          }
        }
        else {
          $import_type = LOCALE_TRANSLATION_LOCAL;
          $source_result = $source;
        }
        $file = $source_result->files[$import_type];
        module_load_include('bulk.inc', 'locale');
        $options += array(
          'message' => $t('Importing translation for %project.', array(
            '%project' => $source->project,
          )),
        );

        // Import the translation file. For large files the batch operations is
        // progressive and will be called repeatedly untill finished.
        locale_translate_batch_import($file, $options, $context);

        // The import is finished.
        if (isset($context['finished']) && $context['finished'] == 1) {

          // The import is successfull.
          if (isset($context['results']['files'][$file->uri])) {
            $context['message'] = $t('Imported translation for %project.', array(
              '%project' => $source->project,
            ));

            // Keep the data of imported source. In the following batch
            // operation it will be saved in the {locale_file} table.
            $source_result->files[LOCALE_TRANSLATION_IMPORTED] = $source_result->files[$source->type];

            // Downloaded files are stored in the temporary files directory. If
            // files should be kept locally, they will be moved to the local
            // translations after successfull import. Otherwise the temporary
            // file is deleted after being imported.
            if ($import_type == LOCALE_TRANSLATION_DOWNLOADED && config('locale.settings')
              ->get('translation.path') && isset($source_result->files[LOCALE_TRANSLATION_LOCAL])) {
              if (file_unmanaged_move($file->uri, $source_result->files[LOCALE_TRANSLATION_LOCAL]->uri, FILE_EXISTS_REPLACE)) {

                // The downloaded file is now moved to the local file location.
                // From this point forward we can treat it as if we imported a
                // local file.
                $import_type = LOCALE_TRANSLATION_LOCAL;
              }
            }

            // The downloaded file is imported but will not be stored locally.
            // Store the timestamp and delete the file.
            if ($import_type == LOCALE_TRANSLATION_DOWNLOADED) {
              $timestamp = filemtime($source_result->files[$import_type]->uri);
              $source_result->files[LOCALE_TRANSLATION_IMPORTED]->timestamp = $timestamp;
              $source_result->files[LOCALE_TRANSLATION_IMPORTED]->last_checked = REQUEST_TIME;
              file_unmanaged_delete($file->uri);
            }

            // If the translation file is stored in the local directory. The
            // timestamp of the file is stored.
            if ($import_type == LOCALE_TRANSLATION_LOCAL) {
              $timestamp = filemtime($source_result->files[$import_type]->uri);
              $source_result->files[LOCALE_TRANSLATION_LOCAL]->timestamp = $timestamp;
              $source_result->files[LOCALE_TRANSLATION_IMPORTED]->timestamp = $timestamp;
              $source_result->files[LOCALE_TRANSLATION_IMPORTED]->last_checked = REQUEST_TIME;
            }
          }
          else {

            // File import failed. We can delete the temporary file.
            if ($import_type == LOCALE_TRANSLATION_DOWNLOADED) {
              file_unmanaged_delete($file->uri);
            }
          }
        }
        $context['results']['sources'][$source->project][$source->langcode] = $source_result;
      }
    }
  }
}