How can I use HTML tags in a Laravel localization file?

PhpLaravelBladeLaravel Localization

Php Problem Overview


I'm trying to utilize Laravel's localization feature, but I need to be able to put emphasis or bolden a portion of a phrase. Inserting a HTML tag into the language file causes it to be escaped when outputted to a blade.

For example, here is my language file entry:

return [
    'nav' => [
        'find' => '<strong>Find</strong> Your Home',
    ]
];

When I call it from within a blade: (I've tried using triple braces as well.)

{{ trans('base.nav.find') }}

It outputs:

&lt;strong&gt;Find&lt;/strong&gt; Your Home

I could potentially split the phrasing up like:

return [
    'nav' => [
        'fyh' => [
            'find' => 'Find',
            'yh'   => 'Your Home',
        ]
    ]
]

And then output:

<strong>{{ trans('base.nav.fyh.find') }}</strong>{{ trans('base.nav.fyh.yh') }}

But that seems like overkill. Any better solutions?

Php Solutions


Solution 1 - Php

Use {!! !!} instead of {{ }} to prevent escaping:

{!! trans('nav.find') !!}

Solution 2 - Php

Using @lang directive:

@lang('nav.find')

Source: Retrieving Translation Strings

Solution 3 - Php

Using Laravel 5.6 and above, can use the __ helper along with the blade syntax:

{!! __('pagination.next') !!}

Had to do those for the pagination blade templates.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMichael IrigoyenView Question on Stackoverflow
Solution 1 - PhpLimon MonteView Answer on Stackoverflow
Solution 2 - PhpSand Of VegaView Answer on Stackoverflow
Solution 3 - PhpbuiltbylarryView Answer on Stackoverflow