duanjian4150 2018-06-01 23:12
浏览 154
已采纳

有一种更好的方法来使用yii2tech \ spreadsheet \ Spreadsheet翻译查询结果

I'm doing a manual query. You can translate the names of the columns easily, but I can not do it with the results of the columns in a slightly more efficient way. The results are printed in excel with the help of yii2tech\spreadsheet\Spreadsheet.

Preparing query

$columns = [
  [
    'attribute' => 'description',
    'label' => Yii::t('data', 'Description')
  ],
  [
    'attribute' => 'type',
    'label' => Yii::t('data', 'Type')
  ]
];

$query
  ->addSelect(['description' => 'data.description'])
  ->addSelect(['type' => 'data.type'])
  ->from('data')

$rows = $query->all();

So far I make the query. The following is my way of translating the results of the type column. Because they can be just some values.

Translating results

foreach ($rows as $key => $row) {
  $rows[$key]['type'] = Yii::t('data', $row['type']);
}

This data is exported to xls format:

Exporting results

$exporter = new Spreadsheet([
  'dataProvider' => new ArrayDataProvider([
    'allModels' => $rows,
  ]),
  'columns' => $columns,
]);
  • 写回答

1条回答 默认 最新

  • dongtiao6362 2018-06-02 11:24
    关注

    You may define translation inside of $columns declaration - it will save you manual iteration trough results array to replace type with translated string:

    $columns = [
        [
            'attribute' => 'description',
            'label' => Yii::t('data', 'Description'),
        ],
        [
            'attribute' => 'type',
            'label' => Yii::t('data', 'Type'),
            'value' => function ($data) {
                return Yii::t('data', $data['type']);
            }
        ],
    ];
    

    If sheet is big and types are often repeated, you may try to cache translated string - Yii::t() may be quite expensive:

    $columns = [
        [
            'attribute' => 'description',
            'label' => Yii::t('data', 'Description'),
        ],
        [
            'attribute' => 'type',
            'label' => Yii::t('data', 'Type'),
            'value' => function ($data) {
                static $translations = [];
                if (!isset($translations[$data['type']])) {
                    $translations[$data['type']] = Yii::t('data', $data['type']);
                }
    
                return $translations[$data['type']];
            },
        ],
    ];
    

    This will call Yii::t() only once per unique type. But if list of types is small and hardcoded, you may simplify this even more - create getTranslatedTypes() static method, which returns translated list of all types:

    public static function getTranslatedTypes() {
        return [
            'some type' => Yii::t('data', 'some type'),
            // ...
        ];
    }
    

    And use it as a source of translations:

    $translations = Type::getTranslatedTypes();
    $columns = [
        [
            'attribute' => 'description',
            'label' => Yii::t('data', 'Description'),
        ],
        [
            'attribute' => 'type',
            'label' => Yii::t('data', 'Type'),
            'value' => function ($data) use ($translations) {
                return $translations[$data['type']] ?? $data['type'];
            },
        ],
    ];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?