Skip to content

Commit 5ff36c6

Browse files
themsaidtaylorotwell
authored andcommitted
[5.4] Replace symfony's translator (#15563)
* replace symfony's translator * update helper methods
1 parent 3dcfcb1 commit 5ff36c6

File tree

14 files changed

+240
-47
lines changed

14 files changed

+240
-47
lines changed

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
"symfony/http-kernel": "3.2.*",
3737
"symfony/process": "3.2.*",
3838
"symfony/routing": "3.2.*",
39-
"symfony/translation": "3.2.*",
4039
"symfony/var-dumper": "3.2.*",
4140
"vlucas/phpdotenv": "~2.2"
4241
},
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Illuminate\Contracts\Translation;
4+
5+
interface Translator
6+
{
7+
/**
8+
* Get the translation for a given key.
9+
*
10+
* @param string $key
11+
* @param array $replace
12+
* @param string $locale
13+
* @return mixed
14+
*/
15+
public function trans($key, array $replace = [], $locale = null);
16+
17+
/**
18+
* Get a translation according to an integer value.
19+
*
20+
* @param string $key
21+
* @param int|array|\Countable $number
22+
* @param array $replace
23+
* @param string $locale
24+
* @return string
25+
*/
26+
public function transChoice($key, $number, array $replace = [], $locale = null);
27+
28+
/**
29+
* Set the default locale.
30+
*
31+
* @param string $locale
32+
* @return void
33+
*/
34+
public function setLocale($locale);
35+
36+
/**
37+
* Get the default locale being used.
38+
*
39+
* @return string
40+
*/
41+
public function getLocale();
42+
}

src/Illuminate/Foundation/Application.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1089,7 +1089,7 @@ public function registerCoreContainerAliases()
10891089
'filesystem.disk' => ['Illuminate\Contracts\Filesystem\Filesystem'],
10901090
'filesystem.cloud' => ['Illuminate\Contracts\Filesystem\Cloud'],
10911091
'hash' => ['Illuminate\Contracts\Hashing\Hasher'],
1092-
'translator' => ['Illuminate\Translation\Translator', 'Symfony\Component\Translation\TranslatorInterface'],
1092+
'translator' => ['Illuminate\Translation\Translator', 'Illuminate\Contracts\Translation\Translator'],
10931093
'log' => ['Illuminate\Log\Writer', 'Illuminate\Contracts\Logging\Log', 'Psr\Log\LoggerInterface'],
10941094
'mailer' => ['Illuminate\Mail\Mailer', 'Illuminate\Contracts\Mail\Mailer', 'Illuminate\Contracts\Mail\MailQueue'],
10951095
'auth.password' => ['Illuminate\Auth\Passwords\PasswordBrokerManager', 'Illuminate\Contracts\Auth\PasswordBrokerFactory'],

src/Illuminate/Foundation/helpers.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -762,18 +762,17 @@ function storage_path($path = '')
762762
* Translate the given message.
763763
*
764764
* @param string $id
765-
* @param array $parameters
766-
* @param string $domain
765+
* @param array $replace
767766
* @param string $locale
768-
* @return \Symfony\Component\Translation\TranslatorInterface|string
767+
* @return \Illuminate\Contracts\Translation\Translator|string
769768
*/
770-
function trans($id = null, $parameters = [], $domain = 'messages', $locale = null)
769+
function trans($id = null, $replace = [], $locale = null)
771770
{
772771
if (is_null($id)) {
773772
return app('translator');
774773
}
775774

776-
return app('translator')->trans($id, $parameters, $domain, $locale);
775+
return app('translator')->trans($id, $replace, $locale);
777776
}
778777
}
779778

