diff --git a/composer.json b/composer.json index 3ad256f..679d496 100644 --- a/composer.json +++ b/composer.json @@ -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" }, diff --git a/src/Localization/LocalizationExtension.php b/src/Localization/LocalizationExtension.php index 78caf8c..8e54f61 100644 --- a/src/Localization/LocalizationExtension.php +++ b/src/Localization/LocalizationExtension.php @@ -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]); + } + } } } diff --git a/src/Localization/Translator.php b/src/Localization/Translator.php index 7fd9f96..963b50f 100644 --- a/src/Localization/Translator.php +++ b/src/Localization/Translator.php @@ -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); } diff --git a/src/Localization/TranslatorFactory.php b/src/Localization/TranslatorFactory.php index 44f601c..7d318cd 100644 --- a/src/Localization/TranslatorFactory.php +++ b/src/Localization/TranslatorFactory.php @@ -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];