1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Printful\GettextCms; |
4
|
|
|
|
5
|
|
|
use Gettext\Generators\Mo; |
6
|
|
|
use Printful\GettextCms\Exceptions\InvalidPathException; |
7
|
|
|
use Printful\GettextCms\Interfaces\MessageConfigInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class allows to export translations from repository to a generated .mo file which is used with gettext |
11
|
|
|
*/ |
12
|
|
|
class MessageBuilder |
13
|
|
|
{ |
14
|
|
|
/** @var MessageConfigInterface */ |
15
|
|
|
private $config; |
16
|
|
|
|
17
|
|
|
/** @var MessageStorage */ |
18
|
|
|
private $storage; |
19
|
|
|
|
20
|
|
|
/** @var MessageRevisions */ |
21
|
|
|
private $revisions; |
22
|
|
|
|
23
|
6 |
|
public function __construct( |
24
|
|
|
MessageConfigInterface $config, |
25
|
|
|
MessageStorage $storage, |
26
|
|
|
MessageRevisions $revisions |
27
|
|
|
) { |
28
|
6 |
|
$this->config = $config; |
29
|
6 |
|
$this->storage = $storage; |
30
|
6 |
|
$this->revisions = $revisions; |
31
|
6 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $locale |
35
|
|
|
* @param string $domain |
36
|
|
|
* @return bool |
37
|
|
|
* @throws InvalidPathException |
38
|
|
|
*/ |
39
|
6 |
|
public function export(string $locale, string $domain): bool |
40
|
|
|
{ |
41
|
6 |
|
$translations = $this->storage->getEnabledTranslated($locale, $domain); |
42
|
|
|
|
43
|
6 |
|
$revisionedDomain = null; |
44
|
6 |
|
if ($this->config->useRevisions()) { |
45
|
2 |
|
$revisionedDomain = $this->revisions->generateRevisionedDomain($domain, $translations); |
46
|
|
|
} |
47
|
|
|
|
48
|
6 |
|
$moPathname = $this->ensureDirectoryAndGetMoPathname($locale, $revisionedDomain ?: $domain); |
49
|
|
|
|
50
|
5 |
|
$wasSaved = Mo::toFile($translations, $moPathname); |
51
|
|
|
|
52
|
5 |
|
if ($wasSaved && $revisionedDomain) { |
53
|
2 |
|
$this->revisions->saveRevision($locale, $domain, $revisionedDomain); |
54
|
|
|
} |
55
|
|
|
|
56
|
5 |
|
return $wasSaved; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $locale |
61
|
|
|
* @param string $domain |
62
|
|
|
* @return string |
63
|
|
|
* @throws InvalidPathException |
64
|
|
|
*/ |
65
|
6 |
|
private function ensureDirectoryAndGetMoPathname(string $locale, string $domain): string |
66
|
|
|
{ |
67
|
6 |
|
$baseDir = $this->config->getMoDirectory(); |
68
|
6 |
|
$pathname = self::getMoPathname($baseDir, $locale, $domain); |
69
|
|
|
|
70
|
6 |
|
if (!is_dir($baseDir)) { |
71
|
1 |
|
throw new InvalidPathException("Directory '$baseDir' does not exist"); |
72
|
|
|
} |
73
|
|
|
|
74
|
5 |
|
$localeDir = dirname($pathname); |
75
|
|
|
|
76
|
5 |
|
if (!is_dir($localeDir)) { |
77
|
|
|
// Create the {baseDir}/{locale}/LC_MESSAGES directory |
78
|
5 |
|
mkdir($localeDir, 0777, true); |
79
|
|
|
} |
80
|
|
|
|
81
|
5 |
|
return $pathname; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get full pathname to mo file |
86
|
|
|
* |
87
|
|
|
* @param string $moDirectory Base directory path for mo files |
88
|
|
|
* @param string $locale |
89
|
|
|
* @param string $domain |
90
|
|
|
* @return string Full pathname to mo file |
91
|
|
|
*/ |
92
|
6 |
|
public static function getMoPathname($moDirectory, $locale, $domain): string |
93
|
|
|
{ |
94
|
6 |
|
return rtrim($moDirectory, '/') . '/' . $locale . '/LC_MESSAGES/' . $domain . '.mo'; |
95
|
|
|
} |
96
|
|
|
} |