check.translation.php 2.6 KB

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