@@ -783,14 +782,13 @@ function trans($id = null, $parameters = [], $domain = 'messages', $locale = nul
783782
*
784783
* @param string $id
785784
* @param int|array|\Countable $number
786-
* @param array $parameters
787-
* @param string $domain
785+
* @param array $replace
788786
* @param string $locale
789787
* @return string
790788
*/
791-
function trans_choice($id, $number, array $parameters = [], $domain = 'messages', $locale = null)
789+
function trans_choice($id, $number, array $replace = [], $locale = null)
792790
{
793-
return app('translator')->transChoice($id, $number, $parameters, $domain, $locale);
791+
return app('translator')->transChoice($id, $number, $replace, $locale);
794792
}
795793
}
796794

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace Illuminate\Translation;
4+
5+
use Illuminate\Support\Str;
6+
use Illuminate\Support\Collection;
7+
8+
class MessageSelector
9+
{
10+
/**
11+
* Select a proper translation string based on the given number.
12+
*
13+
* @param string $line
14+
* @param int $number
15+
* @return mixed
16+
*/
17+
public function choose($line, $number)
18+
{
19+
$parts = explode('|', $line);
20+
21+
if (($value = $this->extract($parts, $number)) !== null) {
22+
return trim($value);
23+
}
24+
25+
$parts = $this->stripConditions($parts);
26+
27+
return count($parts) == 1 || $number == 1
28+
? $parts[0] : $parts[1];
29+
}
30+
31+
/**
32+
* Extract a translation string using inline conditions.
33+
*
34+
* @param array $parts
35+
* @param int $number
36+
* @return mixed
37+
*/
38+
private function extract($parts, $number)
39+
{
40+
foreach ($parts as $part) {
41+
if (($line = $this->extractFromString($part, $number)) !== null) {
42+
return $line;
43+
}
44+
}
45+
}
46+
47+
/**
48+
* Get the translation string if the condition matches.
49+
*
50+
* @param string $part
51+
* @param int $number
52+
* @return mixed
53+
*/
54+
private function extractFromString($part, $number)
55+
{
56+
preg_match('/^[\{\[]([^\[\]\{\}]*)[\}\]](.*)/s', $part, $matches);
57+
58+
if (count($matches) != 3) {
59+
return;
60+
}
61+
62+
$condition = $matches[1];
63+
64+
$value = $matches[2];
65+
66+
if (Str::contains($condition, ',')) {
67+
list($from, $to) = explode(',', $condition, 2);
68+
69+
if ($to == '*' && $number >= $from) {
70+
return $value;
71+
} elseif ($from == '*' && $number <= $to) {
72+
return $value;
73+
} elseif ($number >= $from && $number <= $to) {
74+
return $value;
75+
}
76+
}
77+
78+
return $condition == $number ? $value : null;
79+
}
80+
81+
/**
82+
* Strip the inline condition.
83+
*
84+
* @param array $parts
85+
* @return array
86+
*/
87+
private function stripConditions($parts)
88+
{
89+
return Collection::make($parts)->map(function ($part) {
90+
return preg_replace('/^[\{\[]([^\[\]\{\}]*)[\}\]]/', '', $part);
91+
})->toArray();
92+
}
93+
}

src/Illuminate/Translation/Translator.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77
use Illuminate\Support\Collection;
88
use Illuminate\Support\Traits\Macroable;
99
use Illuminate\Support\NamespacedItemResolver;
10-
use Symfony\Component\Translation\MessageSelector;
11-
use Symfony\Component\Translation\TranslatorInterface;
10+
use Illuminate\Contracts\Translation\Translator as TranslatorContract;
1211

