|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\qa\Plugin\Qa\Control\I18n; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\qa\Pass; |
|
6
|
|
|
use Drupal\qa\Plugin\Qa\Control\BaseControl; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Find inconsistencies in {i18nvariables} and {languages} |
|
10
|
|
|
*/ |
|
11
|
|
|
class Variables extends BaseControl { |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $package_name; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var \Drupal\Core\StringTranslation\TranslatableMarkup |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $description; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var \Drupal\Core\StringTranslation\TranslatableMarkup |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $title; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritdoc] |
|
30
|
|
|
*/ |
|
31
|
|
|
public function init() { |
|
32
|
|
|
$this->package_name = __NAMESPACE__; |
|
33
|
|
|
$this->title = t('Inconsistent variables translation'); |
|
34
|
|
|
$this->description = t('In most scenarios, when a variable is translated at least once, it ought to be translated in every language on the site, not more, not less.'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Identify variables translations for languages not currently on site |
|
39
|
|
|
*/ |
|
40
|
|
|
public function checkExtra() { |
|
41
|
|
|
$languages = array_keys(language_list()); |
|
|
|
|
|
|
42
|
|
|
$ph = db_placeholders($languages, 'char'); |
|
|
|
|
|
|
43
|
|
|
$sq = <<<sql |
|
44
|
|
|
SELECT v.name, v.language |
|
45
|
|
|
FROM {i18n_variable} v |
|
46
|
|
|
WHERE v.language NOT IN ($ph) |
|
47
|
|
|
ORDER BY 1, 2 |
|
48
|
|
|
sql; |
|
49
|
|
|
$q = db_query($sq, $languages); |
|
|
|
|
|
|
50
|
|
|
// No db_rewrite_sql: this needs to be unhindered by any access control |
|
51
|
|
|
$vars = []; |
|
52
|
|
|
while ($o = db_fetch_object($q)) { |
|
|
|
|
|
|
53
|
|
|
$vars[$o->name][] = $o->language; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$items = []; |
|
57
|
|
|
foreach ($vars as $name => $var_languages) { |
|
58
|
|
|
$items[] = t('@var: @languages', |
|
59
|
|
|
['@var' => $name, '@languages' => implode(', ', $var_languages)]); |
|
60
|
|
|
} |
|
61
|
|
|
$ret = [ |
|
62
|
|
|
'name' => 'extra', |
|
63
|
|
|
'status' => empty($vars) ? 1 : 0, |
|
64
|
|
|
'result' => ['extra' => $items], |
|
65
|
|
|
]; |
|
66
|
|
|
return $ret; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Identify variables for which at least one translation is missing |
|
71
|
|
|
*/ |
|
72
|
|
|
public function checkMissing() { |
|
73
|
|
|
$languages = array_keys(language_list()); |
|
|
|
|
|
|
74
|
|
|
$ph = db_placeholders($languages, 'char'); |
|
|
|
|
|
|
75
|
|
|
$sq = <<<sql |
|
76
|
|
|
SELECT v.name, v.language |
|
77
|
|
|
FROM {i18n_variable} v |
|
78
|
|
|
WHERE v.language IN ($ph) |
|
79
|
|
|
ORDER BY 1, 2 |
|
80
|
|
|
sql; |
|
81
|
|
|
$q = db_query($sq, $languages); |
|
|
|
|
|
|
82
|
|
|
// No db_rewrite_sql: this needs to be unhindered by any access control |
|
83
|
|
|
$vars = []; |
|
84
|
|
|
while ($o = db_fetch_object($q, $ph)) { |
|
|
|
|
|
|
85
|
|
|
$vars[$o->name][] = $o->language; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$items = []; |
|
89
|
|
|
foreach ($vars as $name => $var_languages) { |
|
90
|
|
|
$missing = array_diff($languages, $var_languages); |
|
91
|
|
|
if (!empty($missing)) { |
|
92
|
|
|
$items[] = t('@var: @languages', |
|
93
|
|
|
['@var' => $name, '@languages' => implode(', ', $missing)]); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$ret = [ |
|
98
|
|
|
'name' => 'missing', |
|
99
|
|
|
'status' => empty($items) ? 1 : 0, |
|
100
|
|
|
'result' => ['missing' => $items], |
|
101
|
|
|
]; |
|
102
|
|
|
return $ret; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public static function getDependencies(): array { |
|
106
|
|
|
$ret = ['i18n']; // introduces {i18n_variable} |
|
107
|
|
|
return $ret; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function run(): Pass { |
|
111
|
|
|
$pass = parent::run(); |
|
112
|
|
|
$pass->record($this->checkExtra()); |
|
113
|
|
|
$pass->life->modify(); |
|
114
|
|
|
$pass->record($this->checkMissing()); |
|
115
|
|
|
$pass->life->end(); |
|
116
|
|
|
|
|
117
|
|
|
$extra_row = empty($pass->result['extra']['extra']) |
|
118
|
|
|
? [['data' => t('No extra translation found'), 'colspan' => 2,]] |
|
119
|
|
|
: [ |
|
120
|
|
|
t('Extra translations'), |
|
121
|
|
|
theme('item_list', $pass->result['extra']['extra']), |
|
|
|
|
|
|
122
|
|
|
]; |
|
123
|
|
|
|
|
124
|
|
|
$missing_row = empty($pass->result['missing']['missing']) |
|
125
|
|
|
? [ |
|
126
|
|
|
[ |
|
127
|
|
|
'data' => t('No missing translation found'), |
|
128
|
|
|
'colspan' => 2, |
|
129
|
|
|
], |
|
130
|
|
|
] |
|
131
|
|
|
: [ |
|
132
|
|
|
t('Missing translations'), |
|
133
|
|
|
theme('item_list', $pass->result['missing']['missing']), |
|
134
|
|
|
]; |
|
135
|
|
|
|
|
136
|
|
|
$rows = [$extra_row, $missing_row]; |
|
137
|
|
|
$pass->result = theme('table', NULL, $rows); |
|
138
|
|
|
return $pass; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|