From 96e96ec80be7041e72be28b6d098f55673cbff2a Mon Sep 17 00:00:00 2001 From: KoKsPfLaNzE Date: Fri, 29 Jan 2016 00:33:45 +0100 Subject: [PATCH] changed translation handling in the different renderer, so the translator is not required --- src/ZfcDatagrid/Renderer/AbstractRenderer.php | 12 +++++++++++ src/ZfcDatagrid/Renderer/Csv/Renderer.php | 3 ++- src/ZfcDatagrid/Renderer/JqGrid/Renderer.php | 2 +- .../Renderer/PHPExcel/Renderer.php | 16 +++++++-------- src/ZfcDatagrid/Renderer/TCPDF/Renderer.php | 3 ++- .../Renderer/ZendTable/Renderer.php | 20 +++++++++++-------- .../Renderer/AbstractRendererTest.php | 16 +++++++++++++++ 7 files changed, 53 insertions(+), 19 deletions(-) diff --git a/src/ZfcDatagrid/Renderer/AbstractRenderer.php b/src/ZfcDatagrid/Renderer/AbstractRenderer.php index 412bd56f..f34bb92c 100644 --- a/src/ZfcDatagrid/Renderer/AbstractRenderer.php +++ b/src/ZfcDatagrid/Renderer/AbstractRenderer.php @@ -436,6 +436,15 @@ public function getTranslator() return $this->translator; } + /** + * @param $string + * @return string + */ + public function translate($string) + { + return $this->getTranslator() ? $this->getTranslator()->translate($string) : $string; + } + /** * Set the title * @@ -455,6 +464,9 @@ public function getTitle() return $this->title; } + /** + * @param Cache\Storage\StorageInterface $cache + */ public function setCache(Cache\Storage\StorageInterface $cache) { $this->cache = $cache; diff --git a/src/ZfcDatagrid/Renderer/Csv/Renderer.php b/src/ZfcDatagrid/Renderer/Csv/Renderer.php index 195cff00..878cc88b 100644 --- a/src/ZfcDatagrid/Renderer/Csv/Renderer.php +++ b/src/ZfcDatagrid/Renderer/Csv/Renderer.php @@ -3,6 +3,7 @@ * Render datagrid as CSV * */ + namespace ZfcDatagrid\Renderer\Csv; use Zend\Http\Headers; @@ -59,7 +60,7 @@ public function execute() if (isset($optionsRenderer['header']) && true === $optionsRenderer['header']) { $header = []; foreach ($this->getColumnsToExport() as $col) { - $header[] = $this->getTranslator()->translate($col->getLabel()); + $header[] = $this->translate($col->getLabel()); } fputcsv($fp, $header, $delimiter, $enclosure); } diff --git a/src/ZfcDatagrid/Renderer/JqGrid/Renderer.php b/src/ZfcDatagrid/Renderer/JqGrid/Renderer.php index aa43e847..a93ec2c0 100644 --- a/src/ZfcDatagrid/Renderer/JqGrid/Renderer.php +++ b/src/ZfcDatagrid/Renderer/JqGrid/Renderer.php @@ -212,7 +212,7 @@ public function getData() foreach ($column->getActions() as $action) { /* @var $action \ZfcDatagrid\Column\Action\AbstractAction */ if ($action->isDisplayed($row) === true) { - $action->setTitle($this->getTranslator()->translate($action->getTitle())); + $action->setTitle($this->translate($action->getTitle())); $actions[] = $action->toHtml($row); } } diff --git a/src/ZfcDatagrid/Renderer/PHPExcel/Renderer.php b/src/ZfcDatagrid/Renderer/PHPExcel/Renderer.php index 03216f0f..58b591f4 100644 --- a/src/ZfcDatagrid/Renderer/PHPExcel/Renderer.php +++ b/src/ZfcDatagrid/Renderer/PHPExcel/Renderer.php @@ -2,6 +2,7 @@ /** * Output as an excel file */ + namespace ZfcDatagrid\Renderer\PHPExcel; use PHPExcel; @@ -45,8 +46,7 @@ public function execute() // Sheet 1 $phpExcel->setActiveSheetIndex(0); $sheet = $phpExcel->getActiveSheet(); - $sheet->setTitle($this->getTranslator() - ->translate($optionsRenderer['sheetName'])); + $sheet->setTitle($this->translate($optionsRenderer['sheetName'])); if (true === $optionsRenderer['displayTitle']) { $sheet->setCellValue('A' . $optionsRenderer['rowTitle'], $this->getTitle()); @@ -72,8 +72,7 @@ public function execute() $yRow = $optionsRenderer['startRowData']; foreach ($this->getColumnsToExport() as $col) { /* @var $column \ZfcDatagrid\Column\AbstractColumn */ - $label = $this->getTranslator()->translate($col->getLabel()); - $sheet->setCellValueByColumnAndRow($xColumn, $yRow, $label); + $sheet->setCellValueByColumnAndRow($xColumn, $yRow, $this->translate($col->getLabel())); $sheet->getColumnDimension(PHPExcel_Cell::stringFromColumnIndex($xColumn))->setWidth($col->getWidth()); @@ -341,13 +340,14 @@ protected function setPrinting(PHPExcel $phpExcel) $phpExcel->setActiveSheetIndex(0); } + /** + * @param \PHPExcel_Worksheet $sheet + */ protected function setHeaderFooter(\PHPExcel_Worksheet $sheet) { - $translator = $this->getTranslator(); - - $textRight = $translator->translate('Page') . ' &P / &N'; + $textRight = $this->translate('Page') . ' &P / &N'; - $sheet->getHeaderFooter()->setOddHeader('&L&16&G ' . $translator->translate($this->getTitle())); + $sheet->getHeaderFooter()->setOddHeader('&L&16&G ' . $this->translate($this->getTitle())); $sheet->getHeaderFooter()->setOddFooter('&R' . $textRight); } } diff --git a/src/ZfcDatagrid/Renderer/TCPDF/Renderer.php b/src/ZfcDatagrid/Renderer/TCPDF/Renderer.php index 857cbca3..2fba8eeb 100644 --- a/src/ZfcDatagrid/Renderer/TCPDF/Renderer.php +++ b/src/ZfcDatagrid/Renderer/TCPDF/Renderer.php @@ -2,6 +2,7 @@ /** * Output as a PDF file */ + namespace ZfcDatagrid\Renderer\TCPDF; use TCPDF; @@ -272,7 +273,7 @@ protected function printTableHeader() $this->columnsPositionX[$col->getUniqueId()] = $x; - $label = $this->getTranslator()->translate($col->getLabel()); + $label = $this->translate($col->getLabel()); // Do not wrap header labels, it will look very ugly, that's why max height is set to 7! $pdf->MultiCell($col->getWidth(), 7, $label, 1, $this->getTextAlignment(), true, 2, $x, $y, true, 0, false, true, 7); diff --git a/src/ZfcDatagrid/Renderer/ZendTable/Renderer.php b/src/ZfcDatagrid/Renderer/ZendTable/Renderer.php index 4e2d7a1c..1b626227 100644 --- a/src/ZfcDatagrid/Renderer/ZendTable/Renderer.php +++ b/src/ZfcDatagrid/Renderer/ZendTable/Renderer.php @@ -220,7 +220,6 @@ public function execute() private function getTable() { $paginator = $this->getPaginator(); - $translator = $this->getTranslator(); $options = [ 'columnWidths' => $this->getColumnWidths(), @@ -246,10 +245,8 @@ private function getTable() */ $tableRow = new Table\Row(); foreach ($this->getColumnsToDisplay() as $column) { - $label = $column->getLabel(); - if ($this->getTranslator() !== null) { - $label = $this->getTranslator()->translate($label); - } + $label = $this->translate($column->getLabel()); + if (function_exists('mb_strtoupper')) { $label = mb_strtoupper($label); } else { @@ -299,12 +296,19 @@ private function getTable() */ $tableRow = new Table\Row(); - $footer = $translator->translate('Page') . ' '; - $footer .= $paginator->getCurrentPageNumber() . ' ' . $translator->translate('of') . ' ' . $paginator->count(); + $footer = $this->translate('Page') . ' '; + $footer .= sprintf('%s %s %s', $paginator->getCurrentPageNumber(), $this->translate('of'), $paginator->count()); $footer .= ' / '; - $footer .= $translator->translate('Showing') . ' ' . $paginator->getCurrentItemCount() . ' ' . $translator->translate('of') . ' ' . $paginator->getTotalItemCount() . ' ' . $translator->translate('items'); + $footer .= sprintf( + '%s %s %s %s %s', + $this->translate('Showing'), + $paginator->getCurrentItemCount(), + $this->translate('of'), + $paginator->getTotalItemCount(), + $this->translate('items') + ); $tableColumn = new Table\Column($footer); $tableColumn->setColSpan(count($options['columnWidths'])); diff --git a/tests/ZfcDatagridTest/Renderer/AbstractRendererTest.php b/tests/ZfcDatagridTest/Renderer/AbstractRendererTest.php index 9127b50a..1611029a 100644 --- a/tests/ZfcDatagridTest/Renderer/AbstractRendererTest.php +++ b/tests/ZfcDatagridTest/Renderer/AbstractRendererTest.php @@ -266,6 +266,22 @@ public function testTranslator() $this->assertSame($translator, $renderer->getTranslator()); } + public function testTranslate() + { + /* @var $renderer \ZfcDatagrid\Renderer\AbstractRenderer */ + $renderer = $this->getMockForAbstractClass('ZfcDatagrid\Renderer\AbstractRenderer'); + $this->assertEquals('foobar', $renderer->translate('foobar')); + + $translator = $this->getMock('Zend\I18n\Translator\Translator', ['translate'], [], '', false); + $translator->expects($this->any()) + ->method('translate') + ->willReturn('barfoo'); + + $renderer->setTranslator($translator); + + $this->assertEquals('barfoo', $renderer->translate('foobar')); + } + public function testTitle() { /* @var $renderer \ZfcDatagrid\Renderer\AbstractRenderer */