-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Translation texts with namespaces #82
base: master
Are you sure you want to change the base?
Changes from 4 commits
dbe559b
b281464
2d58d88
253b622
f8a5500
b5e9daa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -94,6 +93,13 @@ class Datagrid extends UI\Control | |
protected $cellsTemplates = []; | ||
|
||
|
||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
$this->translator = new DefaultTranslator(DefaultTranslator::LANG_EN); | ||
} | ||
|
||
|
||
/** | ||
* Adds column | ||
* @param string | ||
|
@@ -256,10 +262,9 @@ public function getTranslator() | |
|
||
public function translate($s, $count = null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this method still needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, it is still used at line 115 |
||
{ | ||
$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,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); | ||
} | ||
|
@@ -283,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(); | ||
} | ||
|
@@ -338,11 +349,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 +440,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 +457,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 +471,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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if the translator would be lazy, we would probably set it only for render, eg. in render method. |
||
|
||
$form->onSuccess[] = function() {}; // fix for Nette Framework 2.0.x | ||
$form->onSubmit[] = [$this, 'processForm']; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Nextras community extensions of Nette Framework | ||
* | ||
* @license MIT | ||
* @link https://github.com/nextras | ||
*/ | ||
|
||
namespace Nextras\Datagrid; | ||
|
||
use Nette\Localization\ITranslator; | ||
use Nette\SmartObject; | ||
|
||
|
||
class DefaultTranslator implements ITranslator | ||
{ | ||
use SmartObject; | ||
|
||
|
||
const LANG_EN = 'en'; | ||
const LANG_CS = 'cs'; | ||
|
||
const TRANSLATIONS = [ | ||
self::LANG_EN => [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure I want support more langs :| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, czech is quite popular language, I've heard =) |
||
'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; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO it's more appropriate to split these into single files |
||
|
||
/** | ||
* This file is part of the Nextras community extensions of Nette Framework | ||
* | ||
* @license MIT | ||
* @link https://github.com/nextras | ||
*/ | ||
|
||
namespace Nextras\Datagrid; | ||
|
||
|
||
class InvalidArgumentException extends \InvalidArgumentException | ||
{ | ||
} | ||
|
||
|
||
class LogicException extends \LogicException | ||
{ | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about making this lazy by getTranslator()