check.translation.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. require_once __DIR__ . '/i18n/I18nCompletionValidator.php';
  3. require_once __DIR__ . '/i18n/I18nData.php';
  4. require_once __DIR__ . '/i18n/I18nFile.php';
  5. require_once __DIR__ . '/i18n/I18nIgnoreFile.php';
  6. require_once __DIR__ . '/i18n/I18nUsageValidator.php';
  7. $i18nFile = new I18nFile();
  8. $i18nIgnoreFile = new I18nIgnoreFile();
  9. $i18nData = new I18nData($i18nFile->load(), $i18nIgnoreFile->load());
  10. $options = getopt("dhl:r");
  11. if (array_key_exists('h', $options)) {
  12. help();
  13. }
  14. if (array_key_exists('l', $options)) {
  15. $languages = array($options['l']);
  16. } else {
  17. $languages = $i18nData->getAvailableLanguages();
  18. }
  19. $displayResults = array_key_exists('d', $options);
  20. $displayReport = array_key_exists('r', $options);
  21. $isValidated = true;
  22. $result = array();
  23. $report = array();
  24. foreach ($languages as $language) {
  25. if ($language === $i18nData::REFERENCE_LANGUAGE) {
  26. $i18nValidator = new I18nUsageValidator($i18nData->getReferenceLanguage(), findUsedTranslations());
  27. $isValidated = $i18nValidator->validate(include __DIR__ . '/i18n/ignore/' . $language . '.php') && $isValidated;
  28. } else {
  29. $i18nValidator = new I18nCompletionValidator($i18nData->getReferenceLanguage(), $i18nData->getLanguage($language));
  30. if (file_exists(__DIR__ . '/i18n/ignore/' . $language . '.php')) {
  31. $isValidated = $i18nValidator->validate(include __DIR__ . '/i18n/ignore/' . $language . '.php') && $isValidated;
  32. } else {
  33. $isValidated = $i18nValidator->validate(null) && $isValidated;
  34. }
  35. }
  36. $report[$language] = sprintf('%-5s - %s', $language, $i18nValidator->displayReport());
  37. $result[$language] = $i18nValidator->displayResult();
  38. }
  39. if ($displayResults) {
  40. foreach ($result as $lang => $value) {
  41. echo 'Language: ', $lang, PHP_EOL;
  42. print_r($value);
  43. echo PHP_EOL;
  44. }
  45. }
  46. if ($displayReport) {
  47. foreach ($report as $value) {
  48. echo $value;
  49. }
  50. }
  51. if (!$isValidated) {
  52. exit(1);
  53. }
  54. /**
  55. * Find used translation keys in the project
  56. *
  57. * Iterates through all php and phtml files in the whole project and extracts all
  58. * translation keys used.
  59. *
  60. * @return array
  61. */
  62. function findUsedTranslations() {
  63. $directory = new RecursiveDirectoryIterator(__DIR__ . '/..');
  64. $iterator = new RecursiveIteratorIterator($directory);
  65. $regex = new RegexIterator($iterator, '/^.+\.(php|phtml)$/i', RecursiveRegexIterator::GET_MATCH);
  66. $usedI18n = array();
  67. foreach (array_keys(iterator_to_array($regex)) as $file) {
  68. $fileContent = file_get_contents($file);
  69. preg_match_all('/_t\([\'"](?P<strings>[^\'"]+)[\'"]/', $fileContent, $matches);
  70. $usedI18n = array_merge($usedI18n, $matches['strings']);
  71. }
  72. return $usedI18n;
  73. }
  74. /**
  75. * Output help message.
  76. */
  77. function help() {
  78. $help = <<<HELP
  79. NAME
  80. %s
  81. SYNOPSIS
  82. php %s [OPTION]...
  83. DESCRIPTION
  84. Check if translation files have missing keys or missing translations.
  85. -d display results.
  86. -h display this help and exit.
  87. -l=LANG filter by LANG.
  88. -r display completion report.
  89. HELP;
  90. $file = str_replace(__DIR__ . '/', '', __FILE__);
  91. echo sprintf($help, $file, $file);
  92. exit;
  93. }