check.translation.php 2.7 KB

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