【问题标题】:How to cache database translations on yii2如何在 yii2 上缓存数据库翻译
【发布时间】:2015-03-23 17:53:38
【问题描述】:

如何在 yii2 上缓存数据库翻译

我尝试了以下但没有成功

'i18n' => [
            'class' => Zelenin\yii\modules\I18n\components\I18N::className(),
            'languages' => ['en', 'ar', 'fr'],
            'sourceMessageTable' => 'source_message',
            'messageTable' => 'message',
            'cache' => 'cache'

        ],

【问题讨论】:

  • 我认为您应该明确提及使用的扩展名和链接,即使它可以从代码中识别出来。

标签: yii2 yii2-advanced-app


【解决方案1】:

问题出在 Zelenin i18n 模块中。如果你查看 Module.php 文件,你可以看到:

        $this->translations['*'] = [
            'class' => DbMessageSource::className(),
            'sourceMessageTable' => $this->sourceMessageTable,
            'messageTable' => $this->messageTable,
            'on missingTranslation' => $this->missingTranslationHandler
        ];

init() 方法中。此代码设置 DbMessageSource 选项,并且没有任何关于缓存的选项。模块也没有任何缓存选项。

如果您将此代码更改为:

        $this->translations['*'] = [
            'class' => DbMessageSource::className(),
            'sourceMessageTable' => $this->sourceMessageTable,
            'messageTable' => $this->messageTable,
            'enableCaching' => true,
            'cachingDuration' => 3600,
            'on missingTranslation' => $this->missingTranslationHandler
        ];

缓存会起作用。一些SELECT 消息将从调试列表中消失。

【讨论】:

  • 最好不要修改zelenin组件。您可以将 yii 配置(通用或前端)i18n 部分更改为:'i18n' => ['class' => Zelenin\yii\modules\I18n\components\I18N::className(), 'translations' => [' *' => [我的回答中的代码]]]
【解决方案2】:

Yii documentation for i18n db messages 说属性cache 只有在属性cacheDuration 不为零时才有意义。我建议你设置这个值,所以;

'i18n' => [
        'class' => Zelenin\yii\modules\I18n\components\I18N::className(),
        'languages' => ['en', 'ar', 'fr'],
        'sourceMessageTable' => 'source_message',
        'messageTable' => 'message',
        'cache' => 'cache',
        'cacheDuration' => 3600

    ],

【讨论】:

    猜你喜欢
    • 2016-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-23
    • 2021-10-28
    • 2011-12-05
    • 2012-11-11
    • 2013-05-13
    相关资源
    最近更新 更多