summaryrefslogtreecommitdiff
path: root/tools/inc/lib/jsgettext/jsgettext.php
blob: 75ea6566ec822710006ea31bfa11a6352e69219a (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
<?php

	require_once 'JSParser.php';
	require_once 'PoeditParser.php';

	function buildOptions($args) {
		$options = ['files' => [], '-o'	=> null, '-k'	=> '_'];
		$len = is_countable($args) ? count($args) : 0;
		$i = 1;
		while ($i < $len) {
			if (preg_match('#^-[a-z]$#i', (string) $args[$i])) {
				$options[$args[$i]] = isset($args[$i+1]) ? trim((string) $args[$i+1]) : true;
				$i += 2;
			}
			else {
				$options['files'][] = $args[$i];
				$i++;
			}
		}
		return $options;
	}

	$options = buildOptions($argv);

	if (!file_exists($options['-o'])) {
		touch($options['-o']);
	}

	if (!is_writable($options['-o'])) {
		die("Invalid output file name. Make sure it exists and is writable.");
	}

	$inputFiles = $options['files'];

	if (empty($inputFiles)) {
		die("You did not provide any input file.");
	}

	$poeditParser = new PoeditParser($options['-o']);
	$poeditParser->parse();

	$errors = [];

	foreach ($inputFiles as $f) {
		if (!is_readable($f) || !preg_match('#\.js$#', (string) $f)) {
			$errors[] = ("$f is not a valid javascript file.");
			continue;
		}
		$jsparser = new JSParser($f, explode(' ', (string) $options['-k']));
		$jsStrings = $jsparser->parse();
		$poeditParser->merge($jsStrings);
	}

	if (!empty($errors)) {
		echo "\nThe following errors occured:\n" . implode("\n", $errors) . "\n";
	}

	$poeditParser->save();
?>