-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathtwig-gettext-extractor
executable file
·81 lines (71 loc) · 2.48 KB
/
twig-gettext-extractor
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
#!/usr/bin/env php
<?php
/**
* This file is part of the Twig Gettext utility.
*
* (c) Саша Стаменковић <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Extracts translations from twig templates.
*
* @author Саша Стаменковић <[email protected]>
*/
if (file_exists($a = __DIR__ . '/../../autoload.php')) {
require_once $a;
} else {
require_once __DIR__ . '/vendor/autoload.php';
}
$twig = new Twig_Environment(new Twig\Gettext\Loader\Filesystem(DIRECTORY_SEPARATOR), [
'cache' => implode(DIRECTORY_SEPARATOR, [sys_get_temp_dir(), 'cache', uniqid()]),
'auto_reload' => true,
]);
$twig->addExtension(new Twig_Extensions_Extension_I18n());
$twig->addExtension(new Symfony\Bridge\Twig\Extension\TranslationExtension(
new Symfony\Component\Translation\Translator(null)
));
$twig->addExtension(new Symfony\Bridge\Twig\Extension\RoutingExtension(
new Twig\Gettext\Routing\Generator\UrlGenerator()
));
$twig->addExtension(new Symfony\Bridge\Twig\Extension\FormExtension());
$twig->addExtension(new Symfony\Bridge\Twig\Extension\AssetExtension(
new Symfony\Component\Asset\Packages()
));
// You can add more extensions here, or via command line with the --functions and --filter options
array_shift($_SERVER['argv']);
$setFunctions = false;
$setFilters = false;
$addTemplate = false;
$setExecutable = false;
$extractor = new Twig\Gettext\Extractor($twig);
foreach ($_SERVER['argv'] as $arg) {
if ('--files' === $arg) {
$addTemplate = true;
} else if ($addTemplate) {
$extractor->addTemplate(getcwd() . DIRECTORY_SEPARATOR . $arg);
} else if ('--exec' === $arg) {
$setExecutable = true;
} else if ($setExecutable) {
$extractor->setExecutable($arg);
$setExecutable = false;
} else if ('--functions' === $arg) {
$setFunctions = true;
} else if ($setFunctions) {
foreach (explode(',', $arg) as $functionName) {
$twig->addFunction(new \Twig_SimpleFunction($functionName, true));
}
$setFunctions = false;
} else if ('--filters' === $arg) {
$setFilters = true;
} else if ($setFilters) {
foreach (explode(',', $arg) as $filterName) {
$twig->addFilter(new \Twig_SimpleFilter($filterName, true));
}
$setFilters = false;
} else {
$extractor->addGettextParameter($arg);
}
}
$extractor->extract();