function locale_translation_batch_fetch_import

Same name in other branches
  1. 8.9.x core/modules/locale/locale.batch.inc \locale_translation_batch_fetch_import()
  2. 10 core/modules/locale/locale.batch.inc \locale_translation_batch_fetch_import()
  3. 11.x core/modules/locale/locale.batch.inc \locale_translation_batch_fetch_import()

Implements callback_batch_operation().

Imports a gettext file from the translation directory. When successfully the translation status is updated.

Parameters

object $project: Source object of the translatable project.

string $langcode: Language code.

array $options: Array of import options.

array $context: The batch context.

See also

locale_translate_batch_import_files()

locale_translation_batch_fetch_download()

2 string references to 'locale_translation_batch_fetch_import'
install_import_translations in core/includes/install.core.inc
Imports languages via a batch process during installation.
_locale_translation_fetch_operations in core/modules/locale/locale.fetch.inc
Helper function to construct the batch operations to fetch translations.

File

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

Code

function locale_translation_batch_fetch_import($project, $langcode, $options, &$context) {
    $sources = locale_translation_get_status([
        $project,
    ], [
        $langcode,
    ]);
    if (isset($sources[$project][$langcode])) {
        $source = $sources[$project][$langcode];
        if (isset($source->type)) {
            if ($source->type == LOCALE_TRANSLATION_REMOTE || $source->type == LOCALE_TRANSLATION_LOCAL) {
                $file = $source->files[LOCALE_TRANSLATION_LOCAL];
                \Drupal::moduleHandler()->loadInclude('locale', 'inc', 'locale.bulk');
                $options += [
                    'message' => t('Importing %langcode translation for %project.', [
                        '%langcode' => $langcode,
                        '%project' => $source->project,
                    ]),
                ];
                // Import the translation file. For large files the batch operations is
                // progressive and will be called repeatedly until finished.
                locale_translate_batch_import($file, $options, $context);
                // The import is finished.
                if (isset($context['finished']) && $context['finished'] == 1) {
                    // The import is successful.
                    if (isset($context['results']['files'][$file->uri])) {
                        $context['message'] = t('Imported %langcode translation for %project.', [
                            '%langcode' => $langcode,
                            '%project' => $source->project,
                        ]);
                        // Save the data of imported source into the {locale_file} table and
                        // update the current translation status.
                        locale_translation_status_save($project, $langcode, LOCALE_TRANSLATION_CURRENT, $source->files[LOCALE_TRANSLATION_LOCAL]);
                    }
                }
            }
        }
    }
}

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