check.translation.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. $languageNameIssues = $i18nData->validateLanguageNames();
  41. foreach ($languageNameIssues as $issue) {
  42. fwrite(STDERR, "Error: {$issue}\n");
  43. $isValidated = false;
  44. }
  45. $result = [];
  46. $report = [];
  47. $percentage = [];
  48. foreach ($languages as $language) {
  49. if ($language === $i18nData::REFERENCE_LANGUAGE) {
  50. $usedTranslations = findUsedTranslations();
  51. $referenceLanguage = $i18nData->getReferenceLanguage();
  52. $pluralFamilies = loadPluralReferenceFamilies($referenceLanguage);
  53. if ($pluralFamilies !== []) {
  54. $referenceLanguage['plurals.php'] = $pluralFamilies;
  55. }
  56. $i18nValidator = new I18nUsageValidator($referenceLanguage, $usedTranslations['keys'], $usedTranslations['prefixes']);
  57. } else {
  58. $i18nValidator = new I18nCompletionValidator($i18nData->getReferenceLanguage(), $i18nData->getLanguage($language));
  59. }
  60. $isValidated = $i18nValidator->validate() && $isValidated;
  61. $report[$language] = sprintf('%-5s - %s', $language, $i18nValidator->displayReport());
  62. $percentage[$language] = $i18nValidator->displayReport(percentage_only: true);
  63. $result[$language] = $i18nValidator->displayResult();
  64. }
  65. if ($cliOptions->displayResult) {
  66. foreach ($result as $lang => $value) {
  67. echo 'Language: ', $lang, PHP_EOL;
  68. print_r($value);
  69. echo PHP_EOL;
  70. }
  71. }
  72. if ($cliOptions->displayReport) {
  73. foreach ($report as $value) {
  74. echo $value;
  75. }
  76. }
  77. function writeToReadme(string $readmePath, string $markdownTable): void {
  78. $language = explode('.', $readmePath)[1];
  79. // expecting `README.md` for `en` or `README.fr.md` for `fr`
  80. if ($language === 'md') {
  81. $language = 'en';
  82. }
  83. Minz_Translate::init($language);
  84. $placeholders = [];
  85. if (preg_match_all('/__.*?__/', $markdownTable, $placeholders) === false) {
  86. echo 'Error: Fail while matching translation placeholders', PHP_EOL;
  87. exit(1);
  88. }
  89. foreach (array_unique($placeholders[0]) as $_ => $placeholder) {
  90. $markdownTable = str_replace($placeholder, _t('gen.readme.' . substr($placeholder, 2, -2)), $markdownTable);
  91. }
  92. $readme = file_get_contents($readmePath);
  93. if ($readme === false) {
  94. echo 'Error: Unable to open ' . $readmePath, PHP_EOL;
  95. exit(1);
  96. }
  97. if (file_put_contents($readmePath, preg_replace('/<translations>(.*?)<\/translations>/s', <<<EOF
  98. <translations>
  99. <!-- This section is automatically generated by `./cli/check.translation.php -g` -->
  100. $markdownTable
  101. </translations>
  102. EOF, $readme)) === false) {
  103. echo 'Error: Fail while writing to ' . $readmePath, PHP_EOL;
  104. exit(1);
  105. }
  106. echo 'Successfully written translation status into ' . $readmePath, PHP_EOL;
  107. }
  108. if ($cliOptions->generateReadme) {
  109. if ($languageNameIssues !== []) {
  110. // Refuse to regenerate the README when language directory names and
  111. // `gen.lang.*` keys disagree, otherwise we would silently produce a
  112. // corrupt translation table (e.g. literal `gen.lang.*` keys instead of
  113. // localised language names). Routine incomplete translations are fine.
  114. exit(1);
  115. }
  116. $markdownTable = <<<EOF
  117. | __language__ | __translated__ | |
  118. | - | - | - |
  119. EOF;
  120. $markdownTable .= "\n";
  121. foreach ($percentage as $lang => $value) {
  122. $percentageInt = intval(rtrim($value, '%'));
  123. $completed = intval($percentageInt / 10);
  124. $uncompleted = intval(ceil((100 - $percentageInt) / 10));
  125. $progressBar = str_repeat('■', $completed) . str_repeat('・', $uncompleted);
  126. $ghSearchUrl = 'https://github.com/search?q=' . urlencode("repo:FreshRSS/FreshRSS path:app/i18n/$lang /(TODO|DIRTY)$/");
  127. $markdownTable .= '| ' . implode(' | ', [
  128. _t('gen.lang.' . $lang) . " ($lang)",
  129. $progressBar . ' ' . $percentageInt . '%',
  130. "[__contribute__]($ghSearchUrl)",
  131. ]) . " |\n";
  132. }
  133. // In case we're located in ./cli/
  134. if (!file_exists('constants.php')) {
  135. chdir('..');
  136. }
  137. foreach (array_merge(['README.md'], glob('README.*.md') ?: []) as $readmePath) {
  138. writeToReadme($readmePath, rtrim($markdownTable));
  139. }
  140. exit();
  141. }
  142. if (!$isValidated) {
  143. exit(1);
  144. }
  145. /**
  146. * Find used translation keys in the project
  147. *
  148. * Iterates through all php and phtml files in the whole project and extracts all
  149. * translation keys used.
  150. *
  151. * @return array{keys:list<string>,prefixes:list<string>}
  152. */
  153. function findUsedTranslations(): array {
  154. $directory = new RecursiveDirectoryIterator(__DIR__ . '/..', FilesystemIterator::SKIP_DOTS);
  155. $iterator = new RecursiveIteratorIterator($directory, RecursiveIteratorIterator::LEAVES_ONLY, RecursiveIteratorIterator::CATCH_GET_CHILD);
  156. $regex = new RegexIterator($iterator, '/^.+\.(php|phtml)$/i', RecursiveRegexIterator::GET_MATCH);
  157. $usedI18n = [];
  158. $usedPrefixes = [];
  159. foreach ($regex as $file => $value) {
  160. if (!is_string($file) || $file === '') {
  161. continue;
  162. }
  163. $fileContent = file_get_contents($file);
  164. if ($fileContent === false) {
  165. continue;
  166. }
  167. preg_match_all('/_t\([\'"](?P<strings>[^\'"]+)[\'"]/', $fileContent, $matches);
  168. $usedI18n = array_merge($usedI18n, $matches['strings']);
  169. preg_match_all('/Minz_Translate::plural\(\s*[\'"](?P<string>[^\'"]+)[\'"](?P<dynamic>\s*\.)?/', $fileContent, $pluralMatches, PREG_SET_ORDER);
  170. foreach ($pluralMatches as $match) {
  171. $string = $match['string'];
  172. if (($match['dynamic'] ?? '') !== '') {
  173. $usedPrefixes[] = $string;
  174. } else {
  175. $usedI18n[] = $string;
  176. }
  177. }
  178. }
  179. return [
  180. 'keys' => array_values(array_unique($usedI18n)),
  181. 'prefixes' => array_values(array_unique($usedPrefixes)),
  182. ];
  183. }
  184. /**
  185. * @param array<string,array<string,I18nValue>> $referenceLanguage
  186. * @return array<string,I18nValue>
  187. */
  188. function loadPluralReferenceFamilies(array $referenceLanguage): array {
  189. $pluralFamilies = [];
  190. foreach ($referenceLanguage as $values) {
  191. foreach ($values as $key => $value) {
  192. if (preg_match('/^(?P<base>.+)\.(?P<index>\d+)$/', $key, $matches) !== 1) {
  193. continue;
  194. }
  195. $baseKey = $matches['base'];
  196. $index = $matches['index'];
  197. $pluralFamilies[$baseKey][(int)$index] = $value->__toString();
  198. }
  199. }
  200. $normalisedFamilies = [];
  201. foreach ($pluralFamilies as $baseKey => $messageFamily) {
  202. $messages = [];
  203. ksort($messageFamily);
  204. foreach ($messageFamily as $message) {
  205. if ($message !== '') {
  206. $messages[] = $message;
  207. }
  208. }
  209. $normalisedFamilies[$baseKey] = new I18nValue(implode(' | ', $messages));
  210. }
  211. return $normalisedFamilies;
  212. }
  213. /**
  214. * Output help message.
  215. */
  216. function checkHelp(): never {
  217. $file = str_replace(__DIR__ . '/', '', __FILE__);
  218. echo <<<HELP
  219. NAME
  220. $file
  221. SYNOPSIS
  222. php $file [OPTION]...
  223. DESCRIPTION
  224. Check if translation files have missing keys or missing translations.
  225. -d, --display-result display results.
  226. -h, --help display this help and exit.
  227. -l, --language=LANG filter by LANG.
  228. -r, --display-report display completion report.
  229. -g, --generate-readme generate translation progress section in readme.
  230. HELP;
  231. exit();
  232. }