|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Printful\GettextCms; |
|
4
|
|
|
|
|
5
|
|
|
use Gettext\Translation; |
|
6
|
|
|
use Printful\GettextCms\Exceptions\MissingMessagesException; |
|
7
|
|
|
use Printful\GettextCms\Interfaces\MessageConfigInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class allows adding dynamically generated translations which are not present in files. |
|
11
|
|
|
* For example, add messages from database fields which you still want to use gettext with. |
|
12
|
|
|
*/ |
|
13
|
|
|
class DynamicMessageImporter |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var MessageConfigInterface */ |
|
16
|
|
|
private $config; |
|
17
|
|
|
|
|
18
|
|
|
/** @var MessageStorage */ |
|
19
|
|
|
private $storage; |
|
20
|
|
|
|
|
21
|
|
|
/** @var array [context => [message, ..], ..] */ |
|
22
|
|
|
private $messages = []; |
|
23
|
|
|
|
|
24
|
8 |
|
public function __construct(MessageConfigInterface $config, MessageStorage $storage) |
|
25
|
|
|
{ |
|
26
|
8 |
|
$this->config = $config; |
|
27
|
8 |
|
$this->storage = $storage; |
|
28
|
8 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Add message to the message queue to be saved. |
|
32
|
|
|
* Save has to be called to save all the translations to the repository |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $original |
|
35
|
|
|
* @param string $context |
|
36
|
|
|
* @return DynamicMessageImporter |
|
37
|
|
|
* |
|
38
|
|
|
* @see \Printful\GettextCms\Structures\DynamicMessageImporter::saveAndDisabledPrevious |
|
39
|
|
|
*/ |
|
40
|
6 |
|
public function add(string $original, string $context = ''): DynamicMessageImporter |
|
41
|
|
|
{ |
|
42
|
6 |
|
$this->messages += [$context => []]; |
|
43
|
6 |
|
$this->messages[$context][$original] = $original; |
|
44
|
|
|
|
|
45
|
6 |
|
return $this; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Save given messages to domain |
|
50
|
|
|
* All old messages will be disabled |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $domain |
|
53
|
|
|
* @throws MissingMessagesException |
|
54
|
|
|
*/ |
|
55
|
7 |
|
public function saveAndDisabledPrevious(string $domain) |
|
56
|
|
|
{ |
|
57
|
7 |
|
if (empty($this->messages)) { |
|
58
|
1 |
|
throw new MissingMessagesException('Tried to save but no messages were given'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
6 |
|
$locales = $this->config->getLocales(); |
|
62
|
6 |
|
$defaultLocale = $this->config->getDefaultLocale(); |
|
63
|
|
|
|
|
64
|
6 |
|
foreach ($locales as $locale) { |
|
65
|
6 |
|
if ($locale === $defaultLocale) { |
|
66
|
|
|
// We do not save the default locale, because default locale is the gettext fallback |
|
67
|
|
|
// if no other locale is set |
|
68
|
1 |
|
continue; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
6 |
|
$this->storage->setAllAsNotDynamic($locale, $domain); |
|
72
|
|
|
|
|
73
|
6 |
|
foreach ($this->messages as $context => $messages) { |
|
74
|
6 |
|
foreach ($messages as $message) { |
|
75
|
6 |
|
$this->storage->createOrUpdateSingleDynamic($locale, $domain, new Translation($context, $message)); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
6 |
|
$this->storage->disableUnused(); |
|
81
|
|
|
|
|
82
|
|
|
// Clear the queue of messages to be saved so on repeated calls we do not do anything |
|
83
|
6 |
|
$this->messages = []; |
|
84
|
|
|
} |
|
85
|
|
|
} |