symfonyexport-to-excelexport-to-csvsonata-adminexport-to-xml

Can header labels be translated in sonata admin bundle export feature?


SonataAdminBundle can export list data into several formats (CSV, Excel, JSON and XML), but the resulting file label headers exactly as the column names.

How can I replace those names for more describing labels? Usually by translating it as it's done in lists in screen.


Solution

  • I've discovered the way. It's quite an obscure feature, but export headers (labels) can be overwritten by using keys in getExportFields method. And also there you can translate it.

    public function getExportFields()
    {
        return array(
            'field label 1' => 'field1',
            $this->trans('field label 2') => 'field2',
            'field label 3' => 'field3',
            [...]
        );
    }