check.translation.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 bool $displayResult;
  14. public bool $help;
  15. public bool $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 ($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. $isValidated = true;
  38. $result = [];
  39. $report = [];
  40. foreach ($languages as $language) {
  41. if ($language === $i18nData::REFERENCE_LANGUAGE) {
  42. $i18nValidator = new I18nUsageValidator($i18nData->getReferenceLanguage(), findUsedTranslations());
  43. } else {
  44. $i18nValidator = new I18nCompletionValidator($i18nData->getReferenceLanguage(), $i18nData->getLanguage($language));
  45. }
  46. $isValidated = $i18nValidator->validate() && $isValidated;
  47. $report[$language] = sprintf('%-5s - %s', $language, $i18nValidator->displayReport());
  48. $result[$language] = $i18nValidator->displayResult();
  49. }
  50. if ($cliOptions->displayResult) {
  51. foreach ($result as $lang => $value) {
  52. echo 'Language: ', $lang, PHP_EOL;
  53. print_r($value);
  54. echo PHP_EOL;
  55. }
  56. }
  57. if ($cliOptions->displayReport) {
  58. foreach ($report as $value) {
  59. echo $value;
  60. }
  61. }
  62. if (!$isValidated) {
  63. exit(1);
  64. }
  65. /**
  66. * Find used translation keys in the project
  67. *
  68. * Iterates through all php and phtml files in the whole project and extracts all
  69. * translation keys used.
  70. *
  71. * @return list<string>
  72. */
  73. function findUsedTranslations(): array {
  74. $directory = new RecursiveDirectoryIterator(__DIR__ . '/..');
  75. $iterator = new RecursiveIteratorIterator($directory);
  76. $regex = new RegexIterator($iterator, '/^.+\.(php|phtml)$/i', RecursiveRegexIterator::GET_MATCH);
  77. $usedI18n = [];
  78. foreach (array_keys(iterator_to_array($regex)) as $file) {
  79. if (!is_string($file) || $file === '') {
  80. continue;
  81. }
  82. $fileContent = file_get_contents($file);
  83. if ($fileContent === false) {
  84. continue;
  85. }
  86. preg_match_all('/_t\([\'"](?P<strings>[^\'"]+)[\'"]/', $fileContent, $matches);
  87. $usedI18n = array_merge($usedI18n, $matches['strings']);
  88. }
  89. return $usedI18n;
  90. }
  91. /**
  92. * Output help message.
  93. */
  94. function checkHelp(): never {
  95. $file = str_replace(__DIR__ . '/', '', __FILE__);
  96. echo <<<HELP
  97. NAME
  98. $file
  99. SYNOPSIS
  100. php $file [OPTION]...
  101. DESCRIPTION
  102. Check if translation files have missing keys or missing translations.
  103. -d, --display-result display results.
  104. -h, --help display this help and exit.
  105. -l, --language=LANG filter by LANG.
  106. -r, --display-report display completion report.
  107. HELP;
  108. exit();
  109. }