Skip to content

Commit

Permalink
[5.4] Replace symfony's translator (#15563)
Browse files Browse the repository at this point in the history
* replace symfony's translator

*     update helper methods
  • Loading branch information
themsaid authored and taylorotwell committed Sep 23, 2016
1 parent 3dcfcb1 commit 5ff36c6
Show file tree
Hide file tree
Showing 14 changed files with 240 additions and 47 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
"symfony/http-kernel": "3.2.*",
"symfony/process": "3.2.*",
"symfony/routing": "3.2.*",
"symfony/translation": "3.2.*",
"symfony/var-dumper": "3.2.*",
"vlucas/phpdotenv": "~2.2"
},
Expand Down
42 changes: 42 additions & 0 deletions src/Illuminate/Contracts/Translation/Translator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Illuminate\Contracts\Translation;

interface Translator
{
/**
* Get the translation for a given key.
*
* @param string $key
* @param array $replace
* @param string $locale
* @return mixed
*/
public function trans($key, array $replace = [], $locale = null);

/**
* Get a translation according to an integer value.
*
* @param string $key
* @param int|array|\Countable $number
* @param array $replace
* @param string $locale
* @return string
*/
public function transChoice($key, $number, array $replace = [], $locale = null);

/**
* Set the default locale.
*
* @param string $locale
* @return void
*/
public function setLocale($locale);

/**
* Get the default locale being used.
*
* @return string
*/
public function getLocale();
}
2 changes: 1 addition & 1 deletion src/Illuminate/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -1089,7 +1089,7 @@ public function registerCoreContainerAliases()
'filesystem.disk' => ['Illuminate\Contracts\Filesystem\Filesystem'],
'filesystem.cloud' => ['Illuminate\Contracts\Filesystem\Cloud'],
'hash' => ['Illuminate\Contracts\Hashing\Hasher'],
'translator' => ['Illuminate\Translation\Translator', 'Symfony\Component\Translation\TranslatorInterface'],
'translator' => ['Illuminate\Translation\Translator', 'Illuminate\Contracts\Translation\Translator'],
'log' => ['Illuminate\Log\Writer', 'Illuminate\Contracts\Logging\Log', 'Psr\Log\LoggerInterface'],
'mailer' => ['Illuminate\Mail\Mailer', 'Illuminate\Contracts\Mail\Mailer', 'Illuminate\Contracts\Mail\MailQueue'],
'auth.password' => ['Illuminate\Auth\Passwords\PasswordBrokerManager', 'Illuminate\Contracts\Auth\PasswordBrokerFactory'],
Expand Down
16 changes: 7 additions & 9 deletions src/Illuminate/Foundation/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -762,18 +762,17 @@ function storage_path($path = '')
* Translate the given message.
*
* @param string $id
* @param array $parameters
* @param string $domain
* @param array $replace
* @param string $locale
* @return \Symfony\Component\Translation\TranslatorInterface|string
* @return \Illuminate\Contracts\Translation\Translator|string
*/
function trans($id = null, $parameters = [], $domain = 'messages', $locale = null)
function trans($id = null, $replace = [], $locale = null)
{
if (is_null($id)) {
return app('translator');
}

return app('translator')->trans($id, $parameters, $domain, $locale);
return app('translator')->trans($id, $replace, $locale);
}
}

Expand All @@ -783,14 +782,13 @@ function trans($id = null, $parameters = [], $domain = 'messages', $locale = nul
*
* @param string $id
* @param int|array|\Countable $number
* @param array $parameters
* @param string $domain
* @param array $replace
* @param string $locale
* @return string
*/
function trans_choice($id, $number, array $parameters = [], $domain = 'messages', $locale = null)
function trans_choice($id, $number, array $replace = [], $locale = null)
{
return app('translator')->transChoice($id, $number, $parameters, $domain, $locale);
return app('translator')->transChoice($id, $number, $replace, $locale);
}
}

Expand Down
93 changes: 93 additions & 0 deletions src/Illuminate/Translation/MessageSelector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace Illuminate\Translation;

use Illuminate\Support\Str;
use Illuminate\Support\Collection;

