5 * Common API for interface translation.
9 * Comparison result of source files timestamps.
11 * Timestamp of source 1 is less than the timestamp of source 2.
13 * @see _locale_translation_source_compare()
15 const LOCALE_TRANSLATION_SOURCE_COMPARE_LT = -1;
18 * Comparison result of source files timestamps.
20 * Timestamp of source 1 is equal to the timestamp of source 2.
22 * @see _locale_translation_source_compare()
24 const LOCALE_TRANSLATION_SOURCE_COMPARE_EQ = 0;
27 * Comparison result of source files timestamps.
29 * Timestamp of source 1 is greater than the timestamp of source 2.
31 * @see _locale_translation_source_compare()
33 const LOCALE_TRANSLATION_SOURCE_COMPARE_GT = 1;
36 * Get array of projects which are available for interface translation.
38 * This project data contains all projects which will be checked for available
39 * interface translations.
41 * For full functionality this function depends on Update module.
42 * When Update module is enabled the project data will contain the most recent
43 * module status; both in enabled status as in version. When Update module is
44 * disabled this function will return the last known module state. The status
45 * will only be updated once Update module is enabled.
47 * @param array $project_names
48 * Array of names of the projects to get.
51 * Array of project data for translation update.
53 * @see locale_translation_build_projects()
55 function locale_translation_get_projects(array $project_names = []) {
56 $projects = &drupal_static(__FUNCTION__, []);
58 if (empty($projects)) {
59 // Get project data from the database.
60 $row_count = \Drupal::service('locale.project')->countProjects();
61 // https://www.drupal.org/node/1777106 is a follow-up issue to make the
62 // check for possible out-of-date project information more robust.
63 if ($row_count == 0) {
64 module_load_include('compare.inc', 'locale');
65 // At least the core project should be in the database, so we build the
66 // data if none are found.
67 locale_translation_build_projects();
69 $projects = \Drupal::service('locale.project')->getAll();
70 array_walk($projects, function (&$project) {
71 $project = (object) $project;
75 // Return the requested project names or all projects.
77 return array_intersect_key($projects, array_combine($project_names, $project_names));
83 * Clears the projects cache.
85 function locale_translation_clear_cache_projects() {
86 drupal_static_reset('locale_translation_get_projects');
90 * Loads cached translation sources containing current translation status.
92 * @param array $projects
93 * Array of project names. Defaults to all translatable projects.
94 * @param array $langcodes
95 * Array of language codes. Defaults to all translatable languages.
98 * Array of source objects. Keyed with <project name>:<language code>.
100 * @see locale_translation_source_build()
102 function locale_translation_load_sources(array $projects = NULL, array $langcodes = NULL) {
104 $projects = $projects ? $projects : array_keys(locale_translation_get_projects());
105 $langcodes = $langcodes ? $langcodes : array_keys(locale_translatable_language_list());
107 // Load source data from locale_translation_status cache.
108 $status = locale_translation_get_status();
110 // Use only the selected projects and languages for update.
111 foreach ($projects as $project) {
112 foreach ($langcodes as $langcode) {
113 $sources[$project][$langcode] = isset($status[$project][$langcode]) ? $status[$project][$langcode] : NULL;
120 * Build translation sources.
122 * @param array $projects
123 * Array of project names. Defaults to all translatable projects.
124 * @param array $langcodes
125 * Array of language codes. Defaults to all translatable languages.
128 * Array of source objects. Keyed by project name and language code.
130 * @see locale_translation_source_build()
132 function locale_translation_build_sources(array $projects = [], array $langcodes = []) {
134 $projects = locale_translation_get_projects($projects);
135 $langcodes = $langcodes ? $langcodes : array_keys(locale_translatable_language_list());
137 foreach ($projects as $project) {
138 foreach ($langcodes as $langcode) {
139 $source = locale_translation_source_build($project, $langcode);
140 $sources[$source->name][$source->langcode] = $source;
147 * Checks whether a po file exists in the local filesystem.
149 * It will search in the directory set in the translation source. Which defaults
150 * to the "translations://" stream wrapper path. The directory may contain any
151 * valid stream wrapper.
153 * The "local" files property of the source object contains the definition of a
154 * po file we are looking for. The file name defaults to
155 * %project-%version.%language.po. Per project this value can be overridden
156 * using the server_pattern directive in the module's .info.yml file or by using
157 * hook_locale_translation_projects_alter().
159 * @param object $source
160 * Translation source object.
163 * Source file object of the po file, updated with:
164 * - "uri": File name and path.
165 * - "timestamp": Last updated time of the po file.
166 * FALSE if the file is not found.
168 * @see locale_translation_source_build()
170 function locale_translation_source_check_file($source) {
171 if (isset($source->files[LOCALE_TRANSLATION_LOCAL])) {
172 $source_file = $source->files[LOCALE_TRANSLATION_LOCAL];
173 $directory = $source_file->directory;
174 $filename = '/' . preg_quote($source_file->filename) . '$/';
176 if ($files = file_scan_directory($directory, $filename, ['key' => 'name', 'recurse' => FALSE])) {
177 $file = current($files);
178 $source_file->uri = $file->uri;
179 $source_file->timestamp = filemtime($file->uri);
187 * Builds abstract translation source.
189 * @param object $project
191 * @param string $langcode
193 * @param string $filename
194 * (optional) File name of translation file. May contain placeholders.
195 * Defaults to the default translation filename from the settings.
199 * - "project": Project name.
200 * - "name": Project name (inherited from project).
201 * - "language": Language code.
202 * - "core": Core version (inherited from project).
203 * - "version": Project version (inherited from project).
204 * - "project_type": Project type (inherited from project).
205 * - "files": Array of file objects containing properties of local and remote
207 * Other processes can add the following properties:
208 * - "type": Most recent translation source found. LOCALE_TRANSLATION_REMOTE
209 * and LOCALE_TRANSLATION_LOCAL indicate available new translations,
210 * LOCALE_TRANSLATION_CURRENT indicate that the current translation is them
211 * most recent. "type" corresponds with a key of the "files" array.
212 * - "timestamp": The creation time of the "type" translation (file).
213 * - "last_checked": The time when the "type" translation was last checked.
214 * The "files" array can hold file objects of type:
215 * LOCALE_TRANSLATION_LOCAL, LOCALE_TRANSLATION_REMOTE and
216 * LOCALE_TRANSLATION_CURRENT. Each contains following properties:
217 * - "type": The object type (LOCALE_TRANSLATION_LOCAL,
218 * LOCALE_TRANSLATION_REMOTE, etc. see above).
219 * - "project": Project name.
220 * - "langcode": Language code.
221 * - "version": Project version.
222 * - "uri": Local or remote file path.
223 * - "directory": Directory of the local po file.
224 * - "filename": File name.
225 * - "timestamp": Timestamp of the file.
226 * - "keep": TRUE to keep the downloaded file.
228 function locale_translation_source_build($project, $langcode, $filename = NULL) {
229 // Follow-up issue: https://www.drupal.org/node/1842380.
230 // Convert $source object to a TranslatableProject class and use a typed class
233 // Create a source object with data of the project object.
234 $source = clone $project;
235 $source->project = $project->name;
236 $source->langcode = $langcode;
238 $source->timestamp = 0;
239 $source->last_checked = 0;
241 $filename = $filename ? $filename : \Drupal::config('locale.settings')->get('translation.default_filename');
243 // If the server_pattern contains a remote file path we will check for a
244 // remote file. The local version of this file will only be checked if a
245 // translations directory has been defined. If the server_pattern is a local
246 // file path we will only check for a file in the local file system.
248 if (_locale_translation_file_is_remote($source->server_pattern)) {
249 $files[LOCALE_TRANSLATION_REMOTE] = (object) [
250 'project' => $project->name,
251 'langcode' => $langcode,
252 'version' => $project->version,
253 'type' => LOCALE_TRANSLATION_REMOTE,
254 'filename' => locale_translation_build_server_pattern($source, basename($source->server_pattern)),
255 'uri' => locale_translation_build_server_pattern($source, $source->server_pattern),
257 $files[LOCALE_TRANSLATION_LOCAL] = (object) [
258 'project' => $project->name,
259 'langcode' => $langcode,
260 'version' => $project->version,
261 'type' => LOCALE_TRANSLATION_LOCAL,
262 'filename' => locale_translation_build_server_pattern($source, $filename),
263 'directory' => 'translations://',
265 $files[LOCALE_TRANSLATION_LOCAL]->uri = $files[LOCALE_TRANSLATION_LOCAL]->directory . $files[LOCALE_TRANSLATION_LOCAL]->filename;
268 $files[LOCALE_TRANSLATION_LOCAL] = (object) [
269 'project' => $project->name,
270 'langcode' => $langcode,
271 'version' => $project->version,
272 'type' => LOCALE_TRANSLATION_LOCAL,
273 'filename' => locale_translation_build_server_pattern($source, basename($source->server_pattern)),
274 'directory' => locale_translation_build_server_pattern($source, drupal_dirname($source->server_pattern)),
276 $files[LOCALE_TRANSLATION_LOCAL]->uri = $files[LOCALE_TRANSLATION_LOCAL]->directory . '/' . $files[LOCALE_TRANSLATION_LOCAL]->filename;
278 $source->files = $files;
280 // If this project+language is already translated, we add its status and
281 // update the current translation timestamp and last_updated time. If the
282 // project+language is not translated before, create a new record.
283 $history = locale_translation_get_file_history();
284 if (isset($history[$project->name][$langcode]) && $history[$project->name][$langcode]->timestamp) {
285 $source->files[LOCALE_TRANSLATION_CURRENT] = $history[$project->name][$langcode];
286 $source->type = LOCALE_TRANSLATION_CURRENT;
287 $source->timestamp = $history[$project->name][$langcode]->timestamp;
288 $source->last_checked = $history[$project->name][$langcode]->last_checked;
291 locale_translation_update_file_history($source);
298 * Build path to translation source, out of a server path replacement pattern.
300 * @param object $project
301 * Project object containing data to be inserted in the template.
302 * @param string $template
303 * String containing placeholders. Available placeholders:
304 * - "%project": Project name.
305 * - "%version": Project version.
306 * - "%core": Project core version.
307 * - "%language": Language code.
310 * String with replaced placeholders.
312 function locale_translation_build_server_pattern($project, $template) {
314 '%project' => $project->name,
315 '%version' => $project->version,
316 '%core' => $project->core,
317 '%language' => isset($project->langcode) ? $project->langcode : '%language',
319 return strtr($template, $variables);
323 * Populate a queue with project to check for translation updates.
325 function locale_cron_fill_queue() {
327 $config = \Drupal::config('locale.settings');
329 // Determine which project+language should be updated.
330 $last = REQUEST_TIME - $config->get('translation.update_interval_days') * 3600 * 24;
331 $projects = \Drupal::service('locale.project')->getAll();
332 $projects = array_filter($projects, function ($project) {
333 return $project['status'] == 1;
335 $files = db_select('locale_file', 'f')
336 ->condition('f.project', array_keys($projects), 'IN')
337 ->condition('f.last_checked', $last, '<')
338 ->fields('f', ['project', 'langcode'])
339 ->execute()->fetchAll();
340 foreach ($files as $file) {
341 $updates[$file->project][] = $file->langcode;
343 // Update the last_checked timestamp of the project+language that will
344 // be checked for updates.
345 db_update('locale_file')
346 ->fields(['last_checked' => REQUEST_TIME])
347 ->condition('project', $file->project)
348 ->condition('langcode', $file->langcode)
352 // For each project+language combination a number of tasks are added to
355 module_load_include('fetch.inc', 'locale');
356 $options = _locale_translation_default_update_options();
357 $queue = \Drupal::queue('locale_translation', TRUE);
359 foreach ($updates as $project => $languages) {
360 $batch = locale_translation_batch_update_build([$project], $languages, $options);
361 foreach ($batch['operations'] as $item) {
362 $queue->createItem($item);
369 * Determine if a file is a remote file.
372 * The URI or URI pattern of the file.
375 * TRUE if the $uri is a remote file.
377 function _locale_translation_file_is_remote($uri) {
378 $scheme = file_uri_scheme($uri);
380 return !\Drupal::service('file_system')->realpath($scheme . '://');
386 * Compare two update sources, looking for the newer one.
388 * The timestamp property of the source objects are used to determine which is
391 * @param object $source1
392 * Source object of the first translation source.
393 * @param object $source2
394 * Source object of available update.
397 * - "LOCALE_TRANSLATION_SOURCE_COMPARE_LT": $source1 < $source2 OR $source1
399 * - "LOCALE_TRANSLATION_SOURCE_COMPARE_EQ": $source1 == $source2 OR both
400 * $source1 and $source2 are missing.
401 * - "LOCALE_TRANSLATION_SOURCE_COMPARE_GT": $source1 > $source2 OR $source2
404 function _locale_translation_source_compare($source1, $source2) {
405 if (isset($source1->timestamp) && isset($source2->timestamp)) {
406 if ($source1->timestamp == $source2->timestamp) {
407 return LOCALE_TRANSLATION_SOURCE_COMPARE_EQ;
410 return $source1->timestamp > $source2->timestamp ? LOCALE_TRANSLATION_SOURCE_COMPARE_GT : LOCALE_TRANSLATION_SOURCE_COMPARE_LT;
413 elseif (isset($source1->timestamp) && !isset($source2->timestamp)) {
414 return LOCALE_TRANSLATION_SOURCE_COMPARE_GT;
416 elseif (!isset($source1->timestamp) && isset($source2->timestamp)) {
417 return LOCALE_TRANSLATION_SOURCE_COMPARE_LT;
420 return LOCALE_TRANSLATION_SOURCE_COMPARE_EQ;
425 * Returns default import options for translation update.
428 * Array of translation import options.
430 function _locale_translation_default_update_options() {
431 $config = \Drupal::config('locale.settings');
433 'customized' => LOCALE_NOT_CUSTOMIZED,
434 'overwrite_options' => [
435 'not_customized' => $config->get('translation.overwrite_not_customized'),
436 'customized' => $config->get('translation.overwrite_customized'),
438 'finish_feedback' => TRUE,
439 'use_remote' => locale_translation_use_remote_source(),