|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the openl10n package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Matthieu Moquet <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Openl10n\Component\Translation\MessageCatalogue; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\Translation\Loader\LoaderInterface as ResourceLoader; |
|
15
|
|
|
use Symfony\Component\Translation\MessageCatalogue; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Load catalogue from resources. |
|
19
|
|
|
*/ |
|
20
|
|
|
class ResourceCatalogueLoader implements MessageCatalogueLoader |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var LoaderInterface[] |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $loaders = []; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $resources = []; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Adds a Loader. |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $format The name of the loader (@see addResource()) |
|
36
|
|
|
* @param ResourceLoader $loader A LoaderInterface instance |
|
37
|
|
|
*/ |
|
38
|
|
|
public function addResourceLoader($format, ResourceLoader $loader) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->loaders[$format] = $loader; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Adds a Resource. |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $format The name of the loader (@see addLoader()) |
|
47
|
|
|
* @param mixed $resource The resource name |
|
48
|
|
|
* @param string $locale The locale |
|
49
|
|
|
* @param string $domain The domain |
|
50
|
|
|
* |
|
51
|
|
|
* @throws \InvalidArgumentException If the locale contains invalid characters |
|
52
|
|
|
*/ |
|
53
|
|
|
public function addResource($format, $resource, $locale, $domain = null) |
|
54
|
|
|
{ |
|
55
|
|
|
if (null === $domain) { |
|
56
|
|
|
$domain = 'messages'; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$this->resources[$locale][] = [$format, $resource, $domain]; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Traverses all resources to fetch messages only for the given locale. |
|
64
|
|
|
* |
|
65
|
|
|
* {@inheritdoc} |
|
66
|
|
|
*/ |
|
67
|
|
|
public function loadCatalogue($locale) |
|
68
|
|
|
{ |
|
69
|
|
|
// Create a new catalogue |
|
70
|
|
|
$catalogue = new MessageCatalogue($locale); |
|
71
|
|
|
|
|
72
|
|
|
// Traverse resources to complete catalogue |
|
73
|
|
|
$resources = isset($this->resources[$locale]) ? $this->resources[$locale] : []; |
|
74
|
|
|
foreach ($resources as $resource) { |
|
75
|
|
|
$name = $resource[0]; |
|
76
|
|
|
$format = $resource[1]; |
|
77
|
|
|
$domain = $resource[2]; |
|
78
|
|
|
|
|
79
|
|
|
if (!isset($this->loaders[$name])) { |
|
80
|
|
|
throw new \RuntimeException("The \"$name\" translation loader is not registered."); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$catalogue->addCatalogue( |
|
84
|
|
|
$this->loaders[$name]->load($format, $locale, $domain) |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $catalogue; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|