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

Improve translations #132

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all 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: 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 @@
* {@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 @@
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 @@
* - `{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 @@ -325,7 +332,7 @@
*
* @param bool $value Whether container is enabled or not.
*/
public function withContainer(bool $value = true): static

Check warning on line 335 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ * * @param bool $value Whether container is enabled or not. */ - public function withContainer(bool $value = true) : static + public function withContainer(bool $value = false) : static { $new = clone $this; $new->withContainer = $value;

Check warning on line 335 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ * * @param bool $value Whether container is enabled or not. */ - public function withContainer(bool $value = true) : static + public function withContainer(bool $value = false) : static { $new = clone $this; $new->withContainer = $value;
{
$new = clone $this;
$new->withContainer = $value;
Expand All @@ -336,14 +343,13 @@
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 @@ -386,7 +392,7 @@
public function render(): string
{
if ($this->dataReader === null) {
throw new DataReaderNotSetException();

Check warning on line 395 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "Throw_": --- Original +++ New @@ @@ public function render() : string { if ($this->dataReader === null) { - throw new DataReaderNotSetException(); + new DataReaderNotSetException(); } return $this->renderGrid(); }

Check warning on line 395 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "Throw_": --- Original +++ New @@ @@ public function render() : string { if ($this->dataReader === null) { - throw new DataReaderNotSetException(); + new DataReaderNotSetException(); } return $this->renderGrid(); }
}

return $this->renderGrid();
Expand Down Expand Up @@ -427,25 +433,14 @@
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 All @@ -461,7 +456,7 @@
);
}

return match ($this->withContainer) {

Check warning on line 459 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "MatchArmRemoval": --- Original +++ New @@ @@ } return match ($this->withContainer) { true => trim($contentGrid . PHP_EOL . Div::tag()->attributes($attributes)->content(PHP_EOL . $this->renderGridTable() . PHP_EOL)->encode(false)->render()), - false => trim($contentGrid . PHP_EOL . $this->renderGridTable()), }; } private function renderGridTable() : string

Check warning on line 459 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "MatchArmRemoval": --- Original +++ New @@ @@ } return match ($this->withContainer) { true => trim($contentGrid . PHP_EOL . Div::tag()->attributes($attributes)->content(PHP_EOL . $this->renderGridTable() . PHP_EOL)->encode(false)->render()), - false => trim($contentGrid . PHP_EOL . $this->renderGridTable()), }; } private function renderGridTable() : string
true => trim(
$contentGrid . PHP_EOL . Div::tag()
->attributes($attributes)
Expand All @@ -469,7 +464,7 @@
->encode(false)
->render()
),
false => trim($contentGrid . PHP_EOL . $this->renderGridTable()),

Check warning on line 467 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "Concat": --- Original +++ New @@ @@ } return match ($this->withContainer) { true => trim($contentGrid . PHP_EOL . Div::tag()->attributes($attributes)->content(PHP_EOL . $this->renderGridTable() . PHP_EOL)->encode(false)->render()), - false => trim($contentGrid . PHP_EOL . $this->renderGridTable()), + false => trim(PHP_EOL . $contentGrid . $this->renderGridTable()), }; } private function renderGridTable() : string

Check warning on line 467 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "ConcatOperandRemoval": --- Original +++ New @@ @@ } return match ($this->withContainer) { true => trim($contentGrid . PHP_EOL . Div::tag()->attributes($attributes)->content(PHP_EOL . $this->renderGridTable() . PHP_EOL)->encode(false)->render()), - false => trim($contentGrid . PHP_EOL . $this->renderGridTable()), + false => trim(PHP_EOL . $this->renderGridTable()), }; } private function renderGridTable() : string

Check warning on line 467 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "ConcatOperandRemoval": --- Original +++ New @@ @@ } return match ($this->withContainer) { true => trim($contentGrid . PHP_EOL . Div::tag()->attributes($attributes)->content(PHP_EOL . $this->renderGridTable() . PHP_EOL)->encode(false)->render()), - false => trim($contentGrid . PHP_EOL . $this->renderGridTable()), + false => trim($contentGrid . $this->renderGridTable()), }; } private function renderGridTable() : string

Check warning on line 467 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "Concat": --- Original +++ New @@ @@ } return match ($this->withContainer) { true => trim($contentGrid . PHP_EOL . Div::tag()->attributes($attributes)->content(PHP_EOL . $this->renderGridTable() . PHP_EOL)->encode(false)->render()), - false => trim($contentGrid . PHP_EOL . $this->renderGridTable()), + false => trim($contentGrid . $this->renderGridTable() . PHP_EOL), }; } private function renderGridTable() : string

Check warning on line 467 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "ConcatOperandRemoval": --- Original +++ New @@ @@ } return match ($this->withContainer) { true => trim($contentGrid . PHP_EOL . Div::tag()->attributes($attributes)->content(PHP_EOL . $this->renderGridTable() . PHP_EOL)->encode(false)->render()), - false => trim($contentGrid . PHP_EOL . $this->renderGridTable()), + false => trim($contentGrid . PHP_EOL), }; } private function renderGridTable() : string

Check warning on line 467 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "UnwrapTrim": --- Original +++ New @@ @@ } return match ($this->withContainer) { true => trim($contentGrid . PHP_EOL . Div::tag()->attributes($attributes)->content(PHP_EOL . $this->renderGridTable() . PHP_EOL)->encode(false)->render()), - false => trim($contentGrid . PHP_EOL . $this->renderGridTable()), + false => $contentGrid . PHP_EOL . $this->renderGridTable(), }; } private function renderGridTable() : string

Check warning on line 467 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "Concat": --- Original +++ New @@ @@ } return match ($this->withContainer) { true => trim($contentGrid . PHP_EOL . Div::tag()->attributes($attributes)->content(PHP_EOL . $this->renderGridTable() . PHP_EOL)->encode(false)->render()), - false => trim($contentGrid . PHP_EOL . $this->renderGridTable()), + false => trim(PHP_EOL . $contentGrid . $this->renderGridTable()), }; } private function renderGridTable() : string

Check warning on line 467 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "ConcatOperandRemoval": --- Original +++ New @@ @@ } return match ($this->withContainer) { true => trim($contentGrid . PHP_EOL . Div::tag()->attributes($attributes)->content(PHP_EOL . $this->renderGridTable() . PHP_EOL)->encode(false)->render()), - false => trim($contentGrid . PHP_EOL . $this->renderGridTable()), + false => trim(PHP_EOL . $this->renderGridTable()), }; } private function renderGridTable() : string

Check warning on line 467 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "ConcatOperandRemoval": --- Original +++ New @@ @@ } return match ($this->withContainer) { true => trim($contentGrid . PHP_EOL . Div::tag()->attributes($attributes)->content(PHP_EOL . $this->renderGridTable() . PHP_EOL)->encode(false)->render()), - false => trim($contentGrid . PHP_EOL . $this->renderGridTable()), + false => trim($contentGrid . $this->renderGridTable()), }; } private function renderGridTable() : string

Check warning on line 467 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "Concat": --- Original +++ New @@ @@ } return match ($this->withContainer) { true => trim($contentGrid . PHP_EOL . Div::tag()->attributes($attributes)->content(PHP_EOL . $this->renderGridTable() . PHP_EOL)->encode(false)->render()), - false => trim($contentGrid . PHP_EOL . $this->renderGridTable()), + false => trim($contentGrid . $this->renderGridTable() . PHP_EOL), }; } private function renderGridTable() : string

Check warning on line 467 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "ConcatOperandRemoval": --- Original +++ New @@ @@ } return match ($this->withContainer) { true => trim($contentGrid . PHP_EOL . Div::tag()->attributes($attributes)->content(PHP_EOL . $this->renderGridTable() . PHP_EOL)->encode(false)->render()), - false => trim($contentGrid . PHP_EOL . $this->renderGridTable()), + false => trim($contentGrid . PHP_EOL), }; } private function renderGridTable() : string

Check warning on line 467 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "UnwrapTrim": --- Original +++ New @@ @@ } return match ($this->withContainer) { true => trim($contentGrid . PHP_EOL . Div::tag()->attributes($attributes)->content(PHP_EOL . $this->renderGridTable() . PHP_EOL)->encode(false)->render()), - false => trim($contentGrid . PHP_EOL . $this->renderGridTable()), + false => $contentGrid . PHP_EOL . $this->renderGridTable(), }; } private function renderGridTable() : string
};
}

Expand All @@ -480,7 +475,7 @@
$this->layoutGridTable,
[
'{header}' => $this->renderHeader(),
'{toolbar}' => $this->toolbar,

Check warning on line 478 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "ArrayItem": --- Original +++ New @@ @@ } private function renderGridTable() : string { - return trim(strtr($this->layoutGridTable, ['{header}' => $this->renderHeader(), '{toolbar}' => $this->toolbar, '{items}' => $this->renderItems(), '{summary}' => $this->renderSummary(), '{pager}' => $this->renderPagination()])); + return trim(strtr($this->layoutGridTable, ['{header}' => $this->renderHeader(), '{toolbar}' > $this->toolbar, '{items}' => $this->renderItems(), '{summary}' => $this->renderSummary(), '{pager}' => $this->renderPagination()])); } private function renderHeader() : string {

Check warning on line 478 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "ArrayItem": --- Original +++ New @@ @@ } private function renderGridTable() : string { - return trim(strtr($this->layoutGridTable, ['{header}' => $this->renderHeader(), '{toolbar}' => $this->toolbar, '{items}' => $this->renderItems(), '{summary}' => $this->renderSummary(), '{pager}' => $this->renderPagination()])); + return trim(strtr($this->layoutGridTable, ['{header}' => $this->renderHeader(), '{toolbar}' > $this->toolbar, '{items}' => $this->renderItems(), '{summary}' => $this->renderSummary(), '{pager}' => $this->renderPagination()])); } private function renderHeader() : string {
'{items}' => $this->renderItems(),
'{summary}' => $this->renderSummary(),
'{pager}' => $this->renderPagination(),
Expand All @@ -500,4 +495,24 @@
->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