如何配置Yii2 i18n在数据库而不是文件中存储消息?

问题描述:

I find Yii2 documentation seriously lacking in documentation about i18n, particularly about using database instead of files for the translation messages.

I see documentation (EDIT: sorry, this was the link I meant) mentions the DbMessageSource class and somewhere else I found that the db tables should be named source_message and message but nowhere does it tell me the schema for those tables! And the yii message/extract command doesn't seem to auto-create the tables either.

How do I go about this?

我发现Yii2文档严重缺乏关于i18n的文档,尤其是关于使用数据库而不是文件来翻译消息。 / p>

我看到文档(编辑:抱歉,这个是我的意思)提到DbMessageSource class和其他地方我发现db表应该命名为 source_message code>和 message code>,但它没有告诉我这些表的架构! 并且 yii message / extract code>命令似乎也不会自动创建表。 p>

我该怎么做? p> DIV>

Your documentation link is about a yii2 extension, you should read instead :

If you don't want to use migration and need SQL instead, files for all databases are in migrations directory.

https://github.com/yiisoft/yii2/tree/master/framework/i18n/migrations

For example, the schema for MySQL is :

CREATE TABLE `source_message`
(
   `id`          integer NOT NULL AUTO_INCREMENT PRIMARY KEY,
   `category`    varchar(255),
   `message`     text
);

CREATE TABLE `message`
(
   `id`          integer NOT NULL,
   `language`    varchar(16) NOT NULL,
   `translation` text
);

ALTER TABLE `message` ADD CONSTRAINT `pk_message_id_language` PRIMARY KEY (`id`, `language`);
ALTER TABLE `message` ADD CONSTRAINT `fk_message_source_message` FOREIGN KEY (`id`) REFERENCES `source_message` (`id`) ON UPDATE CASCADE ON DELETE RESTRICT;

The source_message table stores the messages to be translated, and the message table stores the translated messages. The name of these two tables can be customized by setting $sourceMessageTable and $messageTable, respectively.