Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 5 additions & 21 deletions src/Illuminate/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Illuminate\Contracts\Translation\Loader;
use Illuminate\Contracts\Translation\Translator as TranslatorContract;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\NamespacedItemResolver;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Macroable;
Expand Down Expand Up @@ -216,30 +215,15 @@ protected function makeReplacements($line, array $replace)
return $line;
}

$replace = $this->sortReplacements($replace);
$shouldReplace = [];

foreach ($replace as $key => $value) {
$line = str_replace(
[':'.$key, ':'.Str::upper($key), ':'.Str::ucfirst($key)],
[$value, Str::upper($value), Str::ucfirst($value)],
$line
);
$shouldReplace[':'.$key] = $value;
$shouldReplace[':'.Str::upper($key)] = Str::upper($value);
$shouldReplace[':'.Str::ucfirst($key)] = Str::ucfirst($value);
}

return $line;
}

/**
* Sort the replacements array.
*
* @param array $replace
* @return array
*/
protected function sortReplacements(array $replace)
{
return (new Collection($replace))->sortBy(function ($value, $key) {
return mb_strlen($key) * -1;
})->all();
return strtr($line, $shouldReplace);
}

/**
Expand Down
7 changes: 7 additions & 0 deletions tests/Translation/TranslationTranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,13 @@ public function testGetJsonReplaces()
$this->assertSame('bar onetwo three', $t->get('foo :i:c :u', ['i' => 'one', 'c' => 'two', 'u' => 'three']));
}

public function testGetJsonHasAtomicReplacements()
{
$t = new Translator($this->getLoader(), 'en');
$t->getLoader()->shouldReceive('load')->once()->with('en', '*', '*')->andReturn(['Hello :foo!' => 'Hello :foo!']);
$this->assertSame('Hello baz:bar!', $t->get('Hello :foo!', ['foo' => 'baz:bar', 'bar' => 'abcdef']));
}

public function testGetJsonReplacesForAssociativeInput()
{
$t = new Translator($this->getLoader(), 'en');
Expand Down