Skip to content

Commit 3f8eb78

Browse files
authoredSep 2, 2019
Merge pull request #1 from elcheco/nette3
Upgrade to Nette3
2 parents a30271c + 1851a0f commit 3f8eb78

9 files changed

+37
-63
lines changed
 

‎composer.json

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "elcheco/translator",
3-
"description": "Translation system using neon files, supports plurals and string replacements, fallback language. Extension to Nette Framework ^2.4",
3+
"description": "Translation system using neon files, supports plurals and string replacements, fallback language. Extension to Nette Framework ^3.0",
44
"type": "library",
55
"license": ["BSD-3-Clause"],
66
"authors": [
@@ -12,22 +12,19 @@
1212
"minimum-stability": "dev",
1313
"prefer-stable": true,
1414
"require": {
15-
"php": ">=7.2",
15+
"php": ">=7.3",
1616
"ext-intl": "*",
1717
"ext-tokenizer": "*",
18-
"nette/di": "^2.4",
18+
"nette/di": "^3.0",
1919
"nette/neon": "^3.0",
20-
"nette/safe-stream": "^2.3",
21-
"nette/utils": "^2.5",
20+
"nette/safe-stream": "^2.4",
21+
"nette/utils": "^3.0",
2222
"psr/log": "^1.0"
2323
},
2424
"require-dev": {
2525
"nette/tester": "^2.0",
26-
"mockery/mockery": "^1.0@dev",
27-
"phpstan/phpstan": "^0.9.0@dev"
28-
},
29-
"suggest": {
30-
"monolog/monolog": "To log translation errors."
26+
"mockery/mockery": "^1.2",
27+
"phpstan/phpstan": "^0.11.8"
3128
},
3229
"autoload": {
3330
"psr-4": {

‎src/Translator/Dictionary.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function get(string $message)
3838
abstract protected function lazyLoad();
3939

4040

41-
protected function isReady()
41+
protected function isReady(): bool
4242
{
4343
return is_array($this->messages);
4444
}

‎src/Translator/DictionaryFactoryInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313
interface DictionaryFactoryInterface
1414
{
1515

16-
public function create(string $locale, ?string $fallbackLocale): DictionaryInterface;
16+
public function create(string $locale, ?string $fallbackLocale = null): DictionaryInterface;
1717

1818
}

‎src/Translator/Extension.php

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
namespace ElCheco\Translator;
1111

12-
1312
use Nette\DI\CompilerExtension;
1413
use Nette\DI\Helpers;
1514
use ElCheco\Translator\NeonDictionary\NeonDictionaryFactory;

‎src/Translator/NeonDictionary/NeonDictionary.php

+10-12
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,11 @@
99

1010
namespace ElCheco\Translator\NeonDictionary;
1111

12-
13-
use function is_array;
1412
use Nette\Neon\Neon;
1513
use ElCheco\Translator\Dictionary;
1614

1715
final class NeonDictionary extends Dictionary
1816
{
19-
2017
/**
2118
* @var string
2219
*/
@@ -35,19 +32,16 @@ final class NeonDictionary extends Dictionary
3532

3633
public function __construct(string $filename, string $cacheFilename, ?string $fallbackFilename = null)
3734
{
38-
if (!is_file($filename)) {
39-
35+
if (!\is_file($filename)) {
4036
throw NeonDictionaryException::fileNotFound($filename);
4137
}
4238

4339
if ($fallbackFilename && !\is_file($fallbackFilename)) {
44-
4540
throw NeonDictionaryException::fileNotFound($fallbackFilename);
4641
}
4742

4843
$this->filename = $filename;
4944
$this->cacheFilename = $cacheFilename;
50-
5145
$this->fallbackFilename = $fallbackFilename;
5246
}
5347

@@ -64,14 +58,18 @@ protected function lazyLoad()
6458
} else {
6559

6660
// load translations from neon file
67-
$fallbackDecoded = Neon::decode(file_get_contents($this->fallbackFilename));
68-
$fallbackTranslations = \is_array($fallbackDecoded) ? $fallbackDecoded: [];
61+
if ($this->fallbackFilename) {
62+
$fallbackDecoded = Neon::decode(\file_get_contents($this->fallbackFilename));
63+
$fallbackTranslations = \is_array($fallbackDecoded) ? $fallbackDecoded : [];
64+
}
6965

7066
// load translations from neon file
71-
$decoded = Neon::decode(file_get_contents($this->filename));
67+
$decoded = Neon::decode(\file_get_contents($this->filename));
7268
$translations = \is_array($decoded) ? $decoded: [];
7369

74-
$translations = \array_merge($fallbackTranslations, $translations);
70+
if ($this->fallbackFilename) {
71+
$translations = \array_merge($fallbackTranslations, $translations);
72+
}
7573

7674
$translations = $this->parse($translations);
7775

@@ -84,7 +82,7 @@ protected function lazyLoad()
8482
}
8583
}
8684

87-
protected function parse(array $translations)
85+
protected function parse(array $translations): array
8886
{
8987
foreach ($translations as &$translation) {
9088
if (\is_array($translation)) {

‎src/Translator/NeonDictionary/NeonDictionaryFactory.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
namespace ElCheco\Translator\NeonDictionary;
1111

12-
1312
use ElCheco\Translator\DictionaryFactoryInterface;
1413
use ElCheco\Translator\DictionaryInterface;
1514

@@ -29,14 +28,14 @@ final class NeonDictionaryFactory implements DictionaryFactoryInterface
2928

3029
public function __construct(string $directory, string $cacheDir, int $cacheDirMode = 0775)
3130
{
32-
if (!is_dir($directory)) {
31+
if (!\is_dir($directory)) {
3332

3433
throw NeonDictionaryException::translationDirNotFound($directory);
3534
}
3635

3736
$this->directory = $directory;
3837

39-
if (!is_dir($cacheDir) && @!mkdir($cacheDir, $cacheDirMode, true) || !is_writable($cacheDir)) {
38+
if (!\is_dir($cacheDir) && @!\mkdir($cacheDir, $cacheDirMode, true) || !\is_writable($cacheDir)) {
4039

4140
throw NeonDictionaryException::cacheDirIsNotWritable($cacheDir);
4241
}
@@ -45,7 +44,7 @@ public function __construct(string $directory, string $cacheDir, int $cacheDirMo
4544
}
4645

4746

48-
public function create(string $locale, ?string $fallbackLocale): DictionaryInterface
47+
public function create(string $locale, ?string $fallbackLocale = null): DictionaryInterface
4948
{
5049
$sourceFile = "$this->directory/$locale.neon";
5150
$cacheFile = "$this->cacheDir/$locale.php";

‎src/Translator/Translator.php

+15-30
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,11 @@
99

1010
namespace ElCheco\Translator;
1111

12-
1312
use NumberFormatter;
1413
use Psr\Log\LoggerInterface;
15-
use function array_key_exists;
16-
use function array_shift;
17-
use function end;
18-
use function func_get_args;
19-
use function gettype;
20-
use function is_numeric;
21-
use function is_object;
22-
use function key;
23-
use function method_exists;
24-
use function sprintf;
2514

2615
final class Translator implements TranslatorInterface
2716
{
28-
29-
3017
private const ZERO_INDEX = -1;
3118

3219
/**
@@ -46,9 +33,9 @@ final class Translator implements TranslatorInterface
4633
/**
4734
* fallback locale
4835
*
49-
* @var string
36+
* @var null|string
5037
*/
51-
private $fallBackLocale = 'en_US';
38+
private $fallBackLocale;
5239

5340
/**
5441
* @var DictionaryInterface|null
@@ -100,21 +87,25 @@ public function setFallbackLocale(string $locale): TranslatorInterface
10087
}
10188

10289

103-
public function translate($message, $count = null)
90+
public function translate($message, ...$parameters): string
10491
{
10592
// avoid processing for empty values
10693
if ($message === null || $message === '') {
10794
return '';
10895
}
10996

97+
if (!isset($parameters[0])) {
98+
$parameters[0] = null;
99+
}
100+
110101
// convert to string
111102
if (\is_object($message) && \method_exists($message, '__toString')) {
112103
$message = (string) $message;
113104
}
114105

115106
// numbers are formatted using locale settings (count parameter is used to define decimals)
116107
if (\is_numeric($message)) {
117-
return $this->formatNumber($message, (int) $count);
108+
return $this->formatNumber($message, (int) $parameters['count']);
118109
}
119110

120111
// check message to be string
@@ -139,15 +130,15 @@ public function translate($message, $count = null)
139130
// process plural
140131
if (\is_array($translation)) {
141132

142-
if ($count === null) {
133+
if ((isset($parameters[0]) && $parameters[0] === null)) {
143134
$this->warn('Multiple plural forms are available (message: %s), but the $count is null.', $message);
144135

145136
// fallback to highest
146-
$count = \max(array_keys($translation));
137+
$parameters[0] = \max(\array_keys($translation));
147138
}
148139

149140
$t = new Translation($translation);
150-
$result = $t->get($count);
141+
$result = $t->get($parameters[0]);
151142

152143
}
153144

@@ -157,24 +148,18 @@ public function translate($message, $count = null)
157148
}
158149
}
159150

160-
// process parameters
161-
$args = \func_get_args();
162-
163-
// remove message
164-
\array_shift($args);
165-
166151
// remove count if not provided or explicitly set to null
167-
if ($count === null) {
168-
\array_shift($args);
152+
if ($parameters[0] === null) {
153+
\array_shift($parameters);
169154
}
170155

171-
if (\count($args)) {
156+
if (\count($parameters)) {
172157

173158
// preserve some nette placeholders
174159
$template = \str_replace(['%label', '%name', '%value'], ['%%label', '%%name', '%%value'], $result);
175160

176161
// apply parameters
177-
$result = \vsprintf($template, $args);
162+
$result = \vsprintf($template, $parameters);
178163
}
179164

180165
return $result;

‎src/Translator/TranslatorException.php

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace ElCheco\Translator;
44

5-
65
use Exception;
76

87
class TranslatorException extends Exception

‎src/Translator/TranslatorInterface.php

-3
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,9 @@
99

1010
namespace ElCheco\Translator;
1111

12-
1312
use Nette\Localization\ITranslator;
1413

1514
interface TranslatorInterface extends ITranslator
1615
{
17-
1816
public function setLocale(string $locale): TranslatorInterface;
19-
2017
}

0 commit comments

Comments
 (0)
Please sign in to comment.