laravel 在刀片翻译文件中换行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37727515/
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
Break lines in blade translation files
提问by Jorge Anzola
In my view I have
在我看来,我有
<p>{{ trans('mission-vision-page.mission-description') }}</p>
I've put a block of text in my language file, but I want to maintain the new lines. I've tried:
我在我的语言文件中放入了一段文本,但我想保留新行。我试过了:
1.
1.
return ['mission-description' => 'line 1 <br /> line 2']
2.
2.
return ['mission-description' => 'line 1 \n line 2']
3.
3.
$newLine = '<br />';
return ['mission-description' => 'line 1 ' . $newLine . ' line 2']
I know probably there's a better way to accomplish this, but in my en.php file can I add new lines?
我知道可能有更好的方法来实现这一点,但是在我的 en.php 文件中我可以添加新行吗?
回答by huuuk
use {!! !!}
instead of {{ }}
and <br/>
tag in you message
在您的消息中使用{!! !!}
代替{{ }}
和<br/>
标记
<p>{!! trans('mission-vision-page.mission-description') !!}</p>
回答by Flame
If you happen to use the @lang
blade directive, you can override the helper yourself using the following code:
如果您碰巧使用了@lang
刀片指令,您可以使用以下代码自己覆盖帮助程序:
Blade::directive('lang', function ($expression) {
// @see Illuminate\View\Compilers\Concerns\CompilesTranslations.php::compileLang()
if (is_null($expression)) {
return '<?php $__env->startTranslation(); ?>';
} elseif ($expression[1] === '[') {
return "<?php $__env->startTranslation{$expression}; ?>";
}
return "<?php echo nl2br(app('translator')->getFromJson({$expression})); ?>";
});
This is basically a copy of the helper code in the vendor
dir (Illuminate\View\Compilers\Concerns\CompilesTranslations.php::compileLang()
) with an nl2br
wrapped around the output. This converts any newlines to <br />
tags.
这基本上是vendor
dir ( Illuminate\View\Compilers\Concerns\CompilesTranslations.php::compileLang()
) 中帮助程序代码的副本,并nl2br
在输出中包装了一个。这会将任何换行符转换为<br />
标签。
回答by Grzegorz Adam Kowalski
Add custom blade directive, in App\Providers\AppServiceProvider.php
add:
添加自定义刀片指令,App\Providers\AppServiceProvider.php
添加:
\Blade::directive('lang2br', function ($expression) {
return "<?php echo nl2br(app('translator')->getFromJson({$expression})); ?>";
});
Then you can use like lang
directive:
然后你可以使用 likelang
指令:
@lang2br('something');