13-
class Translator extends NamespacedItemResolver implements TranslatorInterface
12+
class Translator extends NamespacedItemResolver implements TranslatorContract
1413
{
1514
use Macroable;
1615

@@ -45,7 +44,7 @@ class Translator extends NamespacedItemResolver implements TranslatorInterface
4544
/**
4645
* The message selector.
4746
*
48-
* @var \Symfony\Component\Translation\MessageSelector
47+
* @var \Illuminate\Translation\MessageSelector
4948
*/
5049
protected $selector;
5150

@@ -225,30 +224,28 @@ public function choice($key, $number, array $replace = [], $locale = null)
225224
/**
226225
* Get the translation for a given key.
227226
*
228-
* @param string $id
229-
* @param array $parameters
230-
* @param string $domain
227+
* @param string $key
228+
* @param array $replace
231229
* @param string $locale
232230
* @return string|array|null
233231
*/
234-
public function trans($id, array $parameters = [], $domain = 'messages', $locale = null)
232+
public function trans($key, array $replace = [], $locale = null)
235233
{
236-
return $this->get($id, $parameters, $locale);
234+
return $this->get($key, $replace, $locale);
237235
}
238236

239237
/**
240238
* Get a translation according to an integer value.
241239
*
242-
* @param string $id
240+
* @param string $key
243241
* @param int|array|\Countable $number
244-
* @param array $parameters
245-
* @param string $domain
242+
* @param array $replace
246243
* @param string $locale
247244
* @return string
248245
*/
249-
public function transChoice($id, $number, array $parameters = [], $domain = 'messages', $locale = null)
246+
public function transChoice($key, $number, array $replace = [], $locale = null)
250247
{
251-
return $this->choice($id, $number, $parameters, $locale);
248+
return $this->choice($key, $number, $replace, $locale);
252249
}
253250

254251
/**
@@ -329,7 +326,7 @@ protected function parseLocale($locale)
329326
/**
330327
* Get the message selector instance.
331328
*
332-
* @return \Symfony\Component\Translation\MessageSelector
329+
* @return \Illuminate\Translation\MessageSelector
333330
*/
334331
public function getSelector()
335332
{
@@ -343,7 +340,7 @@ public function getSelector()
343340
/**
344341
* Set the message selector instance.
345342
*
346-
* @param \Symfony\Component\Translation\MessageSelector $selector
343+
* @param \Illuminate\Translation\MessageSelector $selector
347344
* @return void
348345
*/
349346
public function setSelector(MessageSelector $selector)

src/Illuminate/Translation/composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
"require": {
1717
"php": ">=5.6.4",
1818
"illuminate/filesystem": "5.4.*",
19-
"illuminate/support": "5.4.*",
20-
"symfony/translation": "3.2.*"
19+
"illuminate/support": "5.4.*"
2120
},
2221
"autoload": {
2322
"psr-4": {

src/Illuminate/Validation/Factory.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
use Closure;
66
use Illuminate\Support\Str;
77
use Illuminate\Contracts\Container\Container;
8-
use Symfony\Component\Translation\TranslatorInterface;
8+
use Illuminate\Contracts\Translation\Translator;
99
use Illuminate\Contracts\Validation\Factory as FactoryContract;
1010

1111
class Factory implements FactoryContract
1212
{
1313
/**
1414
* The Translator implementation.
1515
*
16-
* @var \Symfony\Component\Translation\TranslatorInterface
16+
* @var \Illuminate\Contracts\Translation\Translator
1717
*/
1818
protected $translator;
1919

@@ -69,11 +69,11 @@ class Factory implements FactoryContract
6969
/**
7070
* Create a new Validator factory instance.
7171
*
72-
* @param \Symfony\Component\Translation\TranslatorInterface $translator
72+
* @param \Illuminate\Contracts\Translation\Translator $translator
7373
* @param \Illuminate\Contracts\Container\Container $container
7474
* @return void
7575
*/
76-
public function __construct(TranslatorInterface $translator, Container $container = null)
76+
public function __construct(Translator $translator, Container $container = null)
7777
{
7878
$this->container = $container;
7979
$this->translator = $translator;
@@ -227,7 +227,7 @@ public function resolver(Closure $resolver)
227227
/**
228228
* Get the Translator implementation.
229229
*
230-
* @return \Symfony\Component\Translation\TranslatorInterface
230+
* @return \Illuminate\Contracts\Translation\Translator
231231
*/
232232
public function getTranslator()
233233
{

src/Illuminate/Validation/Validator.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use Illuminate\Support\MessageBag;
1818
use Illuminate\Contracts\Container\Container;
1919
use Symfony\Component\HttpFoundation\File\File;
20-
use Symfony\Component\Translation\TranslatorInterface;
20+
use Illuminate\Contracts\Translation\Translator;
2121
use Symfony\Component\HttpFoundation\File\UploadedFile;
2222
use Illuminate\Contracts\Validation\Validator as ValidatorContract;
2323

@@ -26,7 +26,7 @@ class Validator implements ValidatorContract
2626
/**
2727
* The Translator implementation.
2828
*
29-
* @var \Symfony\Component\Translation\TranslatorInterface
29+
* @var \Illuminate\Contracts\Translation\Translator
3030
*/
3131
protected $translator;
3232

@@ -190,14 +190,14 @@ class Validator implements ValidatorContract
190190
/**
191191
* Create a new Validator instance.
192192
*
193-
* @param \Symfony\Component\Translation\TranslatorInterface $translator
193+
* @param \Illuminate\Contracts\Translation\Translator $translator
194194
* @param array $data
195195
* @param array $rules
196196
* @param array $messages
197197
* @param array $customAttributes
198198
* @return void
199199
*/
200-
public function __construct(TranslatorInterface $translator, array $data, array $rules, array $messages = [], array $customAttributes = [])
200+
public function __construct(Translator $translator, array $data, array $rules, array $messages = [], array $customAttributes = [])
201201
{
202202
$this->initialRules = $rules;
203203
$this->translator = $translator;
@@ -3126,7 +3126,7 @@ public function setPresenceVerifier(PresenceVerifierInterface $presenceVerifier)
31263126
/**
31273127
* Get the Translator implementation.
31283128
*
3129-
* @return \Symfony\Component\Translation\TranslatorInterface
3129+
* @return \Illuminate\Contracts\Translation\Translator
31303130
*/
31313131
public function getTranslator()
31323132
{
@@ -3136,10 +3136,10 @@ public function getTranslator()
31363136
/**
31373137
* Set the Translator implementation.
31383138
*
3139-
* @param \Symfony\Component\Translation\TranslatorInterface $translator
3139+
* @param \Illuminate\Contracts\Translation\Translator $translator
31403140
* @return void
31413141
*/
3142-
public function setTranslator(TranslatorInterface $translator)
3142+
public function setTranslator(Translator $translator)
31433143
{
31443144
$this->translator = $translator;
31453145
}

src/Illuminate/Validation/composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
"illuminate/container": "5.4.*",
1919
"illuminate/contracts": "5.4.*",
2020
"illuminate/support": "5.4.*",
21-
"symfony/http-foundation": "3.2.*",
22-
"symfony/translation": "3.2.*"
21+
"symfony/http-foundation": "3.2.*"
2322
},
2423
"autoload": {
2524
"psr-4": {

0 commit comments

Comments
 (0)