check.translation.php 2.6 KB

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