php 如何在 Laravel 本地化文件中使用 HTML 标签?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30810776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:04:40  来源:igfitidea点击:

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

phplaravelbladelaravel-localization

提问by Michael Irigoyen

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.

我正在尝试利用 Laravel 的本地化功能,但我需要能够强调或加粗短语的一部分。将 HTML 标记插入语言文件会导致它在输出到刀片时被转义。

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?

但这似乎有点矫枉过正。有什么更好的解决方案吗?

回答by Limon Monte

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

使用{!! !!}而不是{{ }}防止转义:

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

回答by Sand Of Vega

Using @langdirective:

使用@lang指令:

@lang('nav.find')

Source: Retrieving Translation Strings

来源:检索翻译字符串

回答by builtbylarry

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

使用 Laravel 5.6 及更高版本,可以使用__helper 和刀片语法:

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

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

Had to do those for the pagination blade templates.

必须为分页刀片模板做这些。