check.translation.php 2.8 KB

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