From dbe559b7814b74081cd8041d899ac0c272e96e66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ku=C4=8Dera?= Date: Mon, 3 Jul 2017 13:29:16 +0200 Subject: [PATCH 1/6] Expand all translated texts with namespaces & add DefaultTranslator to prevent collisions --- src/Datagrid.blocks.latte | 18 ++++++++-------- src/Datagrid.php | 34 ++++++++++++++++------------- src/DefaultTranslator.php | 45 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 24 deletions(-) create mode 100644 src/DefaultTranslator.php diff --git a/src/Datagrid.blocks.latte b/src/Datagrid.blocks.latte index c740e3b..da51d4e 100644 --- a/src/Datagrid.blocks.latte +++ b/src/Datagrid.blocks.latte @@ -88,7 +88,7 @@ {/define} {define row-actions-edit-link} - {$control->translate(Edit)} + {$control->translate(nextras.datagrid.edit.label)} {/define} {define row} @@ -160,11 +160,11 @@ {define pagination}
{if $paginator->isFirst()} - « {$control->translate(First)} - « {$control->translate(Previous)} + « {$control->translate(nextras.datagrid.pagination.first)} + « {$control->translate(nextras.datagrid.pagination.previous)} {else} - « {$control->translate(First)} - « {$control->translate(Previous)} + « {$control->translate(nextras.datagrid.pagination.first)} + « {$control->translate(nextras.datagrid.pagination.previous)} {/if} @@ -172,11 +172,11 @@ {if $paginator->isLast()} - {$control->translate(Next)} » - {$control->translate(Last)} » + {$control->translate(nextras.datagrid.pagination.next)} » + {$control->translate(nextras.datagrid.pagination.last)} » {else} - {$control->translate(Next)} » - {$control->translate(Last)} » + {$control->translate(nextras.datagrid.pagination.next)} » + {$control->translate(nextras.datagrid.pagination.last)} » {/if}
{/define} diff --git a/src/Datagrid.php b/src/Datagrid.php index 12d6277..802e8f1 100644 --- a/src/Datagrid.php +++ b/src/Datagrid.php @@ -94,6 +94,13 @@ class Datagrid extends UI\Control protected $cellsTemplates = []; + public function __construct() + { + parent::__construct(); + $this->translator = new DefaultTranslator(); + } + + /** * Adds column * @param string @@ -256,10 +263,9 @@ public function getTranslator() 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); } @@ -338,11 +344,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 +435,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 +452,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,20 +466,18 @@ 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'); + $form['actions']->addSubmit('process', 'nextras.datagrid.action.process'); } - if ($this->translator) { - $form->setTranslator($this->translator); - } + $form->setTranslator($this->translator); $form->onSuccess[] = function() {}; // fix for Nette Framework 2.0.x $form->onSubmit[] = [$this, 'processForm']; diff --git a/src/DefaultTranslator.php b/src/DefaultTranslator.php new file mode 100644 index 0000000..4a32da3 --- /dev/null +++ b/src/DefaultTranslator.php @@ -0,0 +1,45 @@ + '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', + ]; + + + public function translate($message, $count = NULL) + { + return isset(self::TRANSLATIONS[$message]) ? self::TRANSLATIONS[$message] : $message; + } + +} From b2814641f16ee6f7787290a7d5962d2e4ec4fc90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ku=C4=8Dera?= Date: Mon, 3 Jul 2017 13:29:26 +0200 Subject: [PATCH 2/6] typo --- src/Datagrid.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Datagrid.php b/src/Datagrid.php index 802e8f1..b2d9a92 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; From 2d58d880019823c222dcc644a805971100d163ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ku=C4=8Dera?= Date: Tue, 4 Jul 2017 11:21:56 +0200 Subject: [PATCH 3/6] add Czech translations --- composer.json | 5 +++- src/Datagrid.php | 2 +- src/DefaultTranslator.php | 61 ++++++++++++++++++++++++++++++--------- src/exceptions.php | 15 ++++++++++ 4 files changed, 68 insertions(+), 15 deletions(-) create mode 100644 src/exceptions.php 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.php b/src/Datagrid.php index b2d9a92..3fd3586 100644 --- a/src/Datagrid.php +++ b/src/Datagrid.php @@ -96,7 +96,7 @@ class Datagrid extends UI\Control public function __construct() { parent::__construct(); - $this->translator = new DefaultTranslator(); + $this->translator = new DefaultTranslator(DefaultTranslator::LANG_EN); } diff --git a/src/DefaultTranslator.php b/src/DefaultTranslator.php index 4a32da3..590b903 100644 --- a/src/DefaultTranslator.php +++ b/src/DefaultTranslator.php @@ -18,28 +18,63 @@ class DefaultTranslator implements ITranslator use SmartObject; + const LANG_EN = 'en'; + const LANG_CS = 'cs'; + const TRANSLATIONS = [ - 'nextras.datagrid.filter.submit' => 'Filter', - 'nextras.datagrid.filter.cancel' => 'Cancel', + self::LANG_EN => [ + '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.edit.label' => 'Edit', - 'nextras.datagrid.edit.save' => 'Save', - 'nextras.datagrid.edit.cancel' => 'Cancel', + '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.action.label' => 'Action', - 'nextras.datagrid.action.prompt' => '- select action -', - 'nextras.datagrid.action.process' => 'Do', + 'nextras.datagrid.edit.label' => 'Upravit', + 'nextras.datagrid.edit.save' => 'Uložit', + 'nextras.datagrid.edit.cancel' => 'Zrušit', - 'nextras.datagrid.pagination.first' => 'First', - 'nextras.datagrid.pagination.previous' => 'Previous', - 'nextras.datagrid.pagination.next' => 'Next', - 'nextras.datagrid.pagination.last' => 'Last', + '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[$message]) ? self::TRANSLATIONS[$message] : $message; + 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..d33178c --- /dev/null +++ b/src/exceptions.php @@ -0,0 +1,15 @@ + Date: Tue, 4 Jul 2017 11:36:56 +0200 Subject: [PATCH 4/6] use translate macro in template --- src/Datagrid.blocks.latte | 18 +++++++++--------- src/Datagrid.php | 6 ++++++ src/exceptions.php | 5 +++++ 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/Datagrid.blocks.latte b/src/Datagrid.blocks.latte index da51d4e..f38ba09 100644 --- a/src/Datagrid.blocks.latte +++ b/src/Datagrid.blocks.latte @@ -88,7 +88,7 @@ {/define} {define row-actions-edit-link} - {$control->translate(nextras.datagrid.edit.label)} + {_}nextras.datagrid.edit.label{/_} {/define} {define row} @@ -160,11 +160,11 @@ {define pagination}
{if $paginator->isFirst()} - « {$control->translate(nextras.datagrid.pagination.first)} - « {$control->translate(nextras.datagrid.pagination.previous)} + « {_}nextras.datagrid.pagination.first{/_} + « {_}nextras.datagrid.pagination.previous{/_} {else} - « {$control->translate(nextras.datagrid.pagination.first)} - « {$control->translate(nextras.datagrid.pagination.previous)} + « {_}nextras.datagrid.pagination.first{/_} + « {_}nextras.datagrid.pagination.previous{/_} {/if} @@ -172,11 +172,11 @@ {if $paginator->isLast()} - {$control->translate(nextras.datagrid.pagination.next)} » - {$control->translate(nextras.datagrid.pagination.last)} » + {_}nextras.datagrid.pagination.next{/_} » + {_}nextras.datagrid.pagination.last{/_} » {else} - {$control->translate(nextras.datagrid.pagination.next)} » - {$control->translate(nextras.datagrid.pagination.last)} » + {_}nextras.datagrid.pagination.next{/_} » + {_}nextras.datagrid.pagination.last{/_} » {/if}
{/define} diff --git a/src/Datagrid.php b/src/Datagrid.php index 3fd3586..9b35a7c 100644 --- a/src/Datagrid.php +++ b/src/Datagrid.php @@ -273,6 +273,10 @@ 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); } @@ -288,6 +292,8 @@ public function render() $this->template->showFilterCancel = $this->filterDataSource != $this->filterDefaults; // @ intentionaly $this->template->setFile(__DIR__ . '/Datagrid.latte'); + $this->template->setTranslator($this->translator); + $this->onRender($this); $this->template->render(); } diff --git a/src/exceptions.php b/src/exceptions.php index d33178c..33a9616 100644 --- a/src/exceptions.php +++ b/src/exceptions.php @@ -13,3 +13,8 @@ class InvalidArgumentException extends \InvalidArgumentException { } + + +class LogicException extends \LogicException +{ +} From f8a550063141ed0b6bf06c9127bbfb48907485e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ku=C4=8Dera?= Date: Thu, 6 Jul 2017 11:55:23 +0200 Subject: [PATCH 5/6] template translator typo --- src/Datagrid.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Datagrid.php b/src/Datagrid.php index 9b35a7c..179358b 100644 --- a/src/Datagrid.php +++ b/src/Datagrid.php @@ -292,8 +292,6 @@ public function render() $this->template->showFilterCancel = $this->filterDataSource != $this->filterDefaults; // @ intentionaly $this->template->setFile(__DIR__ . '/Datagrid.latte'); - $this->template->setTranslator($this->translator); - $this->onRender($this); $this->template->render(); } @@ -563,9 +561,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; } From b5e9daa87252383367dc0ca0b4e6c0eb157bb14a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ku=C4=8Dera?= Date: Thu, 6 Jul 2017 11:55:52 +0200 Subject: [PATCH 6/6] lazy getTranslator() --- src/Datagrid.php | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/Datagrid.php b/src/Datagrid.php index 179358b..50c2c3a 100644 --- a/src/Datagrid.php +++ b/src/Datagrid.php @@ -93,13 +93,6 @@ class Datagrid extends UI\Control protected $cellsTemplates = []; - public function __construct() - { - parent::__construct(); - $this->translator = new DefaultTranslator(DefaultTranslator::LANG_EN); - } - - /** * Adds column * @param string @@ -256,6 +249,9 @@ public function setTranslator(ITranslator $translator) public function getTranslator() { + if (!$this->translator) { + $this->translator = new DefaultTranslator(DefaultTranslator::LANG_EN); + } return $this->translator; } @@ -281,6 +277,8 @@ public function render() $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; @@ -480,8 +478,6 @@ public function createComponentForm() $form['actions']->addSubmit('process', 'nextras.datagrid.action.process'); } - $form->setTranslator($this->translator); - $form->onSuccess[] = function() {}; // fix for Nette Framework 2.0.x $form->onSubmit[] = [$this, 'processForm']; return $form;