class MessageSelector
{
/**
* Select a proper translation string based on the given number.
*
* @param string $line
* @param int $number
* @return mixed
*/
public function choose($line, $number)
{
$parts = explode('|', $line);

if (($value = $this->extract($parts, $number)) !== null) {
return trim($value);
}

$parts = $this->stripConditions($parts);

return count($parts) == 1 || $number == 1
? $parts[0] : $parts[1];
}

/**
* Extract a translation string using inline conditions.
*
* @param array $parts
* @param int $number
* @return mixed
*/
private function extract($parts, $number)
{
foreach ($parts as $part) {
if (($line = $this->extractFromString($part, $number)) !== null) {
return $line;
}
}
}

/**
* Get the translation string if the condition matches.
*
* @param string $part
* @param int $number
* @return mixed
*/
private function extractFromString($part, $number)
{
preg_match('/^[\{\[]([^\[\]\{\}]*)[\}\]](.*)/s', $part, $matches);

if (count($matches) != 3) {
return;
}

$condition = $matches[1];

$value = $matches[2];

if (Str::contains($condition, ',')) {
list($from, $to) = explode(',', $condition, 2);

if ($to == '*' && $number >= $from) {
return $value;
} elseif ($from == '*' && $number <= $to) {
return $value;
} elseif ($number >= $from && $number <= $to) {
return $value;
}
}

return $condition == $number ? $value : null;
}

/**
* Strip the inline condition.
*
* @param array $parts
* @return array
*/
private function stripConditions($parts)
{
return Collection::make($parts)->map(function ($part) {
return preg_replace('/^[\{\[]([^\[\]\{\}]*)[\}\]]/', '', $part);
})->toArray();
}
}
29 changes: 13 additions & 16 deletions src/Illuminate/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Support\NamespacedItemResolver;
use Symfony\Component\Translation\MessageSelector;
use Symfony\Component\Translation\TranslatorInterface;
use Illuminate\Contracts\Translation\Translator as TranslatorContract;

