|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Printful\GettextCms; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use Printful\GettextCms\Exceptions\GettextCmsException; |
|
8
|
|
|
use Printful\GettextCms\Interfaces\MessageConfigInterface; |
|
9
|
|
|
use Printful\GettextCms\Structures\ScanItem; |
|
10
|
|
|
|
|
11
|
|
|
class MessageImporter |
|
12
|
|
|
{ |
|
13
|
|
|
/** @var MessageExtractor */ |
|
14
|
|
|
private $extractor; |
|
15
|
|
|
|
|
16
|
|
|
/** @var MessageStorage */ |
|
17
|
|
|
private $storage; |
|
18
|
|
|
|
|
19
|
|
|
/** @var MessageConfigInterface */ |
|
20
|
|
|
private $config; |
|
21
|
|
|
|
|
22
|
1 |
|
public function __construct(MessageConfigInterface $config, MessageStorage $storage, MessageExtractor $extractor) |
|
23
|
|
|
{ |
|
24
|
1 |
|
$this->extractor = $extractor; |
|
25
|
1 |
|
$this->storage = $storage; |
|
26
|
1 |
|
$this->config = $config; |
|
27
|
1 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Extract translations and save them all to repository |
|
31
|
|
|
* |
|
32
|
|
|
* @param ScanItem[] $scanItems |
|
33
|
|
|
* @throws GettextCmsException |
|
34
|
|
|
*/ |
|
35
|
1 |
|
public function extractAndSave(array $scanItems) |
|
36
|
|
|
{ |
|
37
|
1 |
|
$defaultLocale = $this->config->getDefaultLocale(); |
|
38
|
|
|
|
|
39
|
1 |
|
$allDomainTranslations = $this->extractor->extract($scanItems); |
|
40
|
|
|
|
|
41
|
1 |
|
foreach ($this->config->getLocales() as $locale) { |
|
42
|
1 |
|
if ($locale === $defaultLocale) { |
|
43
|
|
|
// We do not save the default locale, because default locale is the gettext fallback |
|
44
|
|
|
// if no other locale is set |
|
45
|
1 |
|
continue; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
foreach ($allDomainTranslations as $translations) { |
|
49
|
|
|
// Disable all previous translations and save the new ones |
|
50
|
1 |
|
$this->storage->disableAll($locale, $translations->getDomain()); |
|
51
|
1 |
|
$translations->setLanguage($locale); |
|
52
|
1 |
|
$this->storage->createOrUpdate($translations); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
} |