Skip to content

Commit

Permalink
added filters
Browse files Browse the repository at this point in the history
dkMorlok committed Nov 6, 2018
1 parent 5650941 commit 8b40e19
Showing 4 changed files with 70 additions and 36 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@
"email": "morlokdk@gmail.com"
}],
"require": {
"php": ">=5.4.0",
"php": ">=5.6.0",
"nette/utils": "^2.3",
"nette/safe-stream": "^2.3"
},
44 changes: 21 additions & 23 deletions src/Localization/LocalizationExtension.php
Original file line number Diff line number Diff line change
@@ -3,56 +3,54 @@
namespace Smartsupp\Localization;

use Nette\DI\CompilerExtension;
use Nette\Utils\Validators;

class LocalizationExtension extends CompilerExtension
{

public $defaults = [
'tempDir' => '%tempDir%/cache/Localization',
'translatesDir' => null,
'debugMode' => null,
'sections' => null,
'alias' => null,
'debugMode' => '%debugMode%',
'sections' => [],
'alias' => [],
'parameters' => [],
'filters' => [],
];

private $tempDir;


public function __construct($tempDir = null)
{
$this->tempDir = $tempDir;
}


public function loadConfiguration()
{
$container = $this->getContainerBuilder();
$config = $this->getConfig($this->defaults);
$debugMode = $config['debugMode'] !== null ? $config['debugMode'] : $container->parameters['debugMode'];
$tempDir = $this->tempDir ? $this->tempDir : $container->parameters['tempDir'];

Validators::assertField($config, 'translatesDir', 'string');

$container->addDefinition($this->prefix('translatesStorage'))
->setClass('Smartsupp\Localization\DirectoryStorage')
->setClass(DirectoryStorage::class)
->setArguments([$config['translatesDir']]);

$container->addDefinition($this->prefix('translatesLoader'))
->setClass('Smartsupp\Localization\TranslatesLoader')
->addSetup('$debugMode', [$debugMode])
->addSetup('setTempDir', [$tempDir . '/cache/_Wimo.Localization']);
->setClass(TranslatesLoader::class)
->addSetup('$debugMode', [$config['debugMode']])
->addSetup('setTempDir', [$config['tempDir']]);

$translatorFactory = $container->addDefinition($this->prefix('translatorFactory'))
->setClass('Smartsupp\Localization\TranslatorFactory')
->addSetup('$debugMode', [$debugMode])
->setClass(TranslatorFactory::class)
->addSetup('$defaultSections', [$config['sections']])
->addSetup('setParameters', [$config['parameters']]);

if ($config['sections']) {
$translatorFactory->addSetup('$defaultSections', [$config['sections']]);
}
if ($config['alias']) {
if (count($config['alias'])) {
foreach ($config['alias'] as $from => $to) {
$translatorFactory->addSetup('setAlias', [$from, $to]);
}
}

if (count($config['filters'])) {
foreach ($config['filters'] as $filter) {
$translatorFactory->addSetup('addFilter', [$filter]);
}
}
}

}
35 changes: 28 additions & 7 deletions src/Localization/Translator.php
Original file line number Diff line number Diff line change
@@ -2,18 +2,20 @@

namespace Smartsupp\Localization;

use Nette\Utils\Validators;

class Translator implements ITranslator
{

/** @var boolean */
public $debugMode = false;

/** @var array translation table */
private $dictionary = [];

/** @var array */
private $parameters = [];

/** @var array */
private $filters = [];


/**
* Set dictionary
@@ -53,6 +55,18 @@ public function getParameters()
}


/**
* @param callable $filter
*/
public function addFilter($filter)
{
if (!Validators::isCallable($filter)) {
throw new \Nette\Utils\AssertionException("Filter is not callable");
}
$this->filters[] = $filter;
}


/**
* Has message?
* @param string $key
@@ -92,11 +106,19 @@ public function translate($key, $args = null)
}
}

return $this->replaceParameters($message);
if (count($this->parameters) > 0) {
$message = $this->applyParameters($message);
}

foreach ($this->filters as $filter) {
$message = call_user_func_array($filter, [$message, $key]);
}

return $message;
}


private function replaceParameters($string)
private function applyParameters($string)
{
if (strpos($string, '{') === false) {
return $string;
@@ -105,8 +127,7 @@ private function replaceParameters($string)
if (!isset($this->parameters[$matches[1]])) {
return $matches[0];
} else {
// TODO: prevent cyclic replacing
return $this->replaceParameters($this->parameters[$matches[1]]);
return $this->applyParameters($this->parameters[$matches[1]]);
}
}, $string);
}
25 changes: 20 additions & 5 deletions src/Localization/TranslatorFactory.php
Original file line number Diff line number Diff line change
@@ -2,12 +2,11 @@

namespace Smartsupp\Localization;

use Nette\Utils\Validators;

class TranslatorFactory
{

/** @var boolean */
public $debugMode = false;

/** @var array */
public $alias = [];

@@ -20,6 +19,9 @@ class TranslatorFactory
/** @var array */
private $parameters = [];

/** @var array */
private $filters = [];

/** @var TranslatesLoader */
private $loader;

@@ -53,6 +55,18 @@ public function setParameters(array $parameters)
}


/**
* @param callable $filter
*/
public function addFilter($filter)
{
if (!Validators::isCallable($filter)) {
throw new \Nette\Utils\AssertionException("Filter is not callable");
}
$this->filters[] = $filter;
}


/**
* @param string $lang
* @param array $sections
@@ -61,10 +75,11 @@ public function setParameters(array $parameters)
public function create($lang, array $sections = [])
{
$translator = new Translator();
$translator->debugMode = $this->debugMode;

$translator->setParameters(['lang' => $lang]);
$translator->setParameters($this->parameters);
foreach ($this->filters as $filter) {
$translator->addFilter($filter);
}

if (isset($this->alias[$lang])) {
$lang = $this->alias[$lang];

0 comments on commit 8b40e19

Please sign in to comment.