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