Commit 09afa2d6 authored by Jakub Konečný's avatar Jakub Konečný

use translator registered to Latte

parent ca0c4a6c
Pipeline #60893397 failed with stage
in 4 minutes and 57 seconds
......@@ -4,6 +4,8 @@ Version 2.0.0-dev
- BC break: changed parameters of method BookPagesStorage::getIndex
- allowed Nette 3
- BC break: removed property BookControl::$lang
- removed dependency on nexendrie/translation, now it is just suggested
- BC break: BookControl uses translator registered to Latte
Version 1.4.0
- allowed customization of index and page templates
......
......@@ -23,13 +23,13 @@
"nette/application": "^2.4 || ^3.0",
"nette/utils": "^2.4 || ^3.0",
"latte/latte": "^2.4 || ^3.0",
"nexendrie/translation": "^1.1.1 || ^2.0.1",
"nexendrie/utils": "^1.6.1"
},
"require-dev": {
"nexendrie/code-quality": "dev-master",
"tracy/tracy": "^2.4 || ^3.0",
"nexendrie/site-generator": "^1.0",
"nexendrie/translation": "^1.1.1 || ^2.0.1",
"nette/http": "^2.4.7 || ^3.0",
"nette/caching": "^2.5 || ^3.0"
},
......@@ -44,7 +44,8 @@
}
},
"suggest": {
"nette/security": "To use book page conditions related to user"
"nette/security": "To use book page conditions related to user",
"nexendrie/translation": "Translator"
},
"config": {
"classmap-authoritative": true
......
......@@ -97,9 +97,9 @@ $control->onRender[] = function(Book\BookControl $book, string $page) {
Translations
------------
The (few) texts used by the component are in english but you can have them translated into another language if you prefer. If that translation (or some text of it) is not available, english variant is used.
This component contains a few texts that need to be translated. This package (or any of its direct dependencies) does not contain a translator, you need to register one into Latte. We recommend package **nexendrie/translation**.
If you wish, you can also use your own translator. Just set property $translator in constructor. List of all texts used by the component can be seen in *src/lang/book.en.neon*.
This package though contains English and Czech translation in neon and PHP messages catalogue format. They can be found in folder *src/lang*. Default templates of this component do not use messages in default language but message ids in form book.*string*.
Custom templates
----------------
......
......@@ -3,15 +3,10 @@ declare(strict_types=1);
namespace Nexendrie\BookComponent;
use Nette\Localization\ITranslator;
use Nexendrie\Translation\Translator;
use Nexendrie\Translation\Loaders\MessagesCatalogue;
/**
* BookControl
*
* @author Jakub Konečný
* @property ITranslator $translator
* @property callable|BookPagesStorage $pages
* @property string $indexTemplate
* @property string $pageTemplate
......@@ -23,8 +18,6 @@ class BookControl extends \Nette\Application\UI\Control {
private $presenterName;
/** @var string */
private $folder;
/** @var ITranslator|Translator|null */
protected $translator;
/** @var callable|BookPagesStorage */
protected $pages;
/** @var string */
......@@ -34,21 +27,12 @@ class BookControl extends \Nette\Application\UI\Control {
/** @var callable[] */
public $onRender = [];
public function __construct(string $presenterName, string $folder, ITranslator $translator = null) {
public function __construct(string $presenterName, string $folder) {
$this->presenterName = $presenterName;
$this->folder = $folder;
$this->translator = $translator;
$this->pages = new BookPagesStorage();
}
public function getTranslator(): ?ITranslator {
return $this->translator;
}
public function setTranslator(ITranslator $translator): void {
$this->translator = $translator;
}
/**
* @throws \InvalidArgumentException
*/
......@@ -96,22 +80,12 @@ class BookControl extends \Nette\Application\UI\Control {
$this->pageTemplate = $pageTemplate;
}
protected function setupTranslator(): void {
if(is_null($this->translator)) {
$loader = new MessagesCatalogue();
$loader->folders = [__DIR__ . "/lang"];
$this->translator = new Translator($loader);
}
$this->template->setTranslator($this->translator);
}
/**
* @throws \InvalidArgumentException
*/
public function render(string $page = "index"): void {
$this->template->presenterName = $this->presenterName;
$this->template->folder = $this->folder;
$this->setupTranslator();
$pages = $this->getPages();
if(!$pages->hasPage($page)) {
$page = "index";
......
......@@ -5,7 +5,6 @@ namespace Nexendrie\BookComponent;
use Tester\Assert;
use Nexendrie\Translation\Translator;
use Nexendrie\Translation\Loaders\MessagesCatalogue;
require __DIR__ . "/../../bootstrap.php";
......@@ -20,6 +19,7 @@ final class BookControlTest extends \Tester\TestCase {
private $control;
use \Testbench\TComponent;
use \Testbench\TCompiledContainer;
protected function setUp() {
$this->control = new BookControl2();
......@@ -43,19 +43,13 @@ final class BookControlTest extends \Tester\TestCase {
}
public function testTranslator() {
$loader = new MessagesCatalogue();
$loader->folders = [__DIR__ . "/../../../src/lang"];
$this->control->translator = new Translator($loader);
Assert::same("en", $this->control->translator->lang);
$result = $this->control->translator->translate("book.content");
Assert::type("string", $result);
Assert::same("Content", $result);
$this->control->translator->lang = "cs";
$result = $this->control->translator->translate("book.content");
Assert::type("string", $result);
Assert::same("Obsah", $result);
/** @var Translator $translator */
$translator = $this->getService(Translator::class);
Assert::same("Content", $translator->translate("book.content"));
$translator->lang = "cs";
Assert::same("Obsah", $translator->translate("book.content"));
}
public function testInvalidCustomTemplates() {
Assert::exception(function() {
$this->control->indexTemplate = "abc.latte";
......@@ -82,10 +76,8 @@ final class BookControlTest extends \Tester\TestCase {
}
public function testRenderI() {
Assert::type("null", $this->control->translator);
$filename = __DIR__ . "/bookIndexExpected.latte";
$this->checkRenderOutput($this->control, $filename);
Assert::type(Translator::class, $this->control->translator);
}
public function testRenderP1() {
......
......@@ -8,3 +8,12 @@ services:
- Nexendrie\BookComponent\ConditionPermission
- Nexendrie\BookComponent\ConditionUserInRole
- Nexendrie\BookComponent\ConditionUserLoggedIn
extensions:
translation: Nexendrie\Translation\Bridges\NetteDI\TranslationExtension
translation:
default: en
localeResolver: manual
loader:
name: catalogue
folders!:
- %appDir%/../src/lang
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment