summaryrefslogtreecommitdiff
path: root/cli/i18n/I18nIgnoreFile.php
blob: 714e000ca6c61450ee8d3c499853ec24dd64026c (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php

require_once __DIR__ . '/I18nData.php';
require_once __DIR__ . '/I18nFileInterface.php';

class I18nIgnoreFile implements I18nFileInterface {

	private $i18nPath;

	public function __construct() {
		$this->i18nPath = __DIR__ . '/ignore';
	}

	public function dump(I18nData $i18n) {
		foreach ($i18n->getData() as $language => $content) {
			$filename = $this->i18nPath . DIRECTORY_SEPARATOR . $language . '.php';
			file_put_contents($filename, $this->format($content));
		}
	}

	public function load() {
		$i18n = array();
		$files = new DirectoryIterator($this->i18nPath);
		foreach ($files as $file) {
			if (!$file->isFile()) {
				continue;
			}
			$i18n[$file->getBasename('.php')] = (include $file->getPathname());
		}

		return new I18nData($i18n);
	}

	/**
	 * Format an array of translation
	 *
	 * It takes an array of translation and format it to be dumped in a
	 * translation file. The array is first converted to a string then some
	 * formatting regexes are applied to match the original content.
	 *
	 * @param array $translation
	 * @return string
	 */
	private function format($translation) {
		$translation = var_export(($translation), true);
		$patterns = array(
			'/array \(/',
			'/=>\s*array/',
			'/ {2}/',
			'/\d+ => /',
		);
		$replacements = array(
			'array(',
			'=> array',
			"\t", // Double quoting is mandatory to have a tab instead of the \t string
			'',
		);
		$translation = preg_replace($patterns, $replacements, $translation);

		// Double quoting is mandatory to have new lines instead of \n strings
		return sprintf("<?php\n\nreturn %s;\n", $translation);
	}

}