locale.fetch.inc 3.8 KB

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