|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Printful\GettextCms; |
|
4
|
|
|
|
|
5
|
|
|
use Gettext\Translations; |
|
6
|
|
|
use Printful\GettextCms\Exceptions\InvalidTranslationException; |
|
7
|
|
|
use Printful\GettextCms\Exceptions\UnsupportedDomainException; |
|
8
|
|
|
use Printful\GettextCms\Exceptions\UnsupportedLocaleException; |
|
9
|
|
|
use Printful\GettextCms\Interfaces\MessageConfigInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class used for importing translated messages |
|
13
|
|
|
*/ |
|
14
|
|
|
class TranslatedMessageImporter |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var MessageStorage */ |
|
17
|
|
|
private $storage; |
|
18
|
|
|
|
|
19
|
|
|
/** @var MessageConfigInterface */ |
|
20
|
|
|
private $config; |
|
21
|
|
|
|
|
22
|
4 |
|
public function __construct(MessageConfigInterface $config, MessageStorage $storage) |
|
23
|
|
|
{ |
|
24
|
4 |
|
$this->storage = $storage; |
|
25
|
4 |
|
$this->config = $config; |
|
26
|
4 |
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Import translations from translation object |
|
30
|
|
|
* |
|
31
|
|
|
* @param Translations $translations |
|
32
|
|
|
* @throws UnsupportedLocaleException |
|
33
|
|
|
* @throws UnsupportedDomainException |
|
34
|
|
|
* @throws InvalidTranslationException |
|
35
|
|
|
*/ |
|
36
|
4 |
|
public function importFromTranslations(Translations $translations) |
|
37
|
|
|
{ |
|
38
|
4 |
|
$locale = $translations->getLanguage(); |
|
39
|
4 |
|
$domain = $translations->getDomain(); |
|
40
|
|
|
|
|
41
|
4 |
|
if (!in_array($locale, $this->config->getLocales())) { |
|
42
|
1 |
|
throw new UnsupportedLocaleException('Locale ' . $locale . ' was not found in config'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
3 |
|
if (!in_array($domain, $this->config->getOtherDomains()) && $domain !== $this->config->getDefaultDomain()) { |
|
46
|
1 |
|
throw new UnsupportedDomainException('Domain ' . $domain . ' was not found in config'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
2 |
|
$this->storage->saveTranslated($translations); |
|
50
|
2 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Import Translations from PO string |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $po |
|
56
|
|
|
* @throws InvalidTranslationException |
|
57
|
|
|
* @throws UnsupportedDomainException |
|
58
|
|
|
* @throws UnsupportedLocaleException |
|
59
|
|
|
*/ |
|
60
|
1 |
|
public function importFromPo(string $po) |
|
61
|
|
|
{ |
|
62
|
1 |
|
$this->importFromTranslations(Translations::fromPoString($po)); |
|
63
|
|
|
} |
|
64
|
|
|
} |