check.translation.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 $markdownImgStr): void {
  67. $readme = file_get_contents($readmePath);
  68. if ($readme === false) {
  69. echo 'Error: Unable to open ' . $readmePath, PHP_EOL;
  70. exit(1);
  71. }
  72. if (file_put_contents($readmePath, preg_replace('/<translations>(.*?)<\/translations>/s', <<<EOF
  73. <translations>
  74. <!-- This section is automatically generated by `cli/check.translation.php -g` -->
  75. $markdownImgStr
  76. </translations>
  77. EOF, $readme)) === false) {
  78. echo 'Error: Fail while writing to ' . $readmePath, PHP_EOL;
  79. exit(1);
  80. }
  81. echo 'Successfully written translation status into ' . $readmePath, PHP_EOL;
  82. }
  83. function embedSvg(string $contents): string {
  84. return preg_replace(
  85. '/<svg\s+(?:(?:[^>]*?)(xmlns=["\'][^"\']+["\']))?(?:(?:[^>]*?)(viewBox=["\'][^"\']+["\']))?(?:[^>]*?)>/i',
  86. '<svg \1 \2 width="16" height="16" x="9" y="2">',
  87. $contents
  88. ) ?? '';
  89. }
  90. if ($cliOptions->generateReadme) {
  91. $supportedFormats = ['txt', 'svg'];
  92. $flagsDir = dirname(__DIR__) . '/docs/i18n/flags';
  93. $markdownImgStr = '';
  94. foreach ($percentage as $lang => $value) {
  95. $percentageInt = intval(rtrim($value, '%'));
  96. $color = 'green';
  97. if ($percentageInt < 90) {
  98. $color = 'gold';
  99. }
  100. if ($percentageInt < 70) {
  101. $color = 'darkred';
  102. }
  103. $svgFile = $flagsDir . '/' . $lang . '.svg';
  104. $svg = '';
  105. if (file_exists($svgFile)) {
  106. $svg = file_get_contents($svgFile);
  107. if ($svg === false) {
  108. echo 'Error: Unable to open ' . $svgFile, PHP_EOL;
  109. exit(1);
  110. }
  111. }
  112. $ghSearchUrl = 'https://github.com/search?q=' . urlencode("repo:FreshRSS/FreshRSS path:app/i18n/$lang /(TODO|DIRTY)$/");
  113. $genPath = $flagsDir . '/gen/' . $lang . '.svg';
  114. $template = '<!-- This file is automatically generated by `cli/check.translation.php -g` -->' . "\n";
  115. if ($svg === '') {
  116. $i18nGen = include dirname(__DIR__) . "/app/i18n/$lang/gen.php";
  117. if (!is_array($i18nGen) || !is_string($i18nGen['flag'] ?? null)) {
  118. echo 'Error: No Unicode flag found for language ' . $lang, PHP_EOL;
  119. exit(1);
  120. }
  121. $unicodeFlag = $i18nGen['flag'];
  122. if ($lang !== 'en' && $unicodeFlag === '🇬🇧') {
  123. echo 'Error: Wrong Unicode flag for language ' . $lang, PHP_EOL;
  124. exit(1);
  125. }
  126. $value = $unicodeFlag . ' ' . $percentageInt . '%';
  127. $template .= <<<EOF
  128. <svg xmlns="http://www.w3.org/2000/svg" width="70" height="20">
  129. <g fill="white" font-size="12" font-family="Verdana" text-anchor="middle">
  130. <rect rx="3" width="70" height="20" fill="$color" />
  131. <text x="34" y="14">$value</text>
  132. </g>
  133. </svg>
  134. EOF;
  135. } else {
  136. // An SVG file is available to override the Unicode flag
  137. $value = $percentageInt . '%';
  138. $contents = embedSvg($svg);
  139. $template .= <<<EOF
  140. <svg xmlns="http://www.w3.org/2000/svg" width="70" height="20">
  141. <g fill="white" font-size="12" font-family="Verdana" text-anchor="middle">
  142. <rect rx="3" width="70" height="20" fill="$color" />
  143. <!-- embedded SVG -->
  144. $contents
  145. <!-- end of embedded SVG -->
  146. <text x="43" y="14">$value</text>
  147. </g>
  148. </svg>
  149. EOF;
  150. }
  151. if (file_put_contents($genPath, $template) === false) {
  152. echo 'Error: Fail while writing to ' . $genPath, PHP_EOL;
  153. exit(1);
  154. }
  155. $markdownImgStr .= "[![$lang](./docs/i18n/flags/gen/$lang.svg)]($ghSearchUrl) ";
  156. }
  157. // In case we're located in ./cli/
  158. if (!file_exists('constants.php')) {
  159. chdir('..');
  160. }
  161. foreach (array_merge(['README.md'], glob('README.*.md') ?: []) as $readmePath) {
  162. writeToReadme($readmePath, rtrim($markdownImgStr));
  163. }
  164. exit();
  165. }
  166. if (!$isValidated) {
  167. exit(1);
  168. }
  169. /**
  170. * Find used translation keys in the project
  171. *
  172. * Iterates through all php and phtml files in the whole project and extracts all
  173. * translation keys used.
  174. *
  175. * @return list<string>
  176. */
  177. function findUsedTranslations(): array {
  178. $directory = new RecursiveDirectoryIterator(__DIR__ . '/..');
  179. $iterator = new RecursiveIteratorIterator($directory);
  180. $regex = new RegexIterator($iterator, '/^.+\.(php|phtml)$/i', RecursiveRegexIterator::GET_MATCH);
  181. $usedI18n = [];
  182. foreach (array_keys(iterator_to_array($regex)) as $file) {
  183. if (!is_string($file) || $file === '') {
  184. continue;
  185. }
  186. $fileContent = file_get_contents($file);
  187. if ($fileContent === false) {
  188. continue;
  189. }
  190. preg_match_all('/_t\([\'"](?P<strings>[^\'"]+)[\'"]/', $fileContent, $matches);
  191. $usedI18n = array_merge($usedI18n, $matches['strings']);
  192. }
  193. return $usedI18n;
  194. }
  195. /**
  196. * Output help message.
  197. */
  198. function checkHelp(): never {
  199. $file = str_replace(__DIR__ . '/', '', __FILE__);
  200. echo <<<HELP
  201. NAME
  202. $file
  203. SYNOPSIS
  204. php $file [OPTION]...
  205. DESCRIPTION
  206. Check if translation files have missing keys or missing translations.
  207. -d, --display-result display results.
  208. -h, --help display this help and exit.
  209. -l, --language=LANG filter by LANG.
  210. -r, --display-report display completion report.
  211. -g, --generate-readme generate readme for translation status.
  212. HELP;
  213. exit();
  214. }