locale.translation.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. <?php
  2. /**
  3. * @file
  4. * Common API for interface translation.
  5. */
  6. use Drupal\Core\StreamWrapper\StreamWrapperManager;
  7. /**
  8. * Comparison result of source files timestamps.
  9. *
  10. * Timestamp of source 1 is less than the timestamp of source 2.
  11. *
  12. * @see _locale_translation_source_compare()
  13. */
  14. const LOCALE_TRANSLATION_SOURCE_COMPARE_LT = -1;
  15. /**
  16. * Comparison result of source files timestamps.
  17. *
  18. * Timestamp of source 1 is equal to the timestamp of source 2.
  19. *
  20. * @see _locale_translation_source_compare()
  21. */
  22. const LOCALE_TRANSLATION_SOURCE_COMPARE_EQ = 0;
  23. /**
  24. * Comparison result of source files timestamps.
  25. *
  26. * Timestamp of source 1 is greater than the timestamp of source 2.
  27. *
  28. * @see _locale_translation_source_compare()
  29. */
  30. const LOCALE_TRANSLATION_SOURCE_COMPARE_GT = 1;
  31. /**
  32. * Get array of projects which are available for interface translation.
  33. *
  34. * This project data contains all projects which will be checked for available
  35. * interface translations.
  36. *
  37. * For full functionality this function depends on Update module.
  38. * When Update module is enabled the project data will contain the most recent
  39. * module status; both in enabled status as in version. When Update module is
  40. * disabled this function will return the last known module state. The status
  41. * will only be updated once Update module is enabled.
  42. *
  43. * @param array $project_names
  44. * Array of names of the projects to get.
  45. *
  46. * @return array
  47. * Array of project data for translation update.
  48. *
  49. * @see locale_translation_build_projects()
  50. */
  51. function locale_translation_get_projects(array $project_names = []) {
  52. $projects = &drupal_static(__FUNCTION__, []);
  53. if (empty($projects)) {
  54. // Get project data from the database.
  55. $row_count = \Drupal::service('locale.project')->countProjects();
  56. // https://www.drupal.org/node/1777106 is a follow-up issue to make the
  57. // check for possible out-of-date project information more robust.
  58. if ($row_count == 0) {
  59. module_load_include('compare.inc', 'locale');
  60. // At least the core project should be in the database, so we build the
  61. // data if none are found.
  62. locale_translation_build_projects();
  63. }
  64. $projects = \Drupal::service('locale.project')->getAll();
  65. array_walk($projects, function (&$project) {
  66. $project = (object) $project;
  67. });
  68. }
  69. // Return the requested project names or all projects.
  70. if ($project_names) {
  71. return array_intersect_key($projects, array_combine($project_names, $project_names));
  72. }
  73. return $projects;
  74. }
  75. /**
  76. * Clears the projects cache.
  77. */
  78. function locale_translation_clear_cache_projects() {
  79. drupal_static_reset('locale_translation_get_projects');
  80. }
  81. /**
  82. * Loads cached translation sources containing current translation status.
  83. *
  84. * @param array $projects
  85. * Array of project names. Defaults to all translatable projects.
  86. * @param array $langcodes
  87. * Array of language codes. Defaults to all translatable languages.
  88. *
  89. * @return array
  90. * Array of source objects. Keyed with <project name>:<language code>.
  91. *
  92. * @see locale_translation_source_build()
  93. */
  94. function locale_translation_load_sources(array $projects = NULL, array $langcodes = NULL) {
  95. $sources = [];
  96. $projects = $projects ? $projects : array_keys(locale_translation_get_projects());
  97. $langcodes = $langcodes ? $langcodes : array_keys(locale_translatable_language_list());
  98. // Load source data from locale_translation_status cache.
  99. $status = locale_translation_get_status();
  100. // Use only the selected projects and languages for update.
  101. foreach ($projects as $project) {
  102. foreach ($langcodes as $langcode) {
  103. $sources[$project][$langcode] = isset($status[$project][$langcode]) ? $status[$project][$langcode] : NULL;
  104. }
  105. }
  106. return $sources;
  107. }
  108. /**
  109. * Build translation sources.
  110. *
  111. * @param array $projects
  112. * Array of project names. Defaults to all translatable projects.
  113. * @param array $langcodes
  114. * Array of language codes. Defaults to all translatable languages.
  115. *
  116. * @return array
  117. * Array of source objects. Keyed by project name and language code.
  118. *
  119. * @see locale_translation_source_build()
  120. */
  121. function locale_translation_build_sources(array $projects = [], array $langcodes = []) {
  122. $sources = [];
  123. $projects = locale_translation_get_projects($projects);
  124. $langcodes = $langcodes ? $langcodes : array_keys(locale_translatable_language_list());
  125. foreach ($projects as $project) {
  126. foreach ($langcodes as $langcode) {
  127. $source = locale_translation_source_build($project, $langcode);
  128. $sources[$source->name][$source->langcode] = $source;
  129. }
  130. }
  131. return $sources;
  132. }
  133. /**
  134. * Checks whether a po file exists in the local filesystem.
  135. *
  136. * It will search in the directory set in the translation source. Which defaults
  137. * to the "translations://" stream wrapper path. The directory may contain any
  138. * valid stream wrapper.
  139. *
  140. * The "local" files property of the source object contains the definition of a
  141. * po file we are looking for. The file name defaults to
  142. * %project-%version.%language.po. Per project this value can be overridden
  143. * using the server_pattern directive in the module's .info.yml file or by using
  144. * hook_locale_translation_projects_alter().
  145. *
  146. * @param object $source
  147. * Translation source object.
  148. *
  149. * @return object
  150. * Source file object of the po file, updated with:
  151. * - "uri": File name and path.
  152. * - "timestamp": Last updated time of the po file.
  153. * FALSE if the file is not found.
  154. *
  155. * @see locale_translation_source_build()
  156. */
  157. function locale_translation_source_check_file($source) {
  158. if (isset($source->files[LOCALE_TRANSLATION_LOCAL])) {
  159. $source_file = $source->files[LOCALE_TRANSLATION_LOCAL];
  160. $directory = $source_file->directory;
  161. $filename = '/' . preg_quote($source_file->filename) . '$/';
  162. if (is_dir($directory)) {
  163. if ($files = \Drupal::service('file_system')->scanDirectory($directory, $filename, ['key' => 'name', 'recurse' => FALSE])) {
  164. $file = current($files);
  165. $source_file->uri = $file->uri;
  166. $source_file->timestamp = filemtime($file->uri);
  167. return $source_file;
  168. }
  169. }
  170. }
  171. return FALSE;
  172. }
  173. /**
  174. * Builds abstract translation source.
  175. *
  176. * @param object $project
  177. * Project object.
  178. * @param string $langcode
  179. * Language code.
  180. * @param string $filename
  181. * (optional) File name of translation file. May contain placeholders.
  182. * Defaults to the default translation filename from the settings.
  183. *
  184. * @return object
  185. * Source object:
  186. * - "project": Project name.
  187. * - "name": Project name (inherited from project).
  188. * - "language": Language code.
  189. * - "core": Core version (inherited from project).
  190. * - "version": Project version (inherited from project).
  191. * - "project_type": Project type (inherited from project).
  192. * - "files": Array of file objects containing properties of local and remote
  193. * translation files.
  194. * Other processes can add the following properties:
  195. * - "type": Most recent translation source found. LOCALE_TRANSLATION_REMOTE
  196. * and LOCALE_TRANSLATION_LOCAL indicate available new translations,
  197. * LOCALE_TRANSLATION_CURRENT indicate that the current translation is them
  198. * most recent. "type" corresponds with a key of the "files" array.
  199. * - "timestamp": The creation time of the "type" translation (file).
  200. * - "last_checked": The time when the "type" translation was last checked.
  201. * The "files" array can hold file objects of type:
  202. * LOCALE_TRANSLATION_LOCAL, LOCALE_TRANSLATION_REMOTE and
  203. * LOCALE_TRANSLATION_CURRENT. Each contains following properties:
  204. * - "type": The object type (LOCALE_TRANSLATION_LOCAL,
  205. * LOCALE_TRANSLATION_REMOTE, etc. see above).
  206. * - "project": Project name.
  207. * - "langcode": Language code.
  208. * - "version": Project version.
  209. * - "uri": Local or remote file path.
  210. * - "directory": Directory of the local po file.
  211. * - "filename": File name.
  212. * - "timestamp": Timestamp of the file.
  213. * - "keep": TRUE to keep the downloaded file.
  214. */
  215. function locale_translation_source_build($project, $langcode, $filename = NULL) {
  216. // Follow-up issue: https://www.drupal.org/node/1842380.
  217. // Convert $source object to a TranslatableProject class and use a typed class
  218. // for $source-file.
  219. // Create a source object with data of the project object.
  220. $source = clone $project;
  221. $source->project = $project->name;
  222. $source->langcode = $langcode;
  223. $source->type = '';
  224. $source->timestamp = 0;
  225. $source->last_checked = 0;
  226. $filename = $filename ? $filename : \Drupal::config('locale.settings')->get('translation.default_filename');
  227. // If the server_pattern contains a remote file path we will check for a
  228. // remote file. The local version of this file will only be checked if a
  229. // translations directory has been defined. If the server_pattern is a local
  230. // file path we will only check for a file in the local file system.
  231. $files = [];
  232. if (_locale_translation_file_is_remote($source->server_pattern)) {
  233. $files[LOCALE_TRANSLATION_REMOTE] = (object) [
  234. 'project' => $project->name,
  235. 'langcode' => $langcode,
  236. 'version' => $project->version,
  237. 'type' => LOCALE_TRANSLATION_REMOTE,
  238. 'filename' => locale_translation_build_server_pattern($source, basename($source->server_pattern)),
  239. 'uri' => locale_translation_build_server_pattern($source, $source->server_pattern),
  240. ];
  241. $files[LOCALE_TRANSLATION_LOCAL] = (object) [
  242. 'project' => $project->name,
  243. 'langcode' => $langcode,
  244. 'version' => $project->version,
  245. 'type' => LOCALE_TRANSLATION_LOCAL,
  246. 'filename' => locale_translation_build_server_pattern($source, $filename),
  247. 'directory' => 'translations://',
  248. ];
  249. $files[LOCALE_TRANSLATION_LOCAL]->uri = $files[LOCALE_TRANSLATION_LOCAL]->directory . $files[LOCALE_TRANSLATION_LOCAL]->filename;
  250. }
  251. else {
  252. $files[LOCALE_TRANSLATION_LOCAL] = (object) [
  253. 'project' => $project->name,
  254. 'langcode' => $langcode,
  255. 'version' => $project->version,
  256. 'type' => LOCALE_TRANSLATION_LOCAL,
  257. 'filename' => locale_translation_build_server_pattern($source, basename($source->server_pattern)),
  258. 'directory' => locale_translation_build_server_pattern($source, \Drupal::service('file_system')->dirname($source->server_pattern)),
  259. ];
  260. $files[LOCALE_TRANSLATION_LOCAL]->uri = $files[LOCALE_TRANSLATION_LOCAL]->directory . '/' . $files[LOCALE_TRANSLATION_LOCAL]->filename;
  261. }
  262. $source->files = $files;
  263. // If this project+language is already translated, we add its status and
  264. // update the current translation timestamp and last_updated time. If the
  265. // project+language is not translated before, create a new record.
  266. $history = locale_translation_get_file_history();
  267. if (isset($history[$project->name][$langcode]) && $history[$project->name][$langcode]->timestamp) {
  268. $source->files[LOCALE_TRANSLATION_CURRENT] = $history[$project->name][$langcode];
  269. $source->type = LOCALE_TRANSLATION_CURRENT;
  270. $source->timestamp = $history[$project->name][$langcode]->timestamp;
  271. $source->last_checked = $history[$project->name][$langcode]->last_checked;
  272. }
  273. else {
  274. locale_translation_update_file_history($source);
  275. }
  276. return $source;
  277. }
  278. /**
  279. * Build path to translation source, out of a server path replacement pattern.
  280. *
  281. * @param object $project
  282. * Project object containing data to be inserted in the template.
  283. * @param string $template
  284. * String containing placeholders. Available placeholders:
  285. * - "%project": Project name.
  286. * - "%version": Project version.
  287. * - "%core": Project core version.
  288. * - "%language": Language code.
  289. *
  290. * @return string
  291. * String with replaced placeholders.
  292. */
  293. function locale_translation_build_server_pattern($project, $template) {
  294. $variables = [
  295. '%project' => $project->name,
  296. '%version' => $project->version,
  297. '%core' => $project->core,
  298. '%language' => isset($project->langcode) ? $project->langcode : '%language',
  299. ];
  300. return strtr($template, $variables);
  301. }
  302. /**
  303. * Populate a queue with project to check for translation updates.
  304. */
  305. function locale_cron_fill_queue() {
  306. $updates = [];
  307. $config = \Drupal::config('locale.settings');
  308. // Determine which project+language should be updated.
  309. $last = REQUEST_TIME - $config->get('translation.update_interval_days') * 3600 * 24;
  310. $projects = \Drupal::service('locale.project')->getAll();
  311. $projects = array_filter($projects, function ($project) {
  312. return $project['status'] == 1;
  313. });
  314. $connection = \Drupal::database();
  315. $files = $connection->select('locale_file', 'f')
  316. ->condition('f.project', array_keys($projects), 'IN')
  317. ->condition('f.last_checked', $last, '<')
  318. ->fields('f', ['project', 'langcode'])
  319. ->execute()->fetchAll();
  320. foreach ($files as $file) {
  321. $updates[$file->project][] = $file->langcode;
  322. // Update the last_checked timestamp of the project+language that will
  323. // be checked for updates.
  324. $connection->update('locale_file')
  325. ->fields(['last_checked' => REQUEST_TIME])
  326. ->condition('project', $file->project)
  327. ->condition('langcode', $file->langcode)
  328. ->execute();
  329. }
  330. // For each project+language combination a number of tasks are added to
  331. // the queue.
  332. if ($updates) {
  333. module_load_include('fetch.inc', 'locale');
  334. $options = _locale_translation_default_update_options();
  335. $queue = \Drupal::queue('locale_translation', TRUE);
  336. foreach ($updates as $project => $languages) {
  337. $batch = locale_translation_batch_update_build([$project], $languages, $options);
  338. foreach ($batch['operations'] as $item) {
  339. $queue->createItem($item);
  340. }
  341. }
  342. }
  343. }
  344. /**
  345. * Determine if a file is a remote file.
  346. *
  347. * @param string $uri
  348. * The URI or URI pattern of the file.
  349. *
  350. * @return bool
  351. * TRUE if the $uri is a remote file.
  352. */
  353. function _locale_translation_file_is_remote($uri) {
  354. $scheme = StreamWrapperManager::getScheme($uri);
  355. if ($scheme) {
  356. return !\Drupal::service('file_system')->realpath($scheme . '://');
  357. }
  358. return FALSE;
  359. }
  360. /**
  361. * Compare two update sources, looking for the newer one.
  362. *
  363. * The timestamp property of the source objects are used to determine which is
  364. * the newer one.
  365. *
  366. * @param object $source1
  367. * Source object of the first translation source.
  368. * @param object $source2
  369. * Source object of available update.
  370. *
  371. * @return int
  372. * - "LOCALE_TRANSLATION_SOURCE_COMPARE_LT": $source1 < $source2 OR $source1
  373. * is missing.
  374. * - "LOCALE_TRANSLATION_SOURCE_COMPARE_EQ": $source1 == $source2 OR both
  375. * $source1 and $source2 are missing.
  376. * - "LOCALE_TRANSLATION_SOURCE_COMPARE_GT": $source1 > $source2 OR $source2
  377. * is missing.
  378. */
  379. function _locale_translation_source_compare($source1, $source2) {
  380. if (isset($source1->timestamp) && isset($source2->timestamp)) {
  381. if ($source1->timestamp == $source2->timestamp) {
  382. return LOCALE_TRANSLATION_SOURCE_COMPARE_EQ;
  383. }
  384. else {
  385. return $source1->timestamp > $source2->timestamp ? LOCALE_TRANSLATION_SOURCE_COMPARE_GT : LOCALE_TRANSLATION_SOURCE_COMPARE_LT;
  386. }
  387. }
  388. elseif (isset($source1->timestamp) && !isset($source2->timestamp)) {
  389. return LOCALE_TRANSLATION_SOURCE_COMPARE_GT;
  390. }
  391. elseif (!isset($source1->timestamp) && isset($source2->timestamp)) {
  392. return LOCALE_TRANSLATION_SOURCE_COMPARE_LT;
  393. }
  394. else {
  395. return LOCALE_TRANSLATION_SOURCE_COMPARE_EQ;
  396. }
  397. }
  398. /**
  399. * Returns default import options for translation update.
  400. *
  401. * @return array
  402. * Array of translation import options.
  403. */
  404. function _locale_translation_default_update_options() {
  405. $config = \Drupal::config('locale.settings');
  406. return [
  407. 'customized' => LOCALE_NOT_CUSTOMIZED,
  408. 'overwrite_options' => [
  409. 'not_customized' => $config->get('translation.overwrite_not_customized'),
  410. 'customized' => $config->get('translation.overwrite_customized'),
  411. ],
  412. 'finish_feedback' => TRUE,
  413. 'use_remote' => locale_translation_use_remote_source(),
  414. ];
  415. }