From 05b1901fcdbb051077d12f776980484d3b782970 Mon Sep 17 00:00:00 2001 From: Alexis Degrugillier Date: Sat, 4 Nov 2017 21:17:08 +0100 Subject: Move translation tools into the cli folder (#1673) Translation tools must be used on cli. It is better to have them in the cli folder. --- cli/check.translation.php | 106 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 cli/check.translation.php (limited to 'cli/check.translation.php') diff --git a/cli/check.translation.php b/cli/check.translation.php new file mode 100644 index 000000000..6ebd12973 --- /dev/null +++ b/cli/check.translation.php @@ -0,0 +1,106 @@ +load(); + +$options = getopt("dhl:r"); + +if (array_key_exists('h', $options)) { + help(); +} +if (array_key_exists('l', $options)) { + $languages = array($options['l']); +} else { + $languages = $i18nData->getAvailableLanguages(); +} +$displayResults = array_key_exists('d', $options); +$displayReport = array_key_exists('r', $options); + +$isValidated = true; +$result = array(); +$report = array(); + +foreach ($languages as $language) { + if ($language === $i18nData::REFERENCE_LANGUAGE) { + $i18nValidator = new I18nUsageValidator($i18nData->getReferenceLanguage(), findUsedTranslations()); + $isValidated = $i18nValidator->validate(include __DIR__ . '/i18n/ignore/' . $language . '.php') && $isValidated; + } else { + $i18nValidator = new I18nCompletionValidator($i18nData->getReferenceLanguage(), $i18nData->getLanguage($language)); + if (file_exists(__DIR__ . '/i18n/ignore/' . $language . '.php')) { + $isValidated = $i18nValidator->validate(include __DIR__ . '/i18n/ignore/' . $language . '.php') && $isValidated; + } else { + $isValidated = $i18nValidator->validate(null) && $isValidated; + } + } + + $report[$language] = sprintf('%-5s - %s', $language, $i18nValidator->displayReport()); + $result[$language] = $i18nValidator->displayResult(); +} + +if ($displayResults) { + foreach ($result as $lang => $value) { + echo 'Language: ', $lang, PHP_EOL; + print_r($value); + echo PHP_EOL; + } +} + +if ($displayReport) { + foreach ($report as $value) { + echo $value; + } +} + +if (!$isValidated) { + exit(1); +} + +/** + * Find used translation keys in the project + * + * Iterates through all php and phtml files in the whole project and extracts all + * translation keys used. + * + * @return array + */ +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[^\'"]+)[\'"]/', $fileContent, $matches); + $usedI18n = array_merge($usedI18n, $matches['strings']); + } + return $usedI18n; +} + +/** + * Output help message. + */ +function help() { + $help = <<