问题
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.
return ['mission-description' => 'line 1 <br /> line 2']
2.
return ['mission-description' => 'line 1 \n line 2']
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?
回答1:
use {!! !!}
instead of {{ }}
and <br/>
tag in you message
<p>{!! trans('mission-vision-page.mission-description') !!}</p>
回答2:
If you happen to use the @lang
blade directive, you can override the helper yourself using the following code:
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.
回答3:
Add custom blade directive, in App\Providers\AppServiceProvider.php
add:
\Blade::directive('lang2br', function ($expression) {
return "<?php echo nl2br(app('translator')->getFromJson({$expression})); ?>";
});
Then you can use like lang
directive:
@lang2br('something');
来源:https://stackoverflow.com/questions/37727515/break-lines-in-blade-translation-files