Skip to content
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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
}
},
"autoload": {
"psr-4": { "Nextras\\Datagrid\\": "src" }
"psr-4": { "Nextras\\Datagrid\\": "src" },
"classmap": [
"src/exceptions.php"
]
}
}
18 changes: 9 additions & 9 deletions src/Datagrid.blocks.latte
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
{/define}

{define row-actions-edit-link}
<a href="{link edit! $primary}" class="ajax" data-datagrid-edit>{$control->translate(Edit)}</a>
<a href="{link edit! $primary}" class="ajax" data-datagrid-edit>{_}nextras.datagrid.edit.label{/_}</a>
{/define}

{define row}
Expand Down Expand Up @@ -160,23 +160,23 @@
{define pagination}
<div class="grid-paginator">
{if $paginator->isFirst()}
<span>« {$control->translate(First)}</span>
<span>« {$control->translate(Previous)}</span>
<span>« {_}nextras.datagrid.pagination.first{/_}</span>
<span>« {_}nextras.datagrid.pagination.previous{/_}</span>
{else}
<a href="{link paginate!, 'page' => 1}" class="ajax">« {$control->translate(First)}</a>
<a href="{link paginate!, 'page' => $paginator->page - 1}" class="ajax">« {$control->translate(Previous)}</a>
<a href="{link paginate!, 'page' => 1}" class="ajax">« {_}nextras.datagrid.pagination.first{/_}</a>
<a href="{link paginate!, 'page' => $paginator->page - 1}" class="ajax">« {_}nextras.datagrid.pagination.previous{/_}</a>
{/if}

<span>
<span>{$paginator->page}</span> / <span>{$paginator->pageCount}</span>
</span>

{if $paginator->isLast()}
<span>{$control->translate(Next)} »</span>
<span>{$control->translate(Last)} »</span>
<span>{_}nextras.datagrid.pagination.next{/_} »</span>
<span>{_}nextras.datagrid.pagination.last{/_} »</span>
{else}
<a href="{link paginate!, 'page' => $paginator->page + 1}" class="ajax">{$control->translate(Next)} »</a>
<a href="{link paginate!, 'page' => $paginator->pageCount}" class="ajax">{$control->translate(Last)} »</a>
<a href="{link paginate!, 'page' => $paginator->page + 1}" class="ajax">{_}nextras.datagrid.pagination.next{/_} »</a>
<a href="{link paginate!, 'page' => $paginator->pageCount}" class="ajax">{_}nextras.datagrid.pagination.last{/_} »</a>
{/if}
</div>
{/define}
41 changes: 25 additions & 16 deletions src/Datagrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -94,6 +93,13 @@ class Datagrid extends UI\Control
protected $cellsTemplates = [];


public function __construct()
{
parent::__construct();
$this->translator = new DefaultTranslator(DefaultTranslator::LANG_EN);
Copy link
Member

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()

}


/**
* Adds column
* @param string
Expand Down Expand Up @@ -256,10 +262,9 @@ public function getTranslator()

public function translate($s, $count = null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this method still needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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);
}


Expand All @@ -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);
}
Expand All @@ -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();
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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']);
Expand All @@ -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);

Expand All @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The 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'];
Expand Down
80 changes: 80 additions & 0 deletions src/DefaultTranslator.php
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 => [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I want support more langs :|

Copy link
Member

Choose a reason for hiding this comment

The 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;
}

}
20 changes: 20 additions & 0 deletions src/exceptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
Copy link
Contributor

Choose a reason for hiding this comment

The 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
{
}