-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloco_translate.module
130 lines (113 loc) · 3.63 KB
/
loco_translate.module
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
<?php
/**
* @file
* Primary module hooks for Loco Translate module.
*/
require_once 'inc/cron_pull.inc';
require_once 'inc/cron_push.inc';
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_theme().
*/
function loco_translate_theme($existing, $type, $theme, $path) {
return [
'loco_translate_overview_page' => [
'variables' => ['variables' => []],
'template' => 'loco-translate--overview--page',
],
];
}
/**
* Implements hook_cron().
*
* Automation of Loco Translate module.
*/
function loco_translate_cron() {
$now = time();
$config = \Drupal::config('loco_translate.settings');
$utility = \Drupal::service('loco_translate.utility');
// Get configured langcode to pull from loco.
$langcodes = $config->get('automation.pull.langcodes');
if ($langcodes) {
foreach ($langcodes as $langcode => $status) {
// Skip unchecked langcode.
if (!$status) {
continue;
}
// Skip when automation pull is disabled.
if ($config->get('automation.pull.interval') <= 0) {
continue;
}
// Check last run of this langcode.
$next_pull = $utility->getLastPull($langcode) + $config->get('automation.pull.interval');
if ($now <= $next_pull) {
continue;
}
try {
loco_translate_cron_pull($langcode, $config->get('automation.pull'));
$utility->setLastPull($langcode, $now);
}
catch (\Exception $e) {
\Drupal::logger('loco_translate')->error($e);
}
}
}
// Get configured langcode to push from loco.
$langcodes = $config->get('automation.push.langcodes');
if ($langcodes && $config->get('automation.push.template')) {
foreach ($langcodes as $langcode => $status) {
// Skip unchecked langcode.
if (!$status) {
continue;
}
// Skip when automation push is disabled.
if ($config->get('automation.push.interval') <= 0) {
continue;
}
// Check last run of this langcode.
$next_push = $utility->getLastPush($langcode) + $config->get('automation.push.interval');
if ($now <= $next_push) {
continue;
}
try {
loco_translate_cron_push($langcode, $config->get('automation.push'));
$utility->setLastPush($langcode, $now);
}
catch (\Exception $e) {
\Drupal::logger('loco_translate')->error($e);
}
}
}
}
/**
* Implements hook_help().
*
* Hook to generate help page.
*/
function loco_translate_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.loco_translate':
$readme = __DIR__ . '/README.md';
$text = file_get_contents($readme);
$output = '';
// If the Markdown module is installed, use it to render the README.
if ($text && \Drupal::moduleHandler()->moduleExists('markdown') === TRUE) {
$filter_manager = \Drupal::service('plugin.manager.filter');
$settings = \Drupal::configFactory()->get('markdown.settings')->getRawData();
$config = ['settings' => $settings];
$filter = $filter_manager->createInstance('markdown', $config);
$output = $filter->process($text, 'en');
}
// Else the Markdown module is not installed output the README as text.
elseif ($text) {
$output = '<pre>' . $text . '</pre>';
}
// Add a link to the Drupal.org project.
$output .= '<p>';
$output .= t('Visit the <a href=":project_link">Loco Translate project page</a> on Drupal.org for more information.', [
':project_link' => 'https://www.drupal.org/project/loco_translate',
]);
$output .= '</p>';
return $output;
}
}