class Translator extends NamespacedItemResolver implements TranslatorInterface
class Translator extends NamespacedItemResolver implements TranslatorContract
{
use Macroable;

Expand Down Expand Up @@ -45,7 +44,7 @@ class Translator extends NamespacedItemResolver implements TranslatorInterface
/**
* The message selector.
*
* @var \Symfony\Component\Translation\MessageSelector
* @var \Illuminate\Translation\MessageSelector
*/
protected $selector;

Expand Down Expand Up @@ -225,30 +224,28 @@ public function choice($key, $number, array $replace = [], $locale = null)
/**
* Get the translation for a given key.
*
* @param string $id
* @param array $parameters
* @param string $domain
* @param string $key
* @param array $replace
* @param string $locale
* @return string|array|null
*/
public function trans($id, array $parameters = [], $domain = 'messages', $locale = null)
public function trans($key, array $replace = [], $locale = null)
{
return $this->get($id, $parameters, $locale);
return $this->get($key, $replace, $locale);
}

/**
* Get a translation according to an integer value.
*
* @param string $id
* @param string $key
* @param int|array|\Countable $number
* @param array $parameters
* @param string $domain
* @param array $replace
* @param string $locale
* @return string
*/
public function transChoice($id, $number, array $parameters = [], $domain = 'messages', $locale = null)
public function transChoice($key, $number, array $replace = [], $locale = null)
{
return $this->choice($id, $number, $parameters, $locale);
return $this->choice($key, $number, $replace, $locale);
}

/**
Expand Down Expand Up @@ -329,7 +326,7 @@ protected function parseLocale($locale)
/**
* Get the message selector instance.
*
* @return \Symfony\Component\Translation\MessageSelector
* @return \Illuminate\Translation\MessageSelector
*/
public function getSelector()
{
Expand All @@ -343,7 +340,7 @@ public function getSelector()
/**
* Set the message selector instance.
*
* @param \Symfony\Component\Translation\MessageSelector $selector
* @param \Illuminate\Translation\MessageSelector $selector
* @return void
*/
public function setSelector(MessageSelector $selector)
Expand Down
3 changes: 1 addition & 2 deletions src/Illuminate/Translation/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
"require": {
"php": ">=5.6.4",
"illuminate/filesystem": "5.4.*",
"illuminate/support": "5.4.*",
"symfony/translation": "3.2.*"
"illuminate/support": "5.4.*"
},
"autoload": {
"psr-4": {
Expand Down
10 changes: 5 additions & 5 deletions src/Illuminate/Validation/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
use Closure;
use Illuminate\Support\Str;
use Illuminate\Contracts\Container\Container;
use Symfony\Component\Translation\TranslatorInterface;
use Illuminate\Contracts\Translation\Translator;
use Illuminate\Contracts\Validation\Factory as FactoryContract;

class Factory implements FactoryContract
{
/**
* The Translator implementation.
*
* @var \Symfony\Component\Translation\TranslatorInterface
* @var \Illuminate\Contracts\Translation\Translator
*/
protected $translator;

Expand Down Expand Up @@ -69,11 +69,11 @@ class Factory implements FactoryContract
/**
* Create a new Validator factory instance.
*
* @param \Symfony\Component\Translation\TranslatorInterface $translator
* @param \Illuminate\Contracts\Translation\Translator $translator
* @param \Illuminate\Contracts\Container\Container $container
* @return void
*/
public function __construct(TranslatorInterface $translator, Container $container = null)
public function __construct(Translator $translator, Container $container = null)
{
$this->container = $container;
$this->translator = $translator;
Expand Down Expand Up @@ -227,7 +227,7 @@ public function resolver(Closure $resolver)
/**
* Get the Translator implementation.
*
* @return \Symfony\Component\Translation\TranslatorInterface
* @return \Illuminate\Contracts\Translation\Translator
*/
public function getTranslator()
{
Expand Down
14 changes: 7 additions & 7 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Illuminate\Support\MessageBag;
use Illuminate\Contracts\Container\Container;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Translation\TranslatorInterface;
use Illuminate\Contracts\Translation\Translator;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Illuminate\Contracts\Validation\Validator as ValidatorContract;

Expand All @@ -26,7 +26,7 @@ class Validator implements ValidatorContract
/**
* The Translator implementation.
*
* @var \Symfony\Component\Translation\TranslatorInterface
* @var \Illuminate\Contracts\Translation\Translator
*/
protected $translator;

Expand Down Expand Up @@ -190,14 +190,14 @@ class Validator implements ValidatorContract
/**
* Create a new Validator instance.
*
* @param \Symfony\Component\Translation\TranslatorInterface $translator
* @param \Illuminate\Contracts\Translation\Translator $translator
* @param array $data
* @param array $rules
* @param array $messages
* @param array $customAttributes
* @return void
*/
public function __construct(TranslatorInterface $translator, array $data, array $rules, array $messages = [], array $customAttributes = [])
public function __construct(Translator $translator, array $data, array $rules, array $messages = [], array $customAttributes = [])
{
$this->initialRules = $rules;
$this->translator = $translator;
Expand Down Expand Up @@ -3126,7 +3126,7 @@ public function setPresenceVerifier(PresenceVerifierInterface $presenceVerifier)
/**
* Get the Translator implementation.
*
* @return \Symfony\Component\Translation\TranslatorInterface
* @return \Illuminate\Contracts\Translation\Translator
*/
public function getTranslator()
{
Expand All @@ -3136,10 +3136,10 @@ public function getTranslator()
/**
* Set the Translator implementation.
*
* @param \Symfony\Component\Translation\TranslatorInterface $translator
* @param \Illuminate\Contracts\Translation\Translator $translator
* @return void
*/
public function setTranslator(TranslatorInterface $translator)
public function setTranslator(Translator $translator)
{
$this->translator = $translator;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Illuminate/Validation/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
"illuminate/container": "5.4.*",
"illuminate/contracts": "5.4.*",
"illuminate/support": "5.4.*",
"symfony/http-foundation": "3.2.*",
"symfony/translation": "3.2.*"
"symfony/http-foundation": "3.2.*"
},
"autoload": {
"psr-4": {
Expand Down
Loading

1 comment on commit 5ff36c6

@guiwoda
Copy link
Contributor

@guiwoda guiwoda commented on 5ff36c6 Jun 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @themsaid! Is there a reasoning behind this change? Just curious about why you'd separate from Symfony's MessageSelector

Please sign in to comment.