I18nCompletionValidator.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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($ignore) {
  20. foreach ($this->reference as $file => $data) {
  21. foreach ($data as $key => $value) {
  22. $this->totalEntries++;
  23. if (is_array($ignore) && in_array($key, $ignore)) {
  24. $this->passEntries++;
  25. continue;
  26. }
  27. if (!array_key_exists($key, $this->language[$file])) {
  28. $this->result .= sprintf('Missing key %s', $key) . PHP_EOL;
  29. continue;
  30. }
  31. if ($value === $this->language[$file][$key]) {
  32. $this->result .= sprintf('Untranslated key %s - %s', $key, $value) . PHP_EOL;
  33. continue;
  34. }
  35. $this->passEntries++;
  36. }
  37. }
  38. return $this->totalEntries === $this->passEntries;
  39. }
  40. }