-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[5.4] Replace symfony's translator (#15563)
* replace symfony's translator * update helper methods
- Loading branch information
1 parent
3dcfcb1
commit 5ff36c6
Showing
14 changed files
with
240 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
5ff36c6
There was a problem hiding this comment.
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