|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Zikula package. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright Zikula Foundation - http://zikula.org/ |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Zikula\Bundle\CoreBundle\Translation; |
|
13
|
|
|
|
|
14
|
|
|
use JMS\TranslationBundle\Model\FileSource; |
|
15
|
|
|
use JMS\TranslationBundle\Model\Message; |
|
16
|
|
|
use JMS\TranslationBundle\Model\MessageCatalogue; |
|
17
|
|
|
use JMS\TranslationBundle\Translation\Extractor\FileVisitorInterface; |
|
18
|
|
|
|
|
19
|
|
|
class ZikulaJsFileExtractor implements FileVisitorInterface |
|
20
|
|
|
{ |
|
21
|
|
|
const JAVASCRIPT_DOMAIN = 'zikula_javascript'; // @todo figure out way to compute the bundle's translation domain? |
|
22
|
|
|
const SINGULAR_CAPTURE_REGEX = '\s?[\'"]([^"\'),]+)[\'"]\s?'; |
|
23
|
|
|
const PLURAL_CAPTURE_REGEX = '\s?[\'"]([^"\'),]+)[\'"]\s?,\s?[\'"]([^"\'),]+)[\'"]'; |
|
24
|
|
|
const REGEX_DELIMITER = '/'; |
|
25
|
|
|
|
|
26
|
|
|
private $singularFunctions = [ |
|
27
|
|
|
'trans', // should be replaced by vendor eventually |
|
28
|
|
|
'transChoice', // should be replaced by vendor eventually |
|
29
|
|
|
'__', |
|
30
|
|
|
'__f', |
|
31
|
|
|
]; |
|
32
|
|
|
private $pluralFunctions = [ |
|
33
|
|
|
'_n', |
|
34
|
|
|
'_fn' |
|
35
|
|
|
]; |
|
36
|
|
|
|
|
37
|
|
|
public function visitFile(\SplFileInfo $file, MessageCatalogue $catalogue) |
|
38
|
|
|
{ |
|
39
|
|
|
if ('.js' !== substr($file, -3)) { |
|
40
|
|
|
return; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
// singular type |
|
44
|
|
|
$argumentsRegex = self::REGEX_DELIMITER |
|
45
|
|
|
.'\.(?:' . implode('|', $this->singularFunctions) . ')\(' |
|
46
|
|
|
.self::SINGULAR_CAPTURE_REGEX |
|
47
|
|
|
.self::REGEX_DELIMITER; |
|
48
|
|
|
preg_match_all($argumentsRegex, file_get_contents($file), $matches); |
|
49
|
|
|
foreach ($matches[1] as $string) { |
|
50
|
|
|
$message = new Message($string, self::JAVASCRIPT_DOMAIN); |
|
51
|
|
|
$message->addSource(new FileSource((string) $file)); |
|
52
|
|
|
$catalogue->add($message); |
|
53
|
|
|
} |
|
54
|
|
|
// plural type |
|
55
|
|
|
$argumentsRegex = self::REGEX_DELIMITER |
|
56
|
|
|
.'\.(?:' . implode('|', $this->pluralFunctions) . ')\(' |
|
57
|
|
|
.self::PLURAL_CAPTURE_REGEX |
|
58
|
|
|
.self::REGEX_DELIMITER; |
|
59
|
|
|
preg_match_all($argumentsRegex, file_get_contents($file), $matches); |
|
60
|
|
|
foreach ($matches[1] as $key => $singluar) { |
|
61
|
|
|
$fullString = $singluar . '|' . $matches[2][$key]; |
|
62
|
|
|
$message = new Message($fullString, self::JAVASCRIPT_DOMAIN); |
|
63
|
|
|
$message->addSource(new FileSource((string) $file)); |
|
64
|
|
|
$catalogue->add($message); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function visitPhpFile(\SplFileInfo $file, MessageCatalogue $catalogue, array $ast) |
|
69
|
|
|
{ |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function visitTwigFile(\SplFileInfo $file, MessageCatalogue $catalogue, \Twig_Node $node) |
|
73
|
|
|
{ |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|