|
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
|
10 |
|
public function __construct( |
|
24
|
|
|
MessageConfigInterface $config, |
|
25
|
|
|
MessageStorage $storage, |
|
26
|
|
|
MessageRevisions $revisions |
|
27
|
|
|
) { |
|
28
|
10 |
|
$this->config = $config; |
|
29
|
10 |
|
$this->storage = $storage; |
|
30
|
10 |
|
$this->revisions = $revisions; |
|
31
|
10 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param string $locale |
|
35
|
|
|
* @param string $domain |
|
36
|
|
|
* @return bool |
|
37
|
|
|
* @throws InvalidPathException |
|
38
|
|
|
*/ |
|
39
|
8 |
|
public function export(string $locale, string $domain): bool |
|
40
|
|
|
{ |
|
41
|
8 |
|
$translations = $this->storage->getEnabledTranslated($locale, $domain); |
|
42
|
|
|
|
|
43
|
8 |
|
$revisionedDomain = null; |
|
44
|
8 |
|
if ($this->config->useRevisions()) { |
|
45
|
4 |
|
$revisionedDomain = $this->revisions->generateRevisionedDomain($domain, $translations); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
8 |
|
$moPathname = $this->ensureDirectoryAndGetMoPathname($locale, $revisionedDomain ?: $domain); |
|
49
|
|
|
|
|
50
|
7 |
|
$wasSaved = Mo::toFile($translations, $moPathname); |
|
51
|
|
|
|
|
52
|
7 |
|
if ($wasSaved && $revisionedDomain) { |
|
53
|
4 |
|
$previousRevisionedDomain = $this->revisions->getRevisionedDomain($locale, $domain); |
|
54
|
|
|
|
|
55
|
4 |
|
$wasRevisionSaved = $this->revisions->saveRevision($locale, $domain, $revisionedDomain); |
|
56
|
|
|
|
|
57
|
4 |
|
if ($wasRevisionSaved) { |
|
58
|
4 |
|
$this->removePreviousRevisionedDomain($locale, $revisionedDomain, $previousRevisionedDomain); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
7 |
|
return $wasSaved; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Remove previous domain file |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $locale |
|
69
|
|
|
* @param string $currentDomain |
|
70
|
|
|
* @param string $previousDomain |
|
71
|
|
|
* @return bool True if cleanup was successful |
|
72
|
|
|
*/ |
|
73
|
4 |
|
private function removePreviousRevisionedDomain( |
|
74
|
|
|
string $locale, |
|
75
|
|
|
string $currentDomain, |
|
76
|
|
|
string $previousDomain |
|
77
|
|
|
): bool { |
|
78
|
4 |
|
if ($currentDomain === $previousDomain) { |
|
79
|
|
|
// Same revisioned domain, no need to remove |
|
80
|
1 |
|
return true; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
4 |
|
$previousPathname = self::getMoPathname($locale, $previousDomain); |
|
|
|
|
|
|
84
|
|
|
|
|
85
|
4 |
|
if (is_file($previousPathname)) { |
|
86
|
3 |
|
return @unlink($previousPathname); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
3 |
|
return false; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param string $locale |
|
94
|
|
|
* @param string $domain |
|
95
|
|
|
* @return string |
|
96
|
|
|
* @throws InvalidPathException |
|
97
|
|
|
*/ |
|
98
|
8 |
|
private function ensureDirectoryAndGetMoPathname(string $locale, string $domain): string |
|
99
|
|
|
{ |
|
100
|
8 |
|
$baseDir = rtrim($this->config->getMoDirectory(), '/'); |
|
101
|
8 |
|
$pathname = $this->getMoPathname($locale, $domain); |
|
102
|
|
|
|
|
103
|
8 |
|
if (!is_dir($baseDir)) { |
|
104
|
1 |
|
throw new InvalidPathException("Directory '$baseDir' does not exist"); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
7 |
|
$localeDir = dirname($pathname); |
|
108
|
|
|
|
|
109
|
7 |
|
if (!is_dir($localeDir)) { |
|
110
|
|
|
// Create the {baseDir}/{locale}/LC_MESSAGES directory |
|
111
|
7 |
|
mkdir($localeDir, 0777, true); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
7 |
|
return $pathname; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Get full pathname to mo file |
|
119
|
|
|
* |
|
120
|
|
|
* @param string $locale |
|
121
|
|
|
* @param string $domain |
|
122
|
|
|
* @return string Full pathname to mo file |
|
123
|
|
|
*/ |
|
124
|
8 |
|
private function getMoPathname($locale, $domain): string |
|
125
|
|
|
{ |
|
126
|
8 |
|
return rtrim($this->config->getMoDirectory(), '/') . '/' . $locale . '/LC_MESSAGES/' . $domain . '.mo'; |
|
127
|
|
|
} |
|
128
|
|
|
} |