|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Nexendrie\Translation\Loaders; |
|
5
|
|
|
|
|
6
|
|
|
use Nette\Utils\Finder; |
|
7
|
|
|
use Nexendrie\Translation\FolderNotSetException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* MessagesCatalogue |
|
11
|
|
|
* Loads texts from compiled (PHP) messages catalogue |
|
12
|
|
|
* |
|
13
|
|
|
* @author Jakub Konečný |
|
14
|
|
|
*/ |
|
15
|
|
|
final class MessagesCatalogue extends FileLoader { |
|
16
|
|
|
protected string $extension = "php"; |
|
17
|
|
|
|
|
18
|
|
|
protected function parseFile(string $filename): array { |
|
19
|
1 |
|
return require $filename; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
protected function loadDomain(string $name): array { |
|
23
|
|
|
return []; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
protected function loadTexts(): void { |
|
27
|
1 |
|
if(count($this->folders) === 0) { |
|
28
|
1 |
|
throw new FolderNotSetException("Folder for translations was not set."); |
|
29
|
|
|
} |
|
30
|
1 |
|
$this->resources = $texts = []; |
|
31
|
1 |
|
$filename = str_replace(static::LANGUAGE_MASK, $this->lang, $this->getLanguageFilenameMask()); |
|
32
|
1 |
|
$files = Finder::findFiles($filename) |
|
33
|
1 |
|
->from($this->folders); |
|
34
|
|
|
/** @var \SplFileInfo $file */ |
|
35
|
1 |
|
foreach($files as $file) { |
|
36
|
1 |
|
$texts = array_merge($texts, $this->parseFile($file->getPathname())); |
|
37
|
1 |
|
if(isset($texts["__resources"])) { |
|
38
|
1 |
|
$this->resources = array_merge($this->resources, $texts["__resources"]); |
|
39
|
1 |
|
unset($texts["__resources"]); |
|
40
|
|
|
} else { |
|
41
|
|
|
/** @var string[] $domains */ |
|
42
|
1 |
|
$domains = array_keys($texts); |
|
43
|
1 |
|
foreach($domains as $domain) { |
|
44
|
1 |
|
$this->addResource($file->getPathname(), $domain); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
1 |
|
$this->texts = $texts; |
|
49
|
1 |
|
$this->loadedLang = $this->lang; |
|
50
|
1 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
protected function getLanguageFilenameMask(): string { |
|
53
|
1 |
|
return "catalogue." . static::LANGUAGE_MASK . "." . $this->extension; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
?> |