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