Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console-core / src / Utils / TranslatorManager.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Core\Utils\TranslatorManager.
6  */
7
8 namespace Drupal\Console\Core\Utils;
9
10 use Symfony\Component\Translation\Translator;
11 use Symfony\Component\Translation\Loader\YamlFileLoader;
12 use Symfony\Component\Translation\Loader\ArrayLoader;
13 use Symfony\Component\Finder\Finder;
14 use Symfony\Component\Yaml\Parser;
15 use Symfony\Component\Filesystem\Filesystem;
16 use Symfony\Component\Yaml\Exception\ParseException;
17
18 /**
19  * Class TranslatorManager
20  *
21  * @package Drupal\Console\Core\Utils
22  */
23 class TranslatorManager implements TranslatorManagerInterface
24 {
25     /**
26      * @var string
27      */
28     protected $language;
29
30     /**
31      * @var Translator
32      */
33     protected $translator;
34
35     /**
36      * @var Parser
37      */
38     protected $parser;
39
40     /**
41      * @var Filesystem
42      */
43     protected $filesystem;
44
45     /**
46      * @var string
47      */
48     protected $coreLanguageRoot;
49
50     /**
51      * Translator constructor.
52      */
53     public function __construct()
54     {
55         $this->parser = new Parser();
56         $this->filesystem = new Filesystem();
57     }
58
59     /**
60      * @param $resource
61      * @param string   $name
62      */
63     private function addResource($resource, $name = 'yaml')
64     {
65         $this->translator->addResource(
66             $name,
67             $resource,
68             $this->language
69         );
70     }
71
72     /**
73      * @param $loader
74      * @param string $name
75      */
76     private function addLoader($loader, $name = 'yaml')
77     {
78         $this->translator->addLoader(
79             $name,
80             $loader
81         );
82     }
83
84     /**
85      * @param $language
86      * @param $directoryRoot
87      *
88      * @return array
89      */
90     private function buildCoreLanguageDirectory(
91         $language,
92         $directoryRoot
93     ) {
94         $coreLanguageDirectory =
95             $directoryRoot .
96             sprintf(
97                 DRUPAL_CONSOLE_LANGUAGE,
98                 $language
99             );
100
101         if (!is_dir($coreLanguageDirectory)) {
102             return $this->buildCoreLanguageDirectory('en', $directoryRoot);
103         }
104
105         if (!$this->coreLanguageRoot) {
106             $this->coreLanguageRoot = $directoryRoot;
107         }
108
109         return [$language, $coreLanguageDirectory];
110     }
111
112     /**
113      * {@inheritdoc}
114      */
115     public function loadCoreLanguage($language, $directoryRoot)
116     {
117         $coreLanguageDirectory = $this->buildCoreLanguageDirectory(
118             $language,
119             $directoryRoot
120         );
121
122         $this->loadResource(
123             $coreLanguageDirectory[0],
124             $coreLanguageDirectory[1]
125         );
126
127         return $this;
128     }
129
130     /**
131      * {@inheritdoc}
132      */
133     public function changeCoreLanguage($language)
134     {
135         return $this->loadCoreLanguage($language, $this->coreLanguageRoot);
136     }
137
138     /**
139      * {@inheritdoc}
140      */
141     public function loadResource($language, $directoryRoot)
142     {
143         if (!is_dir($directoryRoot)) {
144             return;
145         }
146
147         $this->language = $language;
148         $this->translator = new Translator($this->language);
149         $this->addLoader(new ArrayLoader(), 'array');
150         $this->addLoader(new YamlFileLoader(), 'yaml');
151
152         /* @TODO fallback to en */
153         $finder = new Finder();
154         $finder->files()
155             ->name('*.yml')
156             ->in($directoryRoot);
157
158         foreach ($finder as $file) {
159             $resource = $directoryRoot.'/'.$file->getBasename();
160             $filename = $file->getBasename('.yml');
161
162             // Handle application file different than commands
163             if ($filename == 'application') {
164                 try {
165                     $this->loadTranslationByFile($resource, 'application');
166                 } catch (ParseException $e) {
167                     echo 'application.yml'.' '.$e->getMessage();
168                 }
169
170                 continue;
171             }
172             $key = 'commands.'.$filename;
173             try {
174                 $this->loadTranslationByFile($resource, $key);
175             } catch (ParseException $e) {
176                 echo $key.'.yml '.$e->getMessage();
177             }
178         }
179
180         return;
181     }
182
183     /**
184      * Load yml translation where filename is part of translation key.
185      *
186      * @param $resource
187      * @param $resourceKey
188      */
189     protected function loadTranslationByFile($resource, $resourceKey = null)
190     {
191         $resourceParsed = $this->parser->parse(file_get_contents($resource));
192
193         if ($resourceKey) {
194             $parents = explode('.', $resourceKey);
195             $resourceArray = [];
196             $this->setResourceArray($parents, $resourceArray, $resourceParsed);
197             $resourceParsed = $resourceArray;
198         }
199
200         $this->addResource($resourceParsed, 'array');
201     }
202
203     /**
204      * @param $parents
205      * @param $parentsArray
206      * @param $resource
207      *
208      * @return mixed
209      */
210     private function setResourceArray($parents, &$parentsArray, $resource)
211     {
212         $ref = &$parentsArray;
213         foreach ($parents as $parent) {
214             $ref[$parent] = [];
215             $previous = &$ref;
216             $ref = &$ref[$parent];
217         }
218
219         $previous[$parent] = $resource;
220
221         return $parentsArray;
222     }
223
224     /**
225      * {@inheritdoc}
226      */
227     public function getTranslator()
228     {
229         return $this->translator;
230     }
231
232     /**
233      * {@inheritdoc}
234      */
235     public function getLanguage()
236     {
237         return $this->language;
238     }
239
240     /**
241      * {@inheritdoc}
242      */
243     public function trans($key)
244     {
245         return $this->translator->trans($key);
246     }
247 }