annotate core/modules/locale/locale.fetch.inc @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /**
Chris@0 4 * @file
Chris@0 5 * The API for download and import of translations from remote and local sources.
Chris@0 6 */
Chris@0 7
Chris@0 8 /**
Chris@0 9 * Load the common translation API.
Chris@0 10 */
Chris@0 11 // @todo Combine functions differently in files to avoid unnecessary includes.
Chris@0 12 // Follow-up issue: https://www.drupal.org/node/1834298.
Chris@0 13 require_once __DIR__ . '/locale.translation.inc';
Chris@0 14
Chris@0 15 /**
Chris@0 16 * Builds a batch to check, download and import project translations.
Chris@0 17 *
Chris@0 18 * @param array $projects
Chris@0 19 * Array of project names for which to update the translations. Defaults to
Chris@0 20 * all translatable projects.
Chris@0 21 * @param array $langcodes
Chris@0 22 * Array of language codes. Defaults to all translatable languages.
Chris@0 23 * @param array $options
Chris@0 24 * Array of import options. See locale_translate_batch_import_files().
Chris@0 25 *
Chris@0 26 * @return array
Chris@0 27 * Batch definition array.
Chris@0 28 */
Chris@0 29 function locale_translation_batch_update_build($projects = [], $langcodes = [], $options = []) {
Chris@0 30 module_load_include('compare.inc', 'locale');
Chris@0 31 $projects = $projects ? $projects : array_keys(locale_translation_get_projects());
Chris@0 32 $langcodes = $langcodes ? $langcodes : array_keys(locale_translatable_language_list());
Chris@0 33 $status_options = $options;
Chris@0 34 $status_options['finish_feedback'] = FALSE;
Chris@0 35
Chris@0 36 // Check status of local and remote translation files.
Chris@0 37 $operations = _locale_translation_batch_status_operations($projects, $langcodes, $status_options);
Chris@0 38 // Download and import translations.
Chris@0 39 $operations = array_merge($operations, _locale_translation_fetch_operations($projects, $langcodes, $options));
Chris@0 40
Chris@0 41 $batch = [
Chris@0 42 'operations' => $operations,
Chris@0 43 'title' => t('Updating translations'),
Chris@0 44 'progress_message' => '',
Chris@0 45 'error_message' => t('Error importing translation files'),
Chris@0 46 'finished' => 'locale_translation_batch_fetch_finished',
Chris@0 47 'file' => drupal_get_path('module', 'locale') . '/locale.batch.inc',
Chris@0 48 ];
Chris@0 49 return $batch;
Chris@0 50 }
Chris@0 51
Chris@0 52 /**
Chris@0 53 * Builds a batch to download and import project translations.
Chris@0 54 *
Chris@0 55 * @param array $projects
Chris@0 56 * Array of project names for which to check the state of translation files.
Chris@0 57 * Defaults to all translatable projects.
Chris@0 58 * @param array $langcodes
Chris@0 59 * Array of language codes. Defaults to all translatable languages.
Chris@0 60 * @param array $options
Chris@0 61 * Array of import options. See locale_translate_batch_import_files().
Chris@0 62 *
Chris@0 63 * @return array
Chris@0 64 * Batch definition array.
Chris@0 65 */
Chris@0 66 function locale_translation_batch_fetch_build($projects = [], $langcodes = [], $options = []) {
Chris@0 67 $projects = $projects ? $projects : array_keys(locale_translation_get_projects());
Chris@0 68 $langcodes = $langcodes ? $langcodes : array_keys(locale_translatable_language_list());
Chris@0 69
Chris@0 70 $batch = [
Chris@0 71 'operations' => _locale_translation_fetch_operations($projects, $langcodes, $options),
Chris@0 72 'title' => t('Updating translations.'),
Chris@0 73 'progress_message' => '',
Chris@0 74 'error_message' => t('Error importing translation files'),
Chris@0 75 'finished' => 'locale_translation_batch_fetch_finished',
Chris@0 76 'file' => drupal_get_path('module', 'locale') . '/locale.batch.inc',
Chris@0 77 ];
Chris@0 78 return $batch;
Chris@0 79 }
Chris@0 80
Chris@0 81 /**
Chris@0 82 * Helper function to construct the batch operations to fetch translations.
Chris@0 83 *
Chris@0 84 * @param array $projects
Chris@0 85 * Array of project names for which to check the state of translation files.
Chris@0 86 * Defaults to all translatable projects.
Chris@0 87 * @param array $langcodes
Chris@0 88 * Array of language codes. Defaults to all translatable languages.
Chris@0 89 * @param array $options
Chris@0 90 * Array of import options.
Chris@0 91 *
Chris@0 92 * @return array
Chris@0 93 * Array of batch operations.
Chris@0 94 */
Chris@0 95 function _locale_translation_fetch_operations($projects, $langcodes, $options) {
Chris@0 96 $operations = [];
Chris@0 97
Chris@0 98 foreach ($projects as $project) {
Chris@0 99 foreach ($langcodes as $langcode) {
Chris@0 100 if (locale_translation_use_remote_source()) {
Chris@0 101 $operations[] = ['locale_translation_batch_fetch_download', [$project, $langcode]];
Chris@0 102 }
Chris@0 103 $operations[] = ['locale_translation_batch_fetch_import', [$project, $langcode, $options]];
Chris@0 104 }
Chris@0 105 }
Chris@0 106
Chris@0 107 return $operations;
Chris@0 108 }