check.translation.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/usr/bin/env php
  2. <?php
  3. declare(strict_types=1);
  4. require_once __DIR__ . '/_cli.php';
  5. require_once __DIR__ . '/i18n/I18nCompletionValidator.php';
  6. require_once __DIR__ . '/i18n/I18nData.php';
  7. require_once __DIR__ . '/i18n/I18nFile.php';
  8. require_once __DIR__ . '/i18n/I18nUsageValidator.php';
  9. require_once __DIR__ . '/../constants.php';
  10. $i18nFile = new I18nFile();
  11. $i18nData = new I18nData($i18nFile->load());
  12. $parameters = [
  13. 'long' => [
  14. 'display-result' => '',
  15. 'help' => '',
  16. 'language' => ':',
  17. 'display-report' => '',
  18. ],
  19. 'short' => [
  20. 'display-result' => 'd',
  21. 'help' => 'h',
  22. 'language' => 'l',
  23. 'display-report' => 'r',
  24. ],
  25. 'deprecated' => [],
  26. ];
  27. $options = parseCliParams($parameters);
  28. if (!empty($options['invalid']) || array_key_exists('help', $options['valid'])) {
  29. checkHelp();
  30. }
  31. if (array_key_exists('language', $options['valid'])) {
  32. $languages = [$options['valid']['language']];
  33. } else {
  34. $languages = $i18nData->getAvailableLanguages();
  35. }
  36. $displayResults = array_key_exists('display-result', $options['valid']);
  37. $displayReport = array_key_exists('display-report', $options['valid']);
  38. $isValidated = true;
  39. $result = [];
  40. $report = [];
  41. foreach ($languages as $language) {
  42. if ($language === $i18nData::REFERENCE_LANGUAGE) {
  43. $i18nValidator = new I18nUsageValidator($i18nData->getReferenceLanguage(), findUsedTranslations());
  44. } else {
  45. $i18nValidator = new I18nCompletionValidator($i18nData->getReferenceLanguage(), $i18nData->getLanguage($language));
  46. }
  47. $isValidated = $i18nValidator->validate() && $isValidated;
  48. $report[$language] = sprintf('%-5s - %s', $language, $i18nValidator->displayReport());
  49. $result[$language] = $i18nValidator->displayResult();
  50. }
  51. if ($displayResults) {
  52. foreach ($result as $lang => $value) {
  53. echo 'Language: ', $lang, PHP_EOL;
  54. print_r($value);
  55. echo PHP_EOL;
  56. }
  57. }
  58. if ($displayReport) {
  59. foreach ($report as $value) {
  60. echo $value;
  61. }
  62. }
  63. if (!$isValidated) {
  64. exit(1);
  65. }
  66. /**
  67. * Find used translation keys in the project
  68. *
  69. * Iterates through all php and phtml files in the whole project and extracts all
  70. * translation keys used.
  71. *
  72. * @return array<string>
  73. */
  74. function findUsedTranslations(): array {
  75. $directory = new RecursiveDirectoryIterator(__DIR__ . '/..');
  76. $iterator = new RecursiveIteratorIterator($directory);
  77. $regex = new RegexIterator($iterator, '/^.+\.(php|phtml)$/i', RecursiveRegexIterator::GET_MATCH);
  78. $usedI18n = [];
  79. foreach (array_keys(iterator_to_array($regex)) as $file) {
  80. $fileContent = file_get_contents($file);
  81. if ($fileContent === false) {
  82. continue;
  83. }
  84. preg_match_all('/_t\([\'"](?P<strings>[^\'"]+)[\'"]/', $fileContent, $matches);
  85. $usedI18n = array_merge($usedI18n, $matches['strings']);
  86. }
  87. return $usedI18n;
  88. }
  89. /**
  90. * Output help message.
  91. * @return never
  92. */
  93. function checkHelp() {
  94. $file = str_replace(__DIR__ . '/', '', __FILE__);
  95. echo <<<HELP
  96. NAME
  97. $file
  98. SYNOPSIS
  99. php $file [OPTION]...
  100. DESCRIPTION
  101. Check if translation files have missing keys or missing translations.
  102. -d, --display-result display results.
  103. -h, --help display this help and exit.
  104. -l, --language=LANG filter by LANG.
  105. -r, --display-report display completion report.
  106. HELP;
  107. exit;
  108. }