diff --git a/composer.json b/composer.json index ca5678d..b50ea47 100644 --- a/composer.json +++ b/composer.json @@ -24,6 +24,9 @@ } }, "autoload": { - "psr-4": { "Nextras\\Datagrid\\": "src" } + "psr-4": { "Nextras\\Datagrid\\": "src" }, + "classmap": [ + "src/exceptions.php" + ] } } diff --git a/src/Datagrid.blocks.latte b/src/Datagrid.blocks.latte index c740e3b..f38ba09 100644 --- a/src/Datagrid.blocks.latte +++ b/src/Datagrid.blocks.latte @@ -88,7 +88,7 @@ {/define} {define row-actions-edit-link} - {$control->translate(Edit)} + {_}nextras.datagrid.edit.label{/_} {/define} {define row} @@ -160,11 +160,11 @@ {define pagination}
{if $paginator->isFirst()} - « {$control->translate(First)} - « {$control->translate(Previous)} + « {_}nextras.datagrid.pagination.first{/_} + « {_}nextras.datagrid.pagination.previous{/_} {else} - « {$control->translate(First)} - « {$control->translate(Previous)} + « {_}nextras.datagrid.pagination.first{/_} + « {_}nextras.datagrid.pagination.previous{/_} {/if} @@ -172,11 +172,11 @@ {if $paginator->isLast()} - {$control->translate(Next)} » - {$control->translate(Last)} » + {_}nextras.datagrid.pagination.next{/_} » + {_}nextras.datagrid.pagination.last{/_} » {else} - {$control->translate(Next)} » - {$control->translate(Last)} » + {_}nextras.datagrid.pagination.next{/_} » + {_}nextras.datagrid.pagination.last{/_} » {/if}
{/define} diff --git a/src/Datagrid.php b/src/Datagrid.php index 12d6277..50c2c3a 100644 --- a/src/Datagrid.php +++ b/src/Datagrid.php @@ -13,7 +13,6 @@ use Nette\Bridges\ApplicationLatte\Template; use Nette\Forms\Container; use Nette\Forms\Controls\Button; -use Nette\Forms\Controls\Checkbox; use Nette\Utils\Html; use Nette\Utils\Paginator; use Nette\Localization\ITranslator; @@ -250,16 +249,18 @@ public function setTranslator(ITranslator $translator) public function getTranslator() { + if (!$this->translator) { + $this->translator = new DefaultTranslator(DefaultTranslator::LANG_EN); + } return $this->translator; } public function translate($s, $count = null) { - $translator = $this->getTranslator(); - return $translator === null || $s == null || $s instanceof Html // intentionally == + return $s == null || $s instanceof Html // intentionally == ? $s - : $translator->translate((string) $s, $count); + : $this->getTranslator()->translate((string) $s, $count); } @@ -268,10 +269,16 @@ public function translate($s, $count = null) public function render() { + if (!$this->template instanceof Template) { + throw new LogicException('Template must be instance of ' . Template::class); + } + if ($this->filterFormFactory) { $this['form']['filter']->setDefaults($this->filter); } + $this['form']->setTranslator($this->getTranslator()); + $this->template->form = $this['form']; $this->template->data = $this->getData(); $this->template->columns = $this->columns; @@ -338,11 +345,11 @@ protected function getData($key = null) { if (!$this->data) { $onlyRow = $key !== null && $this->presenter->isAjax(); - + if ($this->orderColumn !== NULL && !isset($this->columns[$this->orderColumn])) { $this->orderColumn = NULL; } - + if (!$onlyRow && $this->paginator) { $itemsCount = call_user_func( $this->paginatorItemsCountCallback, @@ -429,10 +436,10 @@ public function createComponentForm() if ($this->filterFormFactory) { $form['filter'] = call_user_func($this->filterFormFactory); if (!isset($form['filter']['filter'])) { - $form['filter']->addSubmit('filter', $this->translate('Filter')); + $form['filter']->addSubmit('filter', 'nextras.datagrid.filter.submit'); } if (!isset($form['filter']['cancel'])) { - $form['filter']->addSubmit('cancel', $this->translate('Cancel')); + $form['filter']->addSubmit('cancel', 'nextras.datagrid.filter.cancel'); } $this->prepareFilterDefaults($form['filter']); @@ -446,9 +453,9 @@ public function createComponentForm() $form['edit'] = call_user_func($this->editFormFactory, $data); if (!isset($form['edit']['save'])) - $form['edit']->addSubmit('save', 'Save'); + $form['edit']->addSubmit('save', 'nextras.datagrid.edit.save'); if (!isset($form['edit']['cancel'])) - $form['edit']->addSubmit('cancel', 'Cancel'); + $form['edit']->addSubmit('cancel', 'nextras.datagrid.edit.cancel'); if (!isset($form['edit'][$this->rowPrimaryKey])) $form['edit']->addHidden($this->rowPrimaryKey); @@ -460,19 +467,15 @@ public function createComponentForm() if ($this->globalActions) { $actions = array_map(function($row) { return $row[0]; }, $this->globalActions); $form['actions'] = new Container(); - $form['actions']->addSelect('action', 'Action', $actions) - ->setPrompt('- select action -'); + $form['actions']->addSelect('action', 'nextras.datagrid.action.label', $actions) + ->setPrompt('nextras.datagrid.action.prompt'); $rows = []; foreach ($this->getData() as $row) { $rows[$this->getter($row, $this->rowPrimaryKey)] = null; } $form['actions']->addCheckboxList('items', '', $rows); - $form['actions']->addSubmit('process', 'Do'); - } - - if ($this->translator) { - $form->setTranslator($this->translator); + $form['actions']->addSubmit('process', 'nextras.datagrid.action.process'); } $form->onSuccess[] = function() {}; // fix for Nette Framework 2.0.x @@ -554,9 +557,7 @@ public function loadState(array $params) protected function createTemplate($class = null) { $template = parent::createTemplate($class); - if ($translator = $this->getTranslator()) { - $template->setTranslator($translator); - } + $template->setTranslator($this->getTranslator()); return $template; } diff --git a/src/DefaultTranslator.php b/src/DefaultTranslator.php new file mode 100644 index 0000000..590b903 --- /dev/null +++ b/src/DefaultTranslator.php @@ -0,0 +1,80 @@ + [ + 'nextras.datagrid.filter.submit' => 'Filter', + 'nextras.datagrid.filter.cancel' => 'Cancel', + + 'nextras.datagrid.edit.label' => 'Edit', + 'nextras.datagrid.edit.save' => 'Save', + 'nextras.datagrid.edit.cancel' => 'Cancel', + + 'nextras.datagrid.action.label' => 'Action', + 'nextras.datagrid.action.prompt' => '- select action -', + 'nextras.datagrid.action.process' => 'Do', + + 'nextras.datagrid.pagination.first' => 'First', + 'nextras.datagrid.pagination.previous' => 'Previous', + 'nextras.datagrid.pagination.next' => 'Next', + 'nextras.datagrid.pagination.last' => 'Last', + ], + self::LANG_CS => [ + 'nextras.datagrid.filter.submit' => 'Filtrovat', + 'nextras.datagrid.filter.cancel' => 'Zrušit', + + 'nextras.datagrid.edit.label' => 'Upravit', + 'nextras.datagrid.edit.save' => 'Uložit', + 'nextras.datagrid.edit.cancel' => 'Zrušit', + + 'nextras.datagrid.action.label' => 'Akce', + 'nextras.datagrid.action.prompt' => '- zvolte akci -', + 'nextras.datagrid.action.process' => 'OK', + + 'nextras.datagrid.pagination.first' => 'První', + 'nextras.datagrid.pagination.previous' => 'Poslední', + 'nextras.datagrid.pagination.next' => 'Další', + 'nextras.datagrid.pagination.last' => 'Předchozí', + ], + ]; + + + /** @var string */ + private $language; + + + public function __construct($language) + { + if (!isset(self::TRANSLATIONS[$language])) { + throw new InvalidArgumentException("Unsupported language '$language'"); + } + $this->language = $language; + } + + + public function translate($message, $count = NULL) + { + return isset(self::TRANSLATIONS[$this->language][$message]) ? self::TRANSLATIONS[$this->language][$message] : $message; + } + +} diff --git a/src/exceptions.php b/src/exceptions.php new file mode 100644 index 0000000..33a9616 --- /dev/null +++ b/src/exceptions.php @@ -0,0 +1,20 @@ +