dongyi7901 2018-02-28 13:20
浏览 138
已采纳

yii2将消息提取到db

I'm using trntv/Yii2-starter-kit. How can I extract messages to DB? My config:

'*'=> [
         'class' => 'yii\i18n\DbMessageSource',
         'sourceMessageTable'=>'{{%i18n_source_message}}',
         'messageTable'=>'{{%i18n_message}}',
         'enableCaching' => YII_ENV_DEV,
         'cachingDuration' => 3600,
         'on missingTranslation' => ['\backend\modules\i18n\Module', 'missingTranslation']
      ]

My I18N file:

'sourcePath'=>Yii::getAlias('@base'),
'languages' => ['uz','ru'],
'translator' => 'Yii::t',
'sort' => false,
'removeUnused' => true,
'only' => [
    '*.php',
],
'ignoreCategories' => ['yii'],

I tryed:

php yii message @common/config/messages/_base.php

And php yii message But always it writes all messages to files: vendor/yiisoft/yii2/messages. How can I export messages to DB? Has anyone help?

  • 写回答

1条回答 默认 最新

  • douni1396 2018-02-28 15:53
    关注

    You need to use the following as per CONSOLE-DOCS there is a ExtendedMessageControler class. This controller extends default MessageController to provide some useful actions:

    • To migrate messages between different message sources run the common like below

      php console/yii message/migrate @common/config/messages/php.php @common/config/messages/db.php

    Which means you should have a file inside the @common/confiog/messages/ folder with the name db.php that will be used to create the message and source_message tables the contents of the file should be

    <?php
    return \yii\helpers\ArrayHelper::merge(
        require(__DIR__ . '/_base.php'),
        [
            // 'db' output format is for saving messages to database.
            'format' => 'db',
            // Connection component to use. Optional.
            'db' => 'db',
            // Custom source message table. Optional.
            'sourceMessageTable' => '{{%i18n_source_message}}',
            // Custom name for translation message table. Optional.
            'messageTable' => '{{%i18n_message}}',
        ]
    );
    

    and the messages source directory will be determined by the php.php file inside the @common/config/messages directory that contains the following

    <?php
    return \yii\helpers\ArrayHelper::merge(
        require(__DIR__ . '/_base.php'),
        [
            // 'php' output format is for saving messages to php files.
            'format' => 'php',
            // Root directory containing message translations.
            'messagePath' => Yii::getAlias('@common/messages'),
            // boolean, whether the message file should be overwritten with the merged messages
            'overwrite' => true,
        ]
    );
    

    You just need to run the migration command and the tables will be created and the messages will be exported to the respective tables.

    for more details see the SOURCE for the actionMigrate().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?