Skip to content

Commit 024067f

Browse files
authored
fix translation bug and add test (#39298)
* fix translation bug and add test * Apply fixes from StyleCI Co-authored-by: Taylor Otwell <[email protected]>
1 parent b757fe1 commit 024067f

File tree

2 files changed

+12
-21
lines changed

2 files changed

+12
-21
lines changed

src/Illuminate/Translation/Translator.php

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use Illuminate\Contracts\Translation\Loader;
77
use Illuminate\Contracts\Translation\Translator as TranslatorContract;
88
use Illuminate\Support\Arr;
9-
use Illuminate\Support\Collection;
109
use Illuminate\Support\NamespacedItemResolver;
1110
use Illuminate\Support\Str;
1211
use Illuminate\Support\Traits\Macroable;
@@ -216,30 +215,15 @@ protected function makeReplacements($line, array $replace)
216215
return $line;
217216
}
218217

219-
$replace = $this->sortReplacements($replace);
218+
$shouldReplace = [];
220219

221220
foreach ($replace as $key => $value) {
222-
$line = str_replace(
223-
[':'.$key, ':'.Str::upper($key), ':'.Str::ucfirst($key)],
224-
[$value, Str::upper($value), Str::ucfirst($value)],
225-
$line
226-
);
221+
$shouldReplace[':'.$key] = $value;
222+
$shouldReplace[':'.Str::upper($key)] = Str::upper($value);
223+
$shouldReplace[':'.Str::ucfirst($key)] = Str::ucfirst($value);
227224
}
228225

229-
return $line;
230-
}
231-
232-
/**
233-
* Sort the replacements array.
234-
*
235-
* @param array $replace
236-
* @return array
237-
*/
238-
protected function sortReplacements(array $replace)
239-
{
240-
return (new Collection($replace))->sortBy(function ($value, $key) {
241-
return mb_strlen($key) * -1;
242-
})->all();
226+
return strtr($line, $shouldReplace);
243227
}
244228

245229
/**

tests/Translation/TranslationTranslatorTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,13 @@ public function testGetJsonReplaces()
150150
$this->assertSame('bar onetwo three', $t->get('foo :i:c :u', ['i' => 'one', 'c' => 'two', 'u' => 'three']));
151151
}
152152

153+
public function testGetJsonHasAtomicReplacements()
154+
{
155+
$t = new Translator($this->getLoader(), 'en');
156+
$t->getLoader()->shouldReceive('load')->once()->with('en', '*', '*')->andReturn(['Hello :foo!' => 'Hello :foo!']);
157+
$this->assertSame('Hello baz:bar!', $t->get('Hello :foo!', ['foo' => 'baz:bar', 'bar' => 'abcdef']));
158+
}
159+
153160
public function testGetJsonReplacesForAssociativeInput()
154161
{
155162
$t = new Translator($this->getLoader(), 'en');

0 commit comments

Comments
 (0)