15

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?

3 Answers 3

36

use {!! !!} instead of {{ }} and <br/> tag in you message

<p>{!! trans('mission-vision-page.mission-description') !!}</p>
2
1

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');
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.