forked from php-censor/php-censor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckLocalizationCommand.php
More file actions
170 lines (144 loc) · 4.48 KB
/
Copy pathCheckLocalizationCommand.php
File metadata and controls
170 lines (144 loc) · 4.48 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
namespace PHPCensor\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Check localizations.
*/
class CheckLocalizationCommand extends Command
{
/**
* @var string
*/
protected $basePath;
/**
* @var array
*/
protected $excluded = ['lang.en.php'];
/**
* Construct.
*/
public function __construct()
{
parent::__construct();
$this->basePath = __DIR__.'/../Languages';
}
/**
* Configure.
*/
protected function configure()
{
$this
->setName('php-censor:check-localizations')
->addOption(
'same',
0,
InputOption::VALUE_OPTIONAL,
'Same than English version (0 = no, 1 = yes)'
)
->addOption(
'langs',
[],
InputOption::VALUE_OPTIONAL,
'List of languages separated by commas. By default, all languages'
)
->setDescription('Check localizations.');
}
/**
* Loops through running.
*
* @param InputInterface $input
* @param OutputInterface $output
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln("\n<info>Check localizations!</info>");
$sameThanEnglish = (null !== $input->getOption('same'))
? $input->getOption('same')
: false;
$languagesList = (null !== $input->getOption('langs'))
? explode(',', $input->getOption('langs'))
: [];
// Get English version
$english = $this->getTranslations($this->basePath.'/lang.en.php');
$othersLanguages = $this->getLanguages($languagesList);
$diffs = $this->compareTranslations($english, $othersLanguages);
foreach ($diffs as $language => $value) {
$output->writeln(sprintf("%s:", $language));
if (!empty($value['not_present'])) {
$output->writeln("\tNot present:\n\t\t" . implode("\n\t\t", $value['not_present']));
}
if ($sameThanEnglish === '1' && !empty($value['same'])) {
$output->writeln("\tSame than English:\n\t\t" . implode("\n\t\t", $value['same']));
}
}
}
/**
* Returns array of translations by language.
*
* @param string $language language code
*
* @return array
*/
private function getTranslations($language)
{
return [
include($language)
];
}
/**
* Returns list of languages.
*
* @param array $languagesList
*
* @return array
*/
private function getLanguages(array $languagesList = [])
{
$files = glob($this->basePath . '/*.php');
$languages = array_map(function ($dir) use ($languagesList) {
$name = basename($dir);
if (in_array($name, $this->excluded, true)) {
return null;
}
// Check if in list of languages.
if (!empty($languagesList)) {
$languageOfName = explode('.', $name);
if (null == $languageOfName[1] || !in_array($languageOfName[1], $languagesList)) {
return null;
}
}
return $name;
}, $files);
return array_filter($languages);
}
/**
* Compare translations.
*
* @param array $default language by default
* @param array $languages others languages
*
* @return array
*/
private function compareTranslations(array $default, array $languages)
{
$diffs = [];
// Return diff language by language
foreach ($languages as $language) {
$current = $this->getTranslations($this->basePath.'/'.$language);
foreach ($default as $key => $values) {
$keyValues = array_keys($values);
foreach ($keyValues as $key2) {
if (!isset($current[$key][$key2])) {
$diffs[$language]['not_present'][] = $key2;
} elseif ($current[$key][$key2] === $default[$key][$key2]) {
$diffs[$language]['same'][] = $key2;
}
}
}
}
return $diffs;
}
}