Skip to content

Commit

Permalink
Improve translations
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Nov 21, 2023
1 parent 912e2d9 commit be685dc
Show file tree
Hide file tree
Showing 19 changed files with 183 additions and 183 deletions.
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"php": "^8.0",
"ext-json": "*",
"ext-mbstring": "*",
"yiisoft/aliases": "^3.0",
"yiisoft/arrays": "^2.0|^3.0",
"yiisoft/data": "dev-master",
"yiisoft/factory": "^1.0",
Expand All @@ -35,7 +34,6 @@
"yiisoft/json": "^1.0",
"yiisoft/router": "^3.0",
"yiisoft/strings": "^2.0",
"yiisoft/translator-message-php": "^1.1",
"yiisoft/translator": "^3.0",
"yiisoft/view": "^8.0",
"yiisoft/widget": "^2.0",
Expand All @@ -53,7 +51,8 @@
"yiisoft/event-dispatcher": "^1.0",
"yiisoft/log": "^2.0",
"yiisoft/router-fastroute": "^3.0",
"yiisoft/test-support": "^3.0"
"yiisoft/test-support": "^3.0",
"yiisoft/translator-message-php": "^1.1"
},
"extra": {
"branch-alias": {
Expand Down
19 changes: 13 additions & 6 deletions config/di.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,27 @@

declare(strict_types=1);

use Yiisoft\Aliases\Aliases;
use Yiisoft\Translator\CategorySource;
use Yiisoft\Translator\IdMessageReader;
use Yiisoft\Translator\IntlMessageFormatter;
use Yiisoft\Translator\Message\Php\MessageSource;
use Yiisoft\Translator\SimpleMessageFormatter;

/** @var array $params */

return [
'translator.dataview' => [
'definition' => static function (Aliases $aliases) use ($params) {
$messageReader = new MessageSource($aliases->get('@yii-dataview/resources/messages'));
'yii.dataview.categorySource' => [
'definition' => static function () use ($params): CategorySource {
$reader = class_exists(MessageSource::class)
? new MessageSource(dirname(__DIR__) . '/messages')
: new IdMessageReader(); // @codeCoverageIgnore

return new CategorySource($params['yiisoft/translator']['dataviewCategory'], $messageReader, new SimpleMessageFormatter());
$formatter = extension_loaded('intl')
? new IntlMessageFormatter()
: new SimpleMessageFormatter();

return new CategorySource($params['yiisoft/yii-dataview']['translation.category'], $reader, $formatter);
},
'tags' => ['translator.categorySource'],
'tags' => ['translation.categorySource'],
],
];
11 changes: 4 additions & 7 deletions config/params.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

declare(strict_types=1);

use Yiisoft\Yii\DataView\BaseListView;

return [
'yiisoft/aliases' => [
'aliases' => [
'@yii-dataview' => dirname(__DIR__),
],
],
'yiisoft/translator' => [
'dataviewCategory' => 'dataview',
'yiisoft/yii-dataview' => [
'translation.category' => BaseListView::DEFAULT_TRANSLATION_CATEGORY,
],
];
8 changes: 8 additions & 0 deletions messages/es/yii-dataview.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

declare(strict_types=1);

return [
'No results found.' => 'No se han encontrado resultados.',
'Page <b>{currentPage}</b> of <b>{totalPages}</b>' => 'Pagina <b>{currentPage}</b> de <b>{totalPages}</b>',
];
8 changes: 8 additions & 0 deletions messages/ru/yii-dataview.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

declare(strict_types=1);

return [
'No results found.' => 'Результатов не найдено.',
'Page <b>{currentPage}</b> of <b>{totalPages}</b>' => 'Страница <b>{currentPage}</b> из <b>{totalPages}</b>',
];
8 changes: 0 additions & 8 deletions resources/messages/en/dataview.php

This file was deleted.

8 changes: 0 additions & 8 deletions resources/messages/es/dataview.php

This file was deleted.

8 changes: 0 additions & 8 deletions resources/messages/ru/dataview.php

This file was deleted.

97 changes: 56 additions & 41 deletions src/BaseListView.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,51 @@
use Yiisoft\Html\Tag\Div;
use Yiisoft\Html\Tag\Td;
use Yiisoft\Router\UrlGeneratorInterface;
use Yiisoft\Translator\CategorySource;
use Yiisoft\Translator\IdMessageReader;
use Yiisoft\Translator\IntlMessageFormatter;
use Yiisoft\Translator\SimpleMessageFormatter;
use Yiisoft\Translator\Translator;
use Yiisoft\Translator\TranslatorInterface;
use Yiisoft\Widget\Widget;
use Yiisoft\Yii\DataView\Exception\DataReaderNotSetException;

abstract class BaseListView extends Widget
{
/**
* A name for {@see CategorySource} used with translator ({@see TranslatorInterface}) by default.
*/
public const DEFAULT_TRANSLATION_CATEGORY = 'yii-dataview';

/**
* @var TranslatorInterface A translator instance used for translations of messages. If it was not set
* explicitly in the constructor, a default one created automatically in {@see createDefaultTranslator()}.
*/
private TranslatorInterface $translator;

private array $attributes = [];
protected string $emptyText = 'dataview.empty.text';
protected ?string $emptyText = null;
private array $emptyTextAttributes = [];
private string $header = '';
private array $headerAttributes = [];
private string $layout = "{header}\n{toolbar}";
private string $layoutGridTable = "{items}\n{summary}\n{pager}";
private string $pagination = '';
protected ?ReadableDataInterface $dataReader = null;
private SimpleMessageFormatter|null $simpleMessageFormatter = null;
private array $sortLinkAttributes = [];
private string $summary = 'dataview.summary';
private ?string $summary = null;
private array $summaryAttributes = [];
private string $toolbar = '';
protected array $urlArguments = [];
protected array $urlQueryParameters = [];
private bool $withContainer = true;

public function __construct(
private TranslatorInterface|null $translator = null,
private UrlGeneratorInterface|null $urlGenerator = null
TranslatorInterface|null $translator = null,
private UrlGeneratorInterface|null $urlGenerator = null,
private string $translationCategory = self::DEFAULT_TRANSLATION_CATEGORY,
) {
$this->translator = $translator ?? $this->createDefaultTranslator();
}

/**
Expand Down Expand Up @@ -76,7 +92,7 @@ public function attributes(array $values): static
* {@see notShowOnEmpty()}
* {@see emptyTextAttributes()}
*/
public function emptyText(string $emptyText): static
public function emptyText(?string $emptyText): static
{
$new = clone $this;
$new->emptyText = $emptyText;
Expand Down Expand Up @@ -106,15 +122,6 @@ public function getDataReader(): ReadableDataInterface
return $this->dataReader;
}

public function getSimpleMessageFormatter(): SimpleMessageFormatter
{
if ($this->simpleMessageFormatter === null) {
$this->simpleMessageFormatter = new SimpleMessageFormatter();
}

return $this->simpleMessageFormatter;
}

public function getUrlGenerator(): UrlGeneratorInterface
{
if ($this->urlGenerator === null) {
Expand Down Expand Up @@ -258,7 +265,7 @@ public function sortLinkAttributes(array $values): static
* - `{page}`: the page number (1-based) current being displayed.
* - `{pageCount}`: the number of pages available.
*/
public function summary(string $value): static
public function summary(?string $value): static
{
$new = clone $this;
$new->summary = $value;
Expand Down Expand Up @@ -336,14 +343,13 @@ public function withContainer(bool $value = true): static
protected function renderEmpty(int $colspan): Td
{
$emptyTextAttributes = $this->emptyTextAttributes;
$emptyText = $this->getSimpleMessageFormatter()->format($this->emptyText, []);

if ($this->translator !== null) {
$emptyText = $this->translator->translate($this->emptyText, [], 'dataview');
}

$emptyTextAttributes['colspan'] = $colspan;

$emptyText = $this->translator->translate(
$this->emptyText ?? 'No results found.',
category: $this->translationCategory
);

return Td::tag()->attributes($emptyTextAttributes)->content($emptyText);
}

Expand Down Expand Up @@ -427,25 +433,14 @@ private function renderSummary(): string
return '';
}

$summary = $this->getSimpleMessageFormatter()
->format(
$this->summary,
[
'currentPage' => $paginator->getCurrentPage(),
'totalPages' => $paginator->getTotalPages(),
]
);

if ($this->translator !== null) {
$summary = $this->translator->translate(
$this->summary,
[
'currentPage' => $paginator->getCurrentPage(),
'totalPages' => $paginator->getTotalPages(),
],
'dataview',
);
}
$summary = $this->translator->translate(
$this->summary ?? 'Page <b>{currentPage}</b> of <b>{totalPages}</b>',
[
'currentPage' => $paginator->getCurrentPage(),
'totalPages' => $paginator->getTotalPages(),
],
$this->translationCategory,
);

return Div::tag()->attributes($this->summaryAttributes)->content($summary)->encode(false)->render();
}
Expand Down Expand Up @@ -500,4 +495,24 @@ private function renderHeader(): string
->render(),
};
}

/**
* Creates default translator to use if {@see $translator} was not set explicitly in the constructor. Depending on
* "intl" extension availability, either {@see IntlMessageFormatter} or {@see SimpleMessageFormatter} is used as
* formatter.
*
* @return Translator Translator instance used for translations of messages.
*/
private function createDefaultTranslator(): Translator
{
$categorySource = new CategorySource(
$this->translationCategory,
new IdMessageReader(),
extension_loaded('intl') ? new IntlMessageFormatter() : new SimpleMessageFormatter(),
);
$translator = new Translator();
$translator->addCategorySources($categorySource);

return $translator;
}
}
Loading

0 comments on commit be685dc

Please sign in to comment.