-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwig-gettext-extractor
More file actions
executable file
·93 lines (77 loc) · 2.98 KB
/
twig-gettext-extractor
File metadata and controls
executable file
·93 lines (77 loc) · 2.98 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env php
<?php
/**
* This file is part of the Twig Gettext utility.
*
* (c) Саша Стаменковић <umpirsky@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use Twig\Gettext\Util\DebugLogger;
// Init composer
if (file_exists($a = __DIR__.'/../../autoload.php')) {
require_once $a;
} else {
require_once __DIR__.'/vendor/autoload.php';
}
// Init twig + core extensions
$twig = new Twig_Environment(new Twig\Gettext\Loader\Filesystem(DIRECTORY_SEPARATOR), array(
'cache' => implode(DIRECTORY_SEPARATOR, array(sys_get_temp_dir(), 'cache', uniqid()) ),
'auto_reload' => true
));
$twig->addExtension(new Twig_Extensions_Extension_I18n());
// Read argv for config
array_shift($_SERVER['argv']);
$argvReadingTemplates = false;
$argvReadingFilters = false;
$resetArgv = function () use (&$argvReadingFilters, &$argvReadingTemplates) {
$argvReadingFilters = false;
$argvReadingTemplates = false;
};
try {
$extractor = new Twig\Gettext\Extractor($twig);
/**
* Syntax for argv:
*
* twig-gettext-extractor <xgettext arguments> <extractor arguments>
*
* xgettext arguments can be "--sort-output", "--force-po", etc and are processed directly.
* extractor arguments are specific to the twig extractor
*
* --files Add a template file to the list of files that need to be extracted.
* --filters Add a dummy filter that is custom to the project to prevent the extractor breaking.
*/
foreach ($_SERVER['argv'] as $argument) {
// Command starters ------------------------------------------------------------------------------------------------
if ($argument === '--filters') {
$resetArgv();
$argvReadingFilters = true;
continue;
}
if ($argument === '--files') {
$resetArgv();
$argvReadingTemplates = true;
continue;
}
// Command parameters ----------------------------------------------------------------------------------------------
if ($argvReadingFilters) {
// We are in a --filters statement
$twig->addFilter(new Twig_SimpleFilter($argument, function ($input) {
return $input;
}));
} else if ($argvReadingTemplates) {
// We are in a --files statement
$extractor->addTemplate(getcwd() . DIRECTORY_SEPARATOR . $argument);
} else {
// We are not in a statement, process as gettext parameter
$extractor->addGettextParameter($argument);
}
}
$extractor->extract();
} catch (Exception $ex) {
DebugLogger::log("A Twig gettext extractor run has failed with an exception.");
DebugLogger::log("Command: " . print_r($argv, true));
DebugLogger::log("Exception: " . print_r($ex, true));
exit(-1); // Die with a non zero exit code so poedit knows we failed
}