check.translation.php 3.5 KB

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