1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Itkg\TranslationBundle\Extractor; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\DependencyInjection\ContainerAware; |
6
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
7
|
|
|
use Symfony\Component\Finder\Finder; |
8
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
9
|
|
|
use Symfony\Component\Translation\Loader\LoaderInterface; |
10
|
|
|
use Symfony\Component\Translation\MessageCatalogue; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class TranslationExtractor |
14
|
|
|
*/ |
15
|
|
|
class TranslationExtractor extends ContainerAware |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var Finder |
19
|
|
|
*/ |
20
|
|
|
private $finder; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Filesystem |
24
|
|
|
*/ |
25
|
|
|
private $filesystem; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var MessageCatalogue[] |
29
|
|
|
*/ |
30
|
|
|
private $messageCatalogues = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param Finder $finder |
34
|
|
|
* @param Filesystem $filesystem |
35
|
|
|
*/ |
36
|
3 |
|
public function __construct(Finder $finder, Filesystem $filesystem) |
37
|
|
|
{ |
38
|
3 |
|
$this->finder = $finder; |
39
|
3 |
|
$this->filesystem = $filesystem; |
40
|
3 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $path |
44
|
|
|
* @param string $format |
45
|
|
|
* @param null|string $domain |
46
|
|
|
* |
47
|
|
|
* @return MessageCatalogue[] |
48
|
|
|
*/ |
49
|
3 |
|
public function extract($path, $format, $domain = null) |
50
|
|
|
{ |
51
|
3 |
|
$this->messageCatalogues = []; |
52
|
3 |
|
if (!$this->filesystem->exists($path)) { |
53
|
|
|
throw new \RuntimeException(sprintf('Path %s does not exist', $path)); |
54
|
|
|
} |
55
|
3 |
|
$files = $this->finder->files()->name('/[a-z]+\.[a-z]{2}\.'.$format.'/')->in($path); |
56
|
|
|
|
57
|
|
|
/** @var SplFileInfo $file */ |
58
|
3 |
|
foreach ($files as $file) { |
59
|
3 |
|
list($translationDomain, $language) = explode('.', $file->getFilename()); |
60
|
3 |
|
if ($domain && $domain !== $translationDomain) { |
|
|
|
|
61
|
1 |
|
continue; |
62
|
|
|
} |
63
|
3 |
|
$this->addMessageCatalogue($this->getLoader($format)->load($file->getRealPath(), $language, $translationDomain), $language); |
64
|
3 |
|
} |
65
|
3 |
|
return $this->messageCatalogues; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param MessageCatalogue $messageCatalogue |
70
|
|
|
* @param string $language |
71
|
|
|
*/ |
72
|
3 |
|
private function addMessageCatalogue(MessageCatalogue $messageCatalogue, $language) |
73
|
|
|
{ |
74
|
3 |
|
if (!$messageCatalogue->all()) { |
75
|
|
|
return; |
76
|
|
|
} |
77
|
|
|
|
78
|
3 |
|
if (isset($this->messageCatalogues[$language])) { |
79
|
|
|
$this->messageCatalogues[$language]->addCatalogue($messageCatalogue); |
80
|
|
|
} else { |
81
|
3 |
|
$this->messageCatalogues[$language] = $messageCatalogue; |
82
|
|
|
} |
83
|
3 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $format |
87
|
|
|
* |
88
|
|
|
* @return LoaderInterface |
89
|
|
|
*/ |
90
|
3 |
View Code Duplication |
private function getLoader($format) |
|
|
|
|
91
|
|
|
{ |
92
|
3 |
|
$service = sprintf('translation.loader.%s', $format); |
93
|
|
|
|
94
|
3 |
|
if (!$this->container->has($service)) { |
95
|
|
|
throw new \InvalidArgumentException(sprintf('Unable to find Symfony Translation loader for format "%s"', $format)); |
96
|
|
|
} |
97
|
|
|
|
98
|
3 |
|
return $this->container->get($service); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.