I18nCompletionValidator.php 1.3 KB

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