1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Printful\GettextCms; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Gettext\Generators\Mo; |
8
|
|
|
use Printful\GettextCms\Exceptions\InvalidPathException; |
9
|
|
|
use Printful\GettextCms\Interfaces\MessageConfigInterface; |
10
|
|
|
|
11
|
|
|
class MessageExporter |
12
|
|
|
{ |
13
|
|
|
/** @var MessageConfigInterface */ |
14
|
|
|
private $config; |
15
|
|
|
|
16
|
|
|
/** @var MessageStorage */ |
17
|
|
|
private $storage; |
18
|
|
|
|
19
|
2 |
|
public function __construct( |
20
|
|
|
MessageConfigInterface $config, |
21
|
|
|
MessageStorage $storage |
22
|
|
|
) { |
23
|
2 |
|
$this->config = $config; |
24
|
2 |
|
$this->storage = $storage; |
25
|
2 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param string $locale |
29
|
|
|
* @param string $domain |
30
|
|
|
* @return bool |
31
|
|
|
* @throws InvalidPathException |
32
|
|
|
*/ |
33
|
2 |
|
public function export(string $locale, string $domain) |
34
|
|
|
{ |
35
|
2 |
|
$translations = $this->storage->getTranslations($locale, $domain); |
36
|
|
|
|
37
|
2 |
|
$pathname = $this->getMoPathname($locale, $domain); |
38
|
|
|
|
39
|
1 |
|
return Mo::toFile($translations, $pathname); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $locale |
44
|
|
|
* @param string $domain |
45
|
|
|
* @return string |
46
|
|
|
* @throws InvalidPathException |
47
|
|
|
*/ |
48
|
2 |
|
private function getMoPathname(string $locale, string $domain) |
49
|
|
|
{ |
50
|
2 |
|
return $pathname = $this->ensureDirectory($locale) . '/' . $domain . '.mo'; |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $locale |
55
|
|
|
* @return string |
56
|
|
|
* @throws InvalidPathException |
57
|
|
|
*/ |
58
|
2 |
|
private function ensureDirectory(string $locale) |
59
|
|
|
{ |
60
|
2 |
|
$path = rtrim($this->config->getMoDirectory(), '/'); |
61
|
|
|
|
62
|
2 |
|
if (!is_dir($path)) { |
63
|
1 |
|
throw new InvalidPathException("Directory '$path' does not exist"); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
$path = $path . '/' . $locale . '/LC_MESSAGES'; |
67
|
|
|
|
68
|
1 |
|
if (!is_dir($path)) { |
69
|
1 |
|
mkdir($path, 0777, true); |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
return $path; |
73
|
|
|
} |
74
|
|
|
} |