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'];
            },
        ],
    ];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP