Skip to content

Commit

Permalink
[5.2] Fix trans capitalization when replacement is numerical array (#…
Browse files Browse the repository at this point in the history
…14249)

* Reorder search and replace

Reorders the 3 search terms from [':ATTRIBUTE', ':Attribute', ':attribute'] to [':attribute', ':ATTRIBUTE', ':Attribute'], so that replacements indexed numerically (undocumented) would not get capitalized.

Translation string: 'Hello, :0!'
Replacement array: ['World']
Expected: 'Hello, World!'

* Reorder search and replace

Reorders the 3 search terms from `[':ATTRIBUTE', ':Attribute', ':attribute']` to `[':attribute', ':ATTRIBUTE', ':Attribute']`, so that replacements indexed numerically (undocumented) would not get capitalized.

* Add test for trans replacement capitalization
  • Loading branch information
microtony authored and taylorotwell committed Jul 8, 2016
1 parent f4267b0 commit def547d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/Illuminate/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ protected function makeReplacements($line, array $replace)

foreach ($replace as $key => $value) {
$line = str_replace(
[':'.Str::upper($key), ':'.Str::ucfirst($key), ':'.$key],
[Str::upper($value), Str::ucfirst($value), $value],
[':'.$key, ':'.Str::upper($key), ':'.Str::ucfirst($key)],
[$value, Str::upper($value), Str::ucfirst($value)],
$line
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2077,8 +2077,8 @@ protected function doReplacements($message, $attribute, $rule, $parameters)
$value = $this->getAttribute($attribute);

$message = str_replace(
[':ATTRIBUTE', ':Attribute', ':attribute'],
[Str::upper($value), Str::ucfirst($value), $value],
[':attribute', ':ATTRIBUTE', ':Attribute'],
[$value, Str::upper($value), Str::ucfirst($value)],
$message
);

Expand Down
8 changes: 8 additions & 0 deletions tests/Translation/TranslationTranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ public function testGetMethodProperlyLoadsAndRetrievesItem()
$this->assertEquals('foo', $t->get('foo::bar.foo'));
}

public function testGetMethodProperlyLoadsAndRetrievesItemWithCapitalization()
{
$t = $this->getMock('Illuminate\Translation\Translator', null, [$this->getLoader(), 'en']);
$t->getLoader()->shouldReceive('load')->once()->with('en', 'bar', 'foo')->andReturn(['foo' => 'foo', 'baz' => 'breeze :Foo :BAR']);
$this->assertEquals('breeze Bar FOO', $t->get('foo::bar.baz', ['foo' => 'bar', 'bar' => 'foo'], 'en'));
$this->assertEquals('foo', $t->get('foo::bar.foo'));
}

public function testGetMethodProperlyLoadsAndRetrievesItemWithLongestReplacementsFirst()
{
$t = $this->getMock('Illuminate\Translation\Translator', null, [$this->getLoader(), 'en']);
Expand Down

0 comments on commit def547d

Please sign in to comment.