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

Testing PR for support nette 3.0 #158

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
11 changes: 5 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ cache:
- $HOME/.composer/cache

php:
- 5.6
- 7.0
- 7.1
- 7.2
- 7.3

env:
- # dev
Expand All @@ -20,14 +19,14 @@ env:
matrix:
fast_finish: true
include:
- php: 7.2
- php: 7.3
env: COMPOSER_EXTRA_ARGS="--prefer-stable" COVERAGE="--coverage ./coverage.xml --coverage-src ./src" TESTER_RUNTIME="phpdbg"
- php: 7.2
- php: 7.3
env: COMPOSER_EXTRA_ARGS="--prefer-stable" PHPSTAN=1
- php: 7.2
- php: 7.3
env: COMPOSER_EXTRA_ARGS="--prefer-stable" CODING_STANDARD=1
exclude:
- php: 7.2
- php: 7.3
env: COMPOSER_EXTRA_ARGS="--prefer-stable"
allow_failures:
- env:
Expand Down
28 changes: 14 additions & 14 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
"issues": "https://github.com/kdyby/translation/issues"
},
"require": {
"php": "^5.6 || ^7.0",
"kdyby/strict-objects": "^1.0",
"latte/latte": "^2.4.6@dev",
"nette/caching": "^2.5@dev",
"nette/di": "^2.4.10@dev",
"nette/finder": "^2.4.1@dev",
"nette/http": "^2.4.7@dev",
"nette/neon": "^2.4.2@dev",
"nette/php-generator": "^2.6.3@dev || ^3.0.1@dev",
"php": ">=7.1",
"kdyby/strict-objects": "dev-master",
"latte/latte": "^3.0@dev",
"nette/caching": "^3.0@dev",
"nette/di": "^3.0@dev",
"nette/finder": "^3.0@dev",
"nette/http": "^3.0@dev",
"nette/neon": "^3.0@dev",
"nette/php-generator": "^3.0.1@dev",
"nette/reflection": "^2.4.2@dev",
"nette/utils": "^2.4.5@dev || ^3.0@dev",
"nette/utils": "^3.0@dev",
"psr/log": "^1.0",
"symfony/translation": "^3.4 || ^4.0",
"symfony/config": "^3.4 || ^4.0"
Expand All @@ -40,10 +40,10 @@
"symfony/yaml": "If you wanna store translations in YAML format - supports multiline strings."
},
"require-dev": {
"nette/application": "^2.4.9@dev",
"nette/bootstrap": "^2.4.5@dev",
"nette/forms": "^2.4.6@dev",
"tracy/tracy": "^2.4.9@dev",
"nette/application": "^3.0@dev",
"nette/bootstrap": "^3.0@dev",
"nette/forms": "^3.0@dev",
"tracy/tracy": "^3.0@dev",
"kdyby/console": "^2.7.1@dev",
"kdyby/monolog": "^1.3.2@dev",
"symfony/yaml": "^3.4 || ^4.0",
Expand Down
7 changes: 6 additions & 1 deletion src/Caching/PhpFileStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ class PhpFileStorage extends \Nette\Caching\Storages\FileStorage implements \Net
*/
public $hint;

/** additional cache structure */
private const
FILE = 'file',
HANDLE = 'handle';

/**
* Reads cache data from disk.
*
Expand All @@ -43,7 +48,7 @@ protected function readData($meta)
* @param string $key
* @return string
*/
protected function getCacheFile($key)
protected function getCacheFile(string $key): string
{
$cacheKey = substr_replace(
$key,
Expand Down
45 changes: 27 additions & 18 deletions src/DI/TranslationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@
use Nette\Bridges\ApplicationLatte\ILatteFactory;
use Nette\Configurator;
use Nette\DI\Compiler;
use Nette\DI\Definitions\Definition;
use Nette\DI\Definitions\FactoryDefinition;
use Nette\DI\Helpers;
use Nette\DI\ServiceDefinition;
use Nette\DI\Statement;
use Nette\PhpGenerator\ClassType as ClassTypeGenerator;
use Nette\PhpGenerator\PhpLiteral;
use Nette\Reflection\ClassType as ReflectionClassType;
use Nette\Utils\Callback;
use Nette\Utils\Finder;
use Nette\Utils\Validators;
use Symfony\Component\Translation\Extractor\ChainExtractor;
Expand Down Expand Up @@ -98,18 +98,22 @@ class TranslationExtension extends \Nette\DI\CompilerExtension

public function __construct()
{
$this->defaults['cache'] = new Statement($this->defaults['cache'], ['%tempDir%/cache']);

}

public function loadConfiguration()
{
$this->loaders = [];

$builder = $this->getContainerBuilder();

$cacheDir = Helpers::expand('%tempDir%/cache', $builder->parameters);
$this->defaults['cache'] = new Statement($this->defaults['cache'], [dirname($cacheDir)]);

$this->setConfig($this->defaults);
$config = $this->getConfig();

$translator = $builder->addDefinition($this->prefix('default'))
->setClass(KdybyTranslator::class, [$this->prefix('@userLocaleResolver')])
->setFactory(KdybyTranslator::class, [$this->prefix('@userLocaleResolver')])
->addSetup('?->setTranslator(?)', [$this->prefix('@userLocaleResolver.param'), '@self'])
->addSetup('setDefaultLocale', [$config['default']])
->addSetup('setLocaleWhitelist', [$config['whitelist']]);
Expand All @@ -118,11 +122,12 @@ public function loadConfiguration()
$translator->addSetup('setFallbackLocales', [$config['fallback']]);

$catalogueCompiler = $builder->addDefinition($this->prefix('catalogueCompiler'))
->setClass(CatalogueCompiler::class, self::filterArgs($config['cache']));
->setFactory(CatalogueCompiler::class, self::filterArgs($config['cache']));

if ($config['debugger'] && interface_exists(IBarPanel::class)) {
$appDir = Helpers::expand('%appDir%', $builder->parameters);
$builder->addDefinition($this->prefix('panel'))
->setClass(Panel::class, [dirname($builder->expand('%appDir%'))])
->setFactory(Panel::class, [dirname($appDir)])
->addSetup('setLocaleWhitelist', [$config['whitelist']]);

$translator->addSetup('?->register(?)', [$this->prefix('@panel'), '@self']);
Expand Down Expand Up @@ -269,19 +274,22 @@ public function beforeCompile()

$this->beforeCompileLogging($config);

$registerToLatte = function (ServiceDefinition $def) {
$def->addSetup('?->onCompile[] = function($engine) { ?::install($engine->getCompiler()); }', ['@self', new PhpLiteral(TranslateMacros::class)]);
/** @var Definition|FactoryDefinition $def */
$registerToLatte = function (Definition $def) {
$def->getResultDefinition()
->addSetup('?->onCompile[] = function($engine) { ?::install($engine->getCompiler()); }', ['@self', new PhpLiteral(TranslateMacros::class)]);

$def->addSetup('addProvider', ['translator', $this->prefix('@default')])
$def->getResultDefinition()
->addSetup('addProvider', ['translator', $this->prefix('@default')])
->addSetup('addFilter', ['translate', [$this->prefix('@helpers'), 'translateFilterAware']]);
};

$latteFactoryService = $builder->getByType(ILatteFactory::class);
if (!$latteFactoryService || !self::isOfType($builder->getDefinition($latteFactoryService)->getClass(), LatteEngine::class)) {
if (!$latteFactoryService || !self::isOfType($builder->getDefinition($latteFactoryService)->getType(), LatteEngine::class)) {
$latteFactoryService = 'nette.latteFactory';
}

if ($builder->hasDefinition($latteFactoryService) && self::isOfType($builder->getDefinition($latteFactoryService)->getClass(), LatteEngine::class)) {
if ($builder->hasDefinition($latteFactoryService) && self::isOfType($builder->getDefinition($latteFactoryService)->getType(), ILatteFactory::class)) {
$registerToLatte($builder->getDefinition($latteFactoryService));
}

Expand Down Expand Up @@ -341,11 +349,12 @@ public function beforeCompile()
$config['dirs'] = array_merge($config['dirs'], array_values($extension->getTranslationResources()));
}

$config['dirs'] = array_map(function ($dir) {
return str_replace((DIRECTORY_SEPARATOR === '/') ? '\\' : '/', DIRECTORY_SEPARATOR, $dir);
$config['dirs'] = array_map(function ($dir) use ($builder) {
$path = Helpers::expand($dir, $builder->parameters);
return str_replace((DIRECTORY_SEPARATOR === '/') ? '\\' : '/', DIRECTORY_SEPARATOR, $path);
}, $config['dirs']);

$dirs = array_values(array_filter($config['dirs'], Callback::closure('is_dir')));
$dirs = array_values(array_filter($config['dirs'], \Closure::fromCallable('is_dir')));
if (count($dirs) > 0) {
foreach ($dirs as $dir) {
$builder->addDependency($dir);
Expand Down Expand Up @@ -420,7 +429,7 @@ protected function validateResource($format, $file, $locale, $domain)

try {
$def = $builder->getDefinition($this->loaders[$format]);
$refl = ReflectionClassType::from($def->getEntity() ?: $def->getClass());
$refl = ReflectionClassType::from($def->getEntity() ?: $def->getType());
$method = $refl->getConstructor();
if ($method !== NULL && $method->getNumberOfRequiredParameters() > 1) {
return;
Expand Down Expand Up @@ -454,9 +463,9 @@ public function afterCompile(ClassTypeGenerator $class)
/**
* {@inheritdoc}
*/
public function getConfig(array $defaults = NULL, $expand = TRUE)
public function getConfig(): array
{
return parent::getConfig($this->defaults) + ['fallback' => ['en_US']];
return parent::getConfig() + ['fallback' => ['en_US']];
}

private function isRegisteredConsoleExtension()
Expand Down
4 changes: 2 additions & 2 deletions src/Diagnostics/Panel.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function __construct($rootDir)
*
* @return string
*/
public function getTab()
public function getTab(): ?string
{
return '<span title="Translation"><img width="16px" height="16px" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB90DExAuL9uIsPUAAAQ2SURBVFjD7Ze9bxxVFMV/583skjgOIXEaKCNFAgmQ+ExS8qEUCEHH30BBScG/QM1fEmgoAAkoAigSKKFASElHESEFbGcdez7uoXhvZteW7ewmQjRMsTs7s/Pufeece+4d+P/4jw8ddvHq1avv2b5mm02Lr9Y7/gpQBCFDD5YhgsAoRLhHMtHnRYNAAb3yt92//9y3v3x+MFY6LIGIuGYbKedngzAGsPK5Kb8BGRAu9xiuL+zQ6NphsQ5NYB7YhA1jQCObyOHG/3sMlpM0hmGNcmXh7w9PYL5wfsphbCMPaOR7BipEJahV0FHZvZ2D+1i2qY+gYERCZTGZzLdFkLNIht/v72AHGM5Pa85MqowUc4p81PaPo2CgIa9RxOZ8jowDZm0WXo2oBdttN8ihIABC5dPLI7BIQQA5prEGIeYdbnVtSSpD3WCaCCqpMB8jXSshEBFEROZ9TCTvepC/be63HWA2ppNRoJttP1flvkp5BArmNBQl5zSIMJtNRxJEwHqVOF1XGLHVdSQp+0CpAi1UytJVYHteBQ7cm7AgIKnAH7CWhBDrVRqfud91UERLFNPyihQMwaOUIAI5I9D0PU2Xz9frCjs4WVVUMhJstR2SwaUWgvx7VQqyG2b4IoIoO9xsWipllJ6sK1JB7Exd4whmbUcYlCVcRKrVq0DSqCIVRlPZ4SCqO7MHC0Ey1Epiu+k5XafMvbRaFXCgdBxBOIgw202/TyNBtmuHibJRYba6LjuAsgjjCA3UxwWPCEwanRCJ7bYd/eHspFootdwhdvqeJsyegyaCSVK271US2E9BNp8otbTd9STlB89NJvRFmJREJ4K7e0El2Op6NiY1x7GQlpoawihgq+lIZF2s1xWdu9GAcO6Yp6rcE4OcwGDGR7nhUglYRjL32pbOQdMH61VCzgiFc8eUs+2cTIkuoI2MmH20D9TLzEzuTafgwtp0dNXO5FlhsOvSh23zzBPTBfDm4ny0BBBWAInWpSmVmSDzqrEko0TpBwct9W+xGgKL41hWoMeJB/LQsa/JlOYkVDrmAHlpwnF0M6of2gdIC7vUvDUNk1HMbwVRbmo+jA3tOlaYByTYbRJdiG1E3UFFg2IvLxqm67qR1yg71kLflDXvwhHhsxtbSycw2xWffXiPP+/NmFTiGyX21t9m99RbWDCZ1Fy5dAlJeXyz6aOfz48Mzli6aPTp8rMXz/585UVeun7z4WW418Kll0/w9Pk1/t5JTHdh2otpElMl1PX8eP06fdus9ObhQ2g4VBrVxpvvdl/Ovmgacev2Dnf+2GXnxi6zGw/KMJrNJUXPS6fXmCaNw+d8GNV8KHXgp87xyg83tVQCALPvLlvCVUK/3tnh9t3mkw8+/u3Tx3kNu/H6C7z6063lnLBU4dd98NHzF9Z47eKpE4/7Hngw+LHHzveX3zmAyBv/xsvpP+li/lm3bxkuAAAAAElFTkSuQmCC" />'
. $this->translator->getLocale() . ($this->untranslated ? ' <b>(' . count(array_unique($this->untranslated, SORT_REGULAR)) . ' errors)</b>' : '')
Expand All @@ -92,7 +92,7 @@ public function getTab()
*
* @return string
*/
public function getPanel()
public function getPanel(): ?string
{
$h = 'htmlSpecialChars';

Expand Down
20 changes: 11 additions & 9 deletions src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,23 +121,25 @@ public function injectPsrLogger(LoggerInterface $logger = NULL)
* Translates the given string.
*
* @param string|\Kdyby\Translation\Phrase|mixed $message The message id
* @param int|array|NULL $count The number to use to find the indice of the message
* @param string|array|NULL $parameters An array of parameters for the message
* @param string|NULL $domain The domain for the message
* @param string|NULL $locale The locale
* @throws \InvalidArgumentException
* @return string|\Nette\Utils\IHtmlString|\Latte\Runtime\IHtmlString
*/
public function translate($message, $count = NULL, $parameters = [], $domain = NULL, $locale = NULL)
public function translate($message, ...$parameters): string
{
if ($message instanceof Phrase) {
return $message->translate($this);
}

$count = isset($parameters[0]) ? $parameters[0] : NULL;
$params = isset($parameters[1]) ? $parameters[1] : [];
$domain = isset($parameters[2]) ? $parameters[2] : NULL;
$locale = isset($parameters[3]) ? $parameters[3] : NULL;

if (is_array($count)) {
$locale = ($domain !== NULL) ? (string) $domain : NULL;
$domain = ($parameters !== NULL && !empty($parameters)) ? (string) $parameters : NULL;
$parameters = $count;
$params = $count;
$count = NULL;
}

Expand Down Expand Up @@ -168,16 +170,16 @@ public function translate($message, $count = NULL, $parameters = [], $domain = N
}

$tmp = [];
foreach ($parameters as $key => $val) {
foreach ($params as $key => $val) {
$tmp['%' . trim($key, '%') . '%'] = $val;
}
$parameters = $tmp;
$params = $tmp;

if ($count !== NULL && is_scalar($count)) {
return $this->transChoice($message, $count, $parameters + ['%count%' => $count], $domain, $locale);
return $this->transChoice($message, $count, $params + ['%count%' => $count], $domain, $locale);
}

return $this->trans($message, $parameters, $domain, $locale);
return $this->trans($message, $params, $domain, $locale);
}

/**
Expand Down