Framework: Text translation method escapes characters of transmitted values.

Created on 4 Oct 2018  Â·  6Comments  Â·  Source: laravel/framework

  • Laravel Version: 5.7.7 (clear installed)
  • PHP Version: 7.2.9 (Laravel Homestead 7.18.0)
// resources/lang/en/unk.php
return [
    'foo' => 'first :value string',
    'bar' => 'second   string',
];
// resources/views/welcome.blade.php
@php($q = '1 2')

{!! $q !!}
{!! __('unk.foo', ['value' => $q]) !!}
{!! __('unk.bar') !!}

Result:

1 2
first 1 2 string
second   string

Method trans() produces exactly the same results.

It should be:

1 2
first 1 2 string
second   string

Example from the site:
The database has a field with the cost of the car: 15366232.00
It needs to be displayed on the page as from 15 366 232 ₽
File resources/lang/*/info.php has lines: 'priceFrom' => 'from :price ₽',

function price_format($value): string
{
    return number_format($value, 0, '.', ' ');
}
{!! __('info.priceFrom', ['price' => price_format($car->price)]) !!}

Commit from Taylor added many problems when using a trans() helper.

Nice prices (no):
2018-10-04 11-50-27 mercedes-benz e- - google chrome

Passing the space character code to a parameter is necessary so that when changing the width of the browser window, the cost does not break into two lines.
For example:

от 3 505
845 ₽

And also, in the current state of affairs, instead of

{!! __('info.priceFrom', ['price' => price_format($car->price)]) !!}

write

{{  __('info.from') }}
{!! price_format($car->price) !!}
{{  __('info.currency.rub') }}

🤷😒

Most helpful comment

I'm reverting this entire dumpster fire. Will just document if you use @lang its up to you to escape.

All 6 comments

Related PR - #25858.

This is expected behaviour due to a recent security patch.

@laurencei: You're talking about d3c0a369057d0b6ebf29b5f51c903b1a85e3e09b which is not related to this issue. If you read https://github.com/laravel/framework/pull/25858#issuecomment-426294679 and https://github.com/laravel/framework/pull/25858#issuecomment-426552670 you'll see that related PR is a breaking change in 5.7.

Thanks @bonzai - I didnt realise it was seperate.

So this is really a duplicate of https://github.com/laravel/framework/issues/25515 then? i.e. the whole lang escaping thing is still breaking?

Taylor recommends (#25919) using new HtmlString('foo') inside Blade templates. This idea looks like a wild crutch.

I'm reverting this entire dumpster fire. Will just document if you use @lang its up to you to escape.

Was this page helpful?
0 / 5 - 0 ratings