aboutsummaryrefslogtreecommitdiff
path: root/cli/i18n/I18nCompletionValidator.php
blob: 2cb71acd50a18497e6332679b2a5d640e83eab96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php

require_once __DIR__ . '/I18nValidatorInterface.php';

class I18nCompletionValidator implements I18nValidatorInterface {

	private $reference;
	private $language;
	private $totalEntries = 0;
	private $passEntries = 0;
	private $result = '';

	public function __construct($reference, $language) {
		$this->reference = $reference;
		$this->language = $language;
	}

	public function displayReport() {
		return sprintf('Translation is %5.1f%% complete.', $this->passEntries / $this->totalEntries * 100) . PHP_EOL;
	}

	public function displayResult() {
		return $this->result;
	}

	public function validate($ignore) {
		foreach ($this->reference as $file => $data) {
			foreach ($data as $key => $value) {
				$this->totalEntries++;
				if (is_array($ignore) && in_array($key, $ignore)) {
					$this->passEntries++;
					continue;
				}
				if (!array_key_exists($key, $this->language[$file])) {
					$this->result .= sprintf('Missing key %s', $key) . PHP_EOL;
					continue;
				}
				if ($value === $this->language[$file][$key]) {
					$this->result .= sprintf('Untranslated key %s - %s', $key, $value) . PHP_EOL;
					continue;
				}
				$this->passEntries++;
			}
		}

		return $this->totalEntries === $this->passEntries;
	}

}