I18nCompletionValidator.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. require_once __DIR__ . '/I18nValidatorInterface.php';
  3. class I18nCompletionValidator implements I18nValidatorInterface {
  4. private $reference;
  5. private $language;
  6. private $totalEntries = 0;
  7. private $passEntries = 0;
  8. private $result = '';
  9. public function __construct($reference, $language) {
  10. $this->reference = $reference;
  11. $this->language = $language;
  12. }
  13. public function displayReport() {
  14. return sprintf('Translation is %5.1f%% complete.', $this->passEntries / $this->totalEntries * 100) . PHP_EOL;
  15. }
  16. public function displayResult() {
  17. return $this->result;
  18. }
  19. public function validate() {
  20. foreach ($this->reference as $file => $data) {
  21. foreach ($data as $refKey => $refValue) {
  22. $this->totalEntries++;
  23. if (!array_key_exists($refKey, $this->language[$file])) {
  24. $this->result .= "Missing key $refKey" . PHP_EOL;
  25. continue;
  26. }
  27. $value = $this->language[$file][$refKey];
  28. if ($value->isIgnore()) {
  29. $this->passEntries++;
  30. continue;
  31. }
  32. if ($refValue->equal($value)) {
  33. $this->result .= "Untranslated key $refKey - $refValue" . PHP_EOL;
  34. continue;
  35. }
  36. $this->passEntries++;
  37. }
  38. }
  39. return $this->totalEntries === $this->passEntries;
  40. }
  41. }