You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
107 lines
2.8 KiB
107 lines
2.8 KiB
7 years ago
|
<?php
|
||
|
|
||
7 years ago
|
require_once __DIR__ . '/i18n/I18nFile.php';
|
||
|
require_once __DIR__ . '/i18n/I18nCompletionValidator.php';
|
||
|
require_once __DIR__ . '/i18n/I18nUsageValidator.php';
|
||
7 years ago
|
|
||
|
$i18nFile = new I18nFile();
|
||
|
$i18nData = $i18nFile->load();
|
||
7 years ago
|
|
||
7 years ago
|
$options = getopt("dhl:r");
|
||
7 years ago
|
|
||
|
if (array_key_exists('h', $options)) {
|
||
|
help();
|
||
|
}
|
||
|
if (array_key_exists('l', $options)) {
|
||
7 years ago
|
$languages = array($options['l']);
|
||
7 years ago
|
} else {
|
||
7 years ago
|
$languages = $i18nData->getAvailableLanguages();
|
||
7 years ago
|
}
|
||
7 years ago
|
$displayResults = array_key_exists('d', $options);
|
||
7 years ago
|
$displayReport = array_key_exists('r', $options);
|
||
|
|
||
7 years ago
|
$isValidated = true;
|
||
|
$result = array();
|
||
7 years ago
|
$report = array();
|
||
|
|
||
7 years ago
|
foreach ($languages as $language) {
|
||
|
if ($language === $i18nData::REFERENCE_LANGUAGE) {
|
||
|
$i18nValidator = new I18nUsageValidator($i18nData->getReferenceLanguage(), findUsedTranslations());
|
||
7 years ago
|
$isValidated = $i18nValidator->validate(include __DIR__ . '/i18n/ignore/' . $language . '.php') && $isValidated;
|
||
7 years ago
|
} else {
|
||
|
$i18nValidator = new I18nCompletionValidator($i18nData->getReferenceLanguage(), $i18nData->getLanguage($language));
|
||
7 years ago
|
if (file_exists(__DIR__ . '/i18n/ignore/' . $language . '.php')) {
|
||
|
$isValidated = $i18nValidator->validate(include __DIR__ . '/i18n/ignore/' . $language . '.php') && $isValidated;
|
||
7 years ago
|
} else {
|
||
|
$isValidated = $i18nValidator->validate(null) && $isValidated;
|
||
7 years ago
|
}
|
||
|
}
|
||
7 years ago
|
|
||
|
$report[$language] = sprintf('%-5s - %s', $language, $i18nValidator->displayReport());
|
||
|
$result[$language] = $i18nValidator->displayResult();
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
if ($displayResults) {
|
||
|
foreach ($result as $lang => $value) {
|
||
7 years ago
|
echo 'Language: ', $lang, PHP_EOL;
|
||
7 years ago
|
print_r($value);
|
||
7 years ago
|
echo PHP_EOL;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if ($displayReport) {
|
||
7 years ago
|
foreach ($report as $value) {
|
||
|
echo $value;
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
if (!$isValidated) {
|
||
7 years ago
|
exit(1);
|
||
|
}
|
||
|
|
||
|
/**
|
||
7 years ago
|
* Find used translation keys in the project
|
||
|
*
|
||
|
* Iterates through all php and phtml files in the whole project and extracts all
|
||
|
* translation keys used.
|
||
7 years ago
|
*
|
||
|
* @return array
|
||
|
*/
|
||
7 years ago
|
function findUsedTranslations() {
|
||
|
$directory = new RecursiveDirectoryIterator(__DIR__ . '/..');
|
||
|
$iterator = new RecursiveIteratorIterator($directory);
|
||
|
$regex = new RegexIterator($iterator, '/^.+\.(php|phtml)$/i', RecursiveRegexIterator::GET_MATCH);
|
||
|
$usedI18n = array();
|
||
|
foreach (array_keys(iterator_to_array($regex)) as $file) {
|
||
|
$fileContent = file_get_contents($file);
|
||
|
preg_match_all('/_t\([\'"](?P<strings>[^\'"]+)[\'"]/', $fileContent, $matches);
|
||
|
$usedI18n = array_merge($usedI18n, $matches['strings']);
|
||
7 years ago
|
}
|
||
7 years ago
|
return $usedI18n;
|
||
7 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Output help message.
|
||
|
*/
|
||
|
function help() {
|
||
|
$help = <<<HELP
|
||
|
NAME
|
||
|
%s
|
||
|
|
||
|
SYNOPSIS
|
||
|
php %s [OPTION]...
|
||
|
|
||
|
DESCRIPTION
|
||
|
Check if translation files have missing keys or missing translations.
|
||
|
|
||
|
-d display results.
|
||
|
-h display this help and exit.
|
||
|
-l=LANG filter by LANG.
|
||
|
-r display completion report.
|
||
|
|
||
|
HELP;
|
||
|
$file = str_replace(__DIR__ . '/', '', __FILE__);
|
||
|
echo sprintf($help, $file, $file);
|
||
|
exit;
|
||
|
}
|