4
0

check.translation.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 dirname(__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 bool $generateReadme;
  17. public function __construct() {
  18. $this->addOption('language', (new CliOption('language', 'l'))->typeOfArrayOfString());
  19. $this->addOption('displayResult', (new CliOption('display-result', 'd'))->withValueNone());
  20. $this->addOption('help', (new CliOption('help', 'h'))->withValueNone());
  21. $this->addOption('displayReport', (new CliOption('display-report', 'r'))->withValueNone());
  22. $this->addOption('generateReadme', (new CliOption('generate-readme', 'g'))->withValueNone());
  23. parent::__construct();
  24. }
  25. };
  26. if (!empty($cliOptions->errors)) {
  27. fail('FreshRSS error: ' . array_shift($cliOptions->errors) . "\n" . $cliOptions->usage);
  28. }
  29. if ($cliOptions->help) {
  30. checkHelp();
  31. }
  32. $i18nFile = new I18nFile();
  33. $i18nData = new I18nData($i18nFile->load());
  34. if (isset($cliOptions->language)) {
  35. $languages = $cliOptions->language;
  36. } else {
  37. $languages = $i18nData->getAvailableLanguages();
  38. }
  39. $isValidated = true;
  40. $result = [];
  41. $report = [];
  42. $percentage = [];
  43. foreach ($languages as $language) {
  44. if ($language === $i18nData::REFERENCE_LANGUAGE) {
  45. $i18nValidator = new I18nUsageValidator($i18nData->getReferenceLanguage(), findUsedTranslations());
  46. } else {
  47. $i18nValidator = new I18nCompletionValidator($i18nData->getReferenceLanguage(), $i18nData->getLanguage($language));
  48. }
  49. $isValidated = $i18nValidator->validate() && $isValidated;
  50. $report[$language] = sprintf('%-5s - %s', $language, $i18nValidator->displayReport());
  51. $percentage[$language] = $i18nValidator->displayReport(percentage_only: true);
  52. $result[$language] = $i18nValidator->displayResult();
  53. }
  54. if ($cliOptions->displayResult) {
  55. foreach ($result as $lang => $value) {
  56. echo 'Language: ', $lang, PHP_EOL;
  57. print_r($value);
  58. echo PHP_EOL;
  59. }
  60. }
  61. if ($cliOptions->displayReport) {
  62. foreach ($report as $value) {
  63. echo $value;
  64. }
  65. }
  66. function writeToReadme(string $readmePath, string $markdownTable): void {
  67. $language = explode('.', $readmePath)[1];
  68. // expecting `README.md` for `en` or `README.fr.md` for `fr`
  69. if ($language === 'md') {
  70. $language = 'en';
  71. }
  72. Minz_Translate::init($language);
  73. $placeholders = [];
  74. if (preg_match_all('/__.*?__/', $markdownTable, $placeholders) === false) {
  75. echo 'Error: Fail while matching translation placeholders', PHP_EOL;
  76. exit(1);
  77. }
  78. foreach (array_unique($placeholders[0]) as $_ => $placeholder) {
  79. $markdownTable = str_replace($placeholder, _t('gen.readme.' . substr($placeholder, 2, -2)), $markdownTable);
  80. }
  81. $readme = file_get_contents($readmePath);
  82. if ($readme === false) {
  83. echo 'Error: Unable to open ' . $readmePath, PHP_EOL;
  84. exit(1);
  85. }
  86. if (file_put_contents($readmePath, preg_replace('/<translations>(.*?)<\/translations>/s', <<<EOF
  87. <translations>
  88. <!-- This section is automatically generated by `./cli/check.translation.php -g` -->
  89. $markdownTable
  90. </translations>
  91. EOF, $readme)) === false) {
  92. echo 'Error: Fail while writing to ' . $readmePath, PHP_EOL;
  93. exit(1);
  94. }
  95. echo 'Successfully written translation status into ' . $readmePath, PHP_EOL;
  96. }
  97. if ($cliOptions->generateReadme) {
  98. $markdownTable = <<<EOF
  99. | __language__ | __translated__ | |
  100. | - | - | - |
  101. EOF;
  102. $markdownTable .= "\n";
  103. foreach ($percentage as $lang => $value) {
  104. $percentageInt = intval(rtrim($value, '%'));
  105. $completed = intval($percentageInt / 10);
  106. $uncompleted = intval(ceil((100 - $percentageInt) / 10));
  107. $progressBar = str_repeat('■', $completed) . str_repeat('・', $uncompleted);
  108. $ghSearchUrl = 'https://github.com/search?q=' . urlencode("repo:FreshRSS/FreshRSS path:app/i18n/$lang /(TODO|DIRTY)$/");
  109. $markdownTable .= '| ' . implode(' | ', [
  110. _t('gen.lang.' . $lang) . " ($lang)",
  111. $progressBar . ' ' . $percentageInt . '%',
  112. "[__contribute__]($ghSearchUrl)",
  113. ]) . " |\n";
  114. }
  115. // In case we're located in ./cli/
  116. if (!file_exists('constants.php')) {
  117. chdir('..');
  118. }
  119. foreach (array_merge(['README.md'], glob('README.*.md') ?: []) as $readmePath) {
  120. writeToReadme($readmePath, rtrim($markdownTable));
  121. }
  122. exit();
  123. }
  124. if (!$isValidated) {
  125. exit(1);
  126. }
  127. /**
  128. * Find used translation keys in the project
  129. *
  130. * Iterates through all php and phtml files in the whole project and extracts all
  131. * translation keys used.
  132. *
  133. * @return list<string>
  134. */
  135. function findUsedTranslations(): array {
  136. $directory = new RecursiveDirectoryIterator(__DIR__ . '/..');
  137. $iterator = new RecursiveIteratorIterator($directory);
  138. $regex = new RegexIterator($iterator, '/^.+\.(php|phtml)$/i', RecursiveRegexIterator::GET_MATCH);
  139. $usedI18n = [];
  140. foreach ($regex as $file => $value) {
  141. if (!is_string($file) || $file === '') {
  142. continue;
  143. }
  144. $fileContent = file_get_contents($file);
  145. if ($fileContent === false) {
  146. continue;
  147. }
  148. preg_match_all('/_t\([\'"](?P<strings>[^\'"]+)[\'"]/', $fileContent, $matches);
  149. $usedI18n = array_merge($usedI18n, $matches['strings']);
  150. }
  151. return $usedI18n;
  152. }
  153. /**
  154. * Output help message.
  155. */
  156. function checkHelp(): never {
  157. $file = str_replace(__DIR__ . '/', '', __FILE__);
  158. echo <<<HELP
  159. NAME
  160. $file
  161. SYNOPSIS
  162. php $file [OPTION]...
  163. DESCRIPTION
  164. Check if translation files have missing keys or missing translations.
  165. -d, --display-result display results.
  166. -h, --help display this help and exit.
  167. -l, --language=LANG filter by LANG.
  168. -r, --display-report display completion report.
  169. -g, --generate-readme generate translation progress section in readme.
  170. HELP;
  171. exit();
  172. }