0 follower

Final Class Yiisoft\Translator\Message\Db\MessageSource

InheritanceYiisoft\Translator\Message\Db\MessageSource
ImplementsYiisoft\Translator\MessageReaderInterface, Yiisoft\Translator\MessageWriterInterface

Allows using a database as a message source for yiisoft/translator.

Use the Yiisoft\Translator\Message\Db\DbSchemaManager::ensureTables() to initialize database schema.

Method Details

Hide inherited methods

__construct() public method

public __construct( \Yiisoft\Db\Connection\ConnectionInterface $db, \Yiisoft\Cache\CacheInterface|null $cache null, string $sourceMessageTable '{{%yii_source_message}}', string $messageTable '{{%yii_message}}', integer $cachingDuration 3600 ): mixed
$db \Yiisoft\Db\Connection\ConnectionInterface
$cache \Yiisoft\Cache\CacheInterface|null
$sourceMessageTable string
$messageTable string
$cachingDuration integer

                public function __construct(
    private ConnectionInterface $db,
    private ?CacheInterface $cache = null,
    private string $sourceMessageTable = '{{%yii_source_message}}',
    private string $messageTable = '{{%yii_message}}',
    private int $cachingDuration = 3600,
) {}

            
getMessage() public method

public getMessage( string $id, string $category, string $locale, array $parameters = [] ): string|null
$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
{
    if (!isset($this->messages[$category][$locale])) {
        $this->messages[$category][$locale] = $this->read($category, $locale);
    }
    return $this->messages[$category][$locale][$id]['message'] ?? null;
}

            
getMessages() public method

public getMessages( string $category, string $locale ): array
$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] ?? [];
}

            
write() public method

public write( string $category, string $locale, array $messages ): void
$category string
$locale string
$messages array
throws \Yiisoft\Db\Exception\Exception
throws 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 = $this->db
        ->select(['id', 'message_id'])
        ->from($this->sourceMessageTable)
        ->where(['category' => $category])
        ->indexBy('message_id')
        ->column();
    $translatedMessages = $this->readFromDb($category, $locale);
    $command = $this->db->createCommand();
    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'];
            }
            $result = $this->db
                ->createCommand()
                ->insertReturningPks(
                    $this->sourceMessageTable,
                    [
                        'category' => $category,
                        'message_id' => $messageId,
                        'comment' => $comment,
                    ],
                );
            $sourceMessages[$messageId] = $result['id'];
        }
        if (!isset($translatedMessages[$messageId])
            || $translatedMessages[$messageId]['message'] !== $messageData['message']
        ) {
            $command->upsert(
                $this->messageTable,
                [
                    'id' => $sourceMessages[$messageId],
                    'locale' => $locale,
                    'translation' => $messageData['message'],
                ],
            )->execute();
        }
    }
}