Final Class Yiisoft\Translator\Message\Db\MessageSource
| Inheritance | Yiisoft\Translator\Message\Db\MessageSource |
|---|---|
| Implements | Yiisoft\Translator\MessageReaderInterface, Yiisoft\Translator\MessageWriterInterface |
Allows using a database as a message source for yiisoft/translator.
Use the {@see \Yiisoft\Translator\Message\Db\DbSchemaManager::ensureTables()} to initialize database schema.
Public Methods
Method Details
| public mixed __construct ( \Yiisoft\Db\Connection\ConnectionInterface $db, \Yiisoft\Cache\CacheInterface|null $cache = null, string $sourceMessageTable = '{{%yii_source_message}}', string $messageTable = '{{%yii_message}}', integer $cachingDuration = 3600 ) | ||
| $db | \Yiisoft\Db\Connection\ConnectionInterface | |
| $cache | \Yiisoft\Cache\CacheInterface|null | |
| $sourceMessageTable | string | |
| $messageTable | string | |
| $cachingDuration | integer | |
public function __construct(
private ConnectionInterface $db,
private CacheInterface|null $cache = null,
private string $sourceMessageTable = '{{%yii_source_message}}',
private string $messageTable = '{{%yii_message}}',
private int $cachingDuration = 3600
) {
}
| public string|null getMessage ( string $id, string $category, string $locale, array $parameters = [] ) | ||
| $id | string | |
| $category | string | |
| $locale | string | |
| $parameters | array | |
| throws | \Yiisoft\Db\Exception\Exception | |
|---|---|---|
| throws | JsonException | |
| throws | \Yiisoft\Db\Exception\InvalidConfigException | |
| throws | Throwable | |
public function getMessage(string $id, string $category, string $locale, array $parameters = []): string|null
{
if (!isset($this->messages[$category][$locale])) {
$this->messages[$category][$locale] = $this->read($category, $locale);
}
return $this->messages[$category][$locale][$id]['message'] ?? null;
}
| public array getMessages ( string $category, string $locale ) | ||
| $category | string | |
| $locale | string | |
| throws | \Yiisoft\Db\Exception\Exception | |
|---|---|---|
| throws | JsonException | |
| throws | \Yiisoft\Db\Exception\InvalidConfigException | |
| throws | Throwable | |
public function getMessages(string $category, string $locale): array
{
if (!isset($this->messages[$category][$locale])) {
$this->messages[$category][$locale] = $this->read($category, $locale);
}
return $this->messages[$category][$locale] ?? [];
}
| public void write ( string $category, string $locale, array $messages ) | ||
| $category | string | |
| $locale | string | |
| $messages | array | |
| throws | \Yiisoft\Db\Exception\Exception | |
|---|---|---|
| throws | \Yiisoft\Db\Exception\InvalidArgumentException | |
| throws | \Yiisoft\Db\Exception\InvalidCallException | |
| throws | \Yiisoft\Db\Exception\InvalidConfigException | |
| throws | Throwable | |
public function write(string $category, string $locale, array $messages): void
{
/** @psalm-var array<int, array<string, string|null>> $sourceMessages */
$sourceMessages = (new Query($this->db))
->select(['id', 'message_id'])
->from($this->sourceMessageTable)
->where(['category' => $category])
->all();
$sourceMessages = ArrayHelper::map($sourceMessages, 'message_id', 'id');
$translatedMessages = $this->readFromDb($category, $locale);
foreach ($messages as $messageId => $messageData) {
if (!array_key_exists('message', $messageData)) {
throw new InvalidArgumentException("Message is not valid for ID \"$messageId\". \"message\" key is missing.");
}
/** @psalm-suppress DocblockTypeContradiction */
if (!is_string($messageData['message'])) {
throw new InvalidArgumentException("Message is not a string for ID \"$messageId\".");
}
if (!isset($sourceMessages[$messageId])) {
$comment = '';
if (array_key_exists('comment', $messageData)) {
/** @psalm-suppress DocblockTypeContradiction */
if (!is_string($messageData['comment'])) {
throw new InvalidArgumentException("Message comment is not a string for ID \"$messageId\".");
}
$comment = $messageData['comment'];
}
/** @psalm-var array<string,string>|false $result */
$result = $this->db
->createCommand()
->insertWithReturningPks(
$this->sourceMessageTable,
[
'category' => $category,
'message_id' => $messageId,
'comment' => $comment,
],
);
if ($result === false) {
throw new RuntimeException("Failed to write source message with \"$messageId\" ID.");
}
$sourceMessages[$messageId] = $result['id'];
}
$needUpdate = false;
if (isset($translatedMessages[$messageId]) && $translatedMessages[$messageId]['message'] !== $messageData['message']) {
$this->db
->createCommand()
->delete($this->messageTable, ['id' => $sourceMessages[$messageId]])
->execute();
$needUpdate = true;
}
if ($needUpdate || !isset($translatedMessages[$messageId])) {
$result = $this->db
->createCommand()
->insertWithReturningPks(
$this->messageTable,
[
'id' => $sourceMessages[$messageId],
'locale' => $locale,
'translation' => $messageData['message'],
]
);
if ($result === false) {
throw new RuntimeException("Failed to write message with \"$messageId\" ID.");
}
}
}
}
Signup or Login in order to comment.