-
Notifications
You must be signed in to change notification settings - Fork 2
/
l10n_update.fetch.inc
133 lines (121 loc) · 4.69 KB
/
l10n_update.fetch.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
/**
* @file
* The API for download and import of translations.
*/
/**
* Load the common translation API.
*/
// @todo Combine functions differently in files to avoid unnecessary includes.
// Follow-up issue https://www.drupal.org/node/1834298
require_once __DIR__ . '/l10n_update.translation.inc';
/**
* Builds a batch to check, download and import project translations.
*
* @param array $projects
* Array of project names for which to update the translations. Defaults to
* all translatable projects.
* @param array $langcodes
* Array of language codes. Defaults to all translatable languages.
* @param array $options
* Array of import options. See locale_translate_batch_import_files().
*
* @return array
* Batch definition array.
*/
function l10n_update_batch_update_build($projects = array(), $langcodes = array(), $options = array()) {
module_load_include('compare.inc', 'l10n_update');
$projects = $projects ? $projects : array_keys(l10n_update_get_projects());
$langcodes = $langcodes ? $langcodes : array_keys(l10n_update_translatable_language_list());
$status_options = $options;
$status_options['finish_feedback'] = FALSE;
// Check status of local and remote translation files.
$operations = _l10n_update_batch_status_operations($projects, $langcodes, $status_options);
// Download and import translations.
$operations = array_merge($operations, _l10n_update_fetch_operations($projects, $langcodes, $options));
$batch = array(
'operations' => $operations,
'title' => t('Updating translations'),
'progress_message' => '',
'error_message' => t('Error importing translation files'),
'finished' => 'l10n_update_batch_fetch_finished',
'file' => backdrop_get_path('module', 'l10n_update') . '/l10n_update.batch.inc',
);
return $batch;
}
/**
* Builds a batch to download and import project translations.
*
* @param array $projects
* Array of project names for which to check the state of translation files.
* Defaults to all translatable projects.
* @param array $langcodes
* Array of language codes. Defaults to all translatable languages.
* @param array $options
* Array of import options. See l10n_update_batch_import_files().
*
* @return array
* Batch definition array.
*/
function l10n_update_batch_fetch_build($projects = array(), $langcodes = array(), $options = array()) {
$projects = $projects ? $projects : array_keys(l10n_update_get_projects());
$langcodes = $langcodes ? $langcodes : array_keys(l10n_update_translatable_language_list());
$batch = array(
'operations' => _l10n_update_fetch_operations($projects, $langcodes, $options),
'title' => t('Updating translations.'),
'progress_message' => '',
'error_message' => t('Error importing translation files'),
'finished' => 'l10n_update_batch_fetch_finished',
'file' => backdrop_get_path('module', 'l10n_update') . '/l10n_update.batch.inc',
);
return $batch;
}
/**
* Helper function to construct the batch operations to fetch translations.
*
* @param array $projects
* Array of project names for which to check the state of translation files.
* Defaults to all translatable projects.
* @param array $langcodes
* Array of language codes. Defaults to all translatable languages.
* @param array $options
* Array of import options.
*
* @return array
* Array of batch operations.
*/
function _l10n_update_fetch_operations(array $projects, array $langcodes, array $options) {
$operations = array();
// If the process is going to download files from a remote, make sure the
// files can be saved in the download directory.
if (!empty($projects) && l10n_update_use_remote_source()) {
$directory = config_get('l10n_update.settings', 'l10n_update_download_store');
// Do not download files if the directory is missing or can not be created.
if (!file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
backdrop_set_message(t('The directory %directory does not exist or is not writable.', array('%directory' => $directory)), 'error');
watchdog('file system', 'The directory %directory does not exist or is not writable.', array('%directory' => $directory), WATCHDOG_ERROR);
return $operations;
}
l10n_update_ensure_htaccess();
}
foreach ($projects as $project) {
foreach ($langcodes as $langcode) {
if (l10n_update_use_remote_source()) {
$operations[] = array(
'l10n_update_batch_fetch_download',
array(
$project,
$langcode,
),
);
}
$operations[] = array('l10n_update_batch_fetch_import', array(
$project,
$langcode,
$options,
),
);
}
}
return $operations;
}