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

≯℡__Kan透↙ 提交于 2019-12-18 13:52:32

问题


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?


回答1:


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

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



回答2:


Using @lang directive:

@lang('nav.find')

Source: Retrieving Translation Strings




回答3:


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.



来源:https://stackoverflow.com/questions/30810776/how-can-i-use-html-tags-in-a-laravel-localization-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!