Skip to content

Commit

Permalink
Prizpusobeni nette 3.0, pridano db storage, vylepseny funkce panelu.
Browse files Browse the repository at this point in the history
  • Loading branch information
Milan Kocourek committed May 2, 2019
1 parent d980572 commit 90b5612
Show file tree
Hide file tree
Showing 11 changed files with 771 additions and 469 deletions.
29 changes: 12 additions & 17 deletions data/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,21 @@ extensions:
translator: l10nNetteTranslator\ApplicationDI\Extension

translator:
# languages are required
languages:
-
lang: l10n\Language\CzechLanguage # must implements l10n\Language\ILanguage
plural: l10n\Plural\PluralRule8 # must implements l10n\Plural\IPlural
- lang: l10n\Language\CzechLanguage
plural: l10n\Plural\PluralRule8
namespace: admin

-
lang: l10n\Language\SlovakLanguage # if language implements l10n\Plural\IPlural, you can ignore plural section
- lang: l10n\Language\EnglishLanguage
plural: l10n\Plural\PluralRule1
namespace: admin
default: true

-
lang: l10n\Language\EnglishLanguage
default: true # if is not set, the first language will be set as default
- lang: l10n\Language\CzechLanguage
plural: l10n\Plural\PluralRule8
namespace: api

# storage is optional
storage: @translator_simple_nette_storage # must implements l10nNetteTranslator\Storage\IStorage
storage: @translatorDbStorage

services:
# You can use any storage implements Nette\Caching\IStorage
translator_nette_storage:
class: Nette\Caching\Storages\FileStorage(%appDir%/Texts) # Texts will be saved in %appDir%/Texts as file named by ISO 639-1
autowired: false

