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