Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 604
Michael Irigoyen Avatar asked Jun 12 '15 19:06

Michael Irigoyen


2 Answers

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

{!! trans('nav.find') !!} 
like image 143
Limon Monte Avatar answered Sep 24 '22 18:09

Limon Monte


Using @lang directive:

@lang('nav.find') 

Source: Retrieving Translation Strings

like image 26
Sand Of Vega Avatar answered Sep 23 '22 18:09

Sand Of Vega