translator_simple_nette_storage: l10nNetteTranslator\Storage\SimpleNetteStorage(@translator_nette_storage)
translatorDbStorage: \l10nNetteTranslator\Storage\TranslatorDbStorage(@database.default.context)
21 changes: 21 additions & 0 deletions data/dbStorage.structure.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
CREATE TABLE `localization` (
`id` int(1) unsigned NOT NULL AUTO_INCREMENT,
`text_id` int(1) unsigned NOT NULL,
`lang` char(2) COLLATE utf8mb4_unicode_ci NOT NULL,
`variant` tinyint(1) unsigned NOT NULL DEFAULT '0',
`translation` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `text_id_lang_variant` (`text_id`,`lang`,`variant`),
KEY `lang` (`lang`),
CONSTRAINT `x` FOREIGN KEY (`text_id`) REFERENCES `localization_text` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='text translations';

CREATE TABLE `localization_text` (
`id` int(1) unsigned NOT NULL AUTO_INCREMENT,
`ns` varchar(24) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
`text` varchar(150) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
`ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `ns` (`ns`,`text`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='default texts for translations';
99 changes: 53 additions & 46 deletions src/l10nNetteTranslator/ApplicationDI/Extension.php
Original file line number Diff line number Diff line change
@@ -1,54 +1,61 @@
<?php

namespace l10nNetteTranslator\ApplicationDI;

use Nette\DI\CompilerExtension;
use Nette\InvalidStateException;
use Nette\PhpGenerator\ClassType;

class Extension extends CompilerExtension {
public function loadConfiguration() {
$config = $this->getConfig();

if (empty($config['languages']) || !is_array($config['languages'])) {
throw new InvalidStateException("Languages must be set, must be array and can't be empty");
}

/** @var \Nette\DI\ContainerBuilder $builder */
$builder = $this->getContainerBuilder();

$translator = $builder->addDefinition('l10n_nette_translator.translator');
$translator->setClass('l10nNetteTranslator\Translator');

foreach ($config['languages'] as $language) {
if (empty($language['lang'])) {
throw new InvalidStateException('Key "lang" must be set');
}

$lang = $builder->literal("new $language[lang]");
$plural = isset($language['plural']) ? $builder->literal("new $language[plural]") : $lang;
$default = !empty($language['default']);

$translator->addSetup('addLanguageAndPlural', [$lang, $plural, $default]);
}

if (!empty($config['storage'])) {
$translator->addSetup('setStorage', [$config['storage']]);
}

$panel = $builder->addDefinition('l10n_nette_translator.panel');
$panel->setClass('l10nNetteTranslator\Panel');

$processor = $builder->addDefinition('l10n_nette_translator.processor');
$processor->setClass('l10nNetteTranslator\TranslatorProcessor');
}

public function afterCompile(ClassType $class) {
$initialize = $class->getMethod('initialize');
$initialize->addBody('$this->getService("tracy.bar")->addPanel($this->getService("l10n_nette_translator.panel"));');
$initialize->addBody('$response = $this->getService("l10n_nette_translator.processor")->run();');
$initialize->addBody('if($response instanceof Nette\Application\IResponse) {');
$initialize->addBody(' $response->send($this->getByType("Nette\Http\IRequest"), $this->getByType("Nette\Http\IResponse"));');
$initialize->addBody(' exit();');
$initialize->addBody('}');
}
class Extension extends CompilerExtension
{
public function loadConfiguration()
{
$config = $this->getConfig();

if (empty($config['languages']) || !is_array($config['languages'])) {
throw new InvalidStateException("Languages must be set, must be array and can't be empty");
}

/** @var \Nette\DI\ContainerBuilder $builder */
$builder = $this->getContainerBuilder();

$translator = $builder->addDefinition('l10n_nette_translator.translator');
$translator->setClass('l10nNetteTranslator\Translator');

foreach ($config['languages'] as $language) {
if (empty($language['lang'])) {
throw new InvalidStateException('Key "lang" must be set');
}
if (empty($language['namespace'])) {
throw new InvalidStateException('Key "namespace" must be set');
}

$lang = $builder->literal("new $language[lang]");
$plural = isset($language['plural']) ? $builder->literal("new $language[plural]") : $lang;
$default = !empty($language['default']);

$translator->addSetup('addLanguageAndPlural', [$lang, $plural, $language['namespace'], $default]);
}

if (!empty($config['storage'])) {
$translator->addSetup('setStorage', [$config['storage']]);
}

$panel = $builder->addDefinition('l10n_nette_translator.panel');
$panel->setClass('l10nNetteTranslator\Panel');

$processor = $builder->addDefinition('l10n_nette_translator.processor');
$processor->setClass('l10nNetteTranslator\TranslatorProcessor');
}

public function afterCompile(ClassType $class)
{
$initialize = $class->getMethod('initialize');
$initialize->addBody('$this->getService("tracy.bar")->addPanel($this->getService("l10n_nette_translator.panel"));');
$initialize->addBody('$response = $this->getService("l10n_nette_translator.processor")->run();');
$initialize->addBody('if($response instanceof Nette\Application\IResponse) {');
$initialize->addBody(' $response->send($this->getByType("Nette\Http\IRequest"), $this->getByType("Nette\Http\IResponse"));');
$initialize->addBody(' exit();');
$initialize->addBody('}');
}
}
82 changes: 48 additions & 34 deletions src/l10nNetteTranslator/LanguageAndPlural.php
Original file line number Diff line number Diff line change
@@ -1,41 +1,55 @@
<?php

namespace l10nNetteTranslator;

use l10n\Language\ILanguage;
use l10n\Plural\IPlural;

class LanguageAndPlural {
/** @var ILanguage */
private $language;

/** @var IPlural */
private $plural;

/**
* @return ILanguage
*/
public function getLanguage() {
return $this->language;
}

/**
* @param ILanguage $language
*/
public function setLanguage(ILanguage $language) {
$this->language = $language;
}

/**
* @return IPlural
*/
public function getPlural() {
return $this->plural;
}

/**
* @param IPlural $plural
*/
public function setPlural(IPlural $plural) {
$this->plural = $plural;
}
class LanguageAndPlural
{
/** @var ILanguage */
private $language;

/** @var IPlural */
private $plural;

/**
* @return ILanguage
*/
public function getLanguage()
{
return $this->language;
}

/**
* @param ILanguage $language
*/
public function setLanguage(ILanguage $language)
{
$this->language = $language;
}

/**
* @return IPlural
*/
public function getPlural()
{
return $this->plural;
}

/**
* @param IPlural $plural
*/
public function setPlural(IPlural $plural)
{
$this->plural = $plural;
}

/**
* @return string
*/
public function __toString(): string
{
return $this->language->getIso639_1();
}
}
96 changes: 53 additions & 43 deletions src/l10nNetteTranslator/Panel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,59 @@

namespace l10nNetteTranslator;

use Nette\Object;
use Nette\SmartObject;
use Tracy\IBarPanel;

class Panel extends Object implements IBarPanel {
/** @var \l10nNetteTranslator\Translator */
protected $translator;

public function __construct(\l10nNetteTranslator\Translator $translator) {
$this->translator = $translator;
}

/**
* Return's panel ID
*
* @return string
*/
public function getId() {
return __CLASS__;
}

/**
* Returns the code for the panel tab
*
* @return string
*/
public function getTab() {
ob_start();
require __DIR__ . '/Templates/tab.phtml';

return ob_get_clean();
}

/**
* Returns the code for the panel itself
*
* @return string
*/
public function getPanel() {
$lang_code = $this->translator->getActiveLanguageAndPlural()->getLanguage()->getIso639_1();

ob_start();
require __DIR__ . '/Templates/panel.phtml';

return ob_get_clean();
}
class Panel implements IBarPanel
{
use SmartObject;
/** @var \l10nNetteTranslator\Translator */
protected $translator;

public function __construct(\l10nNetteTranslator\Translator $translator)
{
$this->translator = $translator;
}

/**
* Return's panel ID
*
* @return string
*/
public function getId()
{
return __CLASS__;
}

/**
* Returns the code for the panel tab
*
* @return string
*/
public function getTab()
{
$lang_code = $this->translator->getActiveLanguageAndPlural()->getLanguage()->getIso639_1();
$namespace = $this->translator->getActiveNamespace();

ob_start();
require __DIR__ . '/Templates/tab.phtml';

return ob_get_clean();
}

/**
* Returns the code for the panel itself
*
* @return string
*/
public function getPanel()
{
$lang_code = $this->translator->getActiveLanguageAndPlural()->getLanguage()->getIso639_1();
$namespace = $this->translator->getActiveNamespace();

ob_start();
require __DIR__ . '/Templates/panel.phtml';

return ob_get_clean();
}
}
22 changes: 12 additions & 10 deletions src/l10nNetteTranslator/Storage/IStorage.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
<?php

namespace l10nNetteTranslator\Storage;

use l10nNetteTranslator\Translator;

interface IStorage extends \l10n\Translator\IStorage {
/**
* @param \l10nNetteTranslator\Translator $translator
* @return void
*/
public function setTranslator(Translator $translator);
interface IStorage extends \l10n\Translator\IStorage
{
/**
* @param \l10nNetteTranslator\Translator $translator
* @return void
*/
public function setTranslator(Translator $translator);

/**
* @return \l10nNetteTranslator\Translator
*/
public function getTranslator();
/**
* @return \l10nNetteTranslator\Translator
*/
public function getTranslator();
}
Loading

0 comments on commit 90b5612

Please sign in to comment.