I18nCompletionValidator.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. if ($this->passEntries > $this->totalEntries) {
  15. throw new \RuntimeException('The number of translated strings cannot be higher than the number of strings');
  16. }
  17. if ($this->totalEntries === 0) {
  18. return 'There is no data.' . PHP_EOL;
  19. }
  20. return sprintf('Translation is %5.1f%% complete.', $this->passEntries / $this->totalEntries * 100) . PHP_EOL;
  21. }
  22. public function displayResult() {
  23. return $this->result;
  24. }
  25. public function validate() {
  26. foreach ($this->reference as $file => $data) {
  27. foreach ($data as $refKey => $refValue) {
  28. $this->totalEntries++;
  29. if (!array_key_exists($file, $this->language) || !array_key_exists($refKey, $this->language[$file])) {
  30. $this->result .= "Missing key $refKey" . PHP_EOL;
  31. continue;
  32. }
  33. $value = $this->language[$file][$refKey];
  34. if ($value->isIgnore()) {
  35. $this->passEntries++;
  36. continue;
  37. }
  38. if ($refValue->equal($value)) {
  39. $this->result .= "Untranslated key $refKey - $refValue" . PHP_EOL;
  40. continue;
  41. }
  42. $this->passEntries++;
  43. }
  44. }
  45. return $this->totalEntries === $this->passEntries;
  46. }
  47. }