Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multilingual links in Blade Laravel

I'm translating a Laravel site which is using Blade templates and was wondering if there is a better way to translate a link as shown below:

app/lang/en/contact.php:

return [
    '1' => 'text before link',
    '2' => 'link text',
    '3' => 'text after link'
];

app/views/contact.blade.php:

<p>{{ Lang::get('contact.1') }} 
      <a herf="{{URL::route('home')}}">{{ Lang::get('contact.2') }}</a> 
{{ Lang::get('contact.3') }}}</p>
like image 813
WillyBurb Avatar asked Dec 01 '22 01:12

WillyBurb


2 Answers

What about this solution?

blade template:

{!! Lang::get('test.test', [ 'url' => 'http://stackoverflow.com/' ]) !!}

lang/test.php

<?php
return [
    'test' => '<a href=":url">My Pretty Link</a>'
]
like image 100
Artem Fitiskin Avatar answered Dec 05 '22 23:12

Artem Fitiskin


You could use the URL as a parameter and you would only need one translation entry:

app/lang/en/contact.php

return [
    '1' => 'text before link <a href=":url">link text</a>text after link'
];

app/views/contact.blade.php

<p>{{ Lang::get('contact.1', URL::route('home')) }}</p>

Downside: you have HTML in your translations

An other way to make your code cleaner (but doesn't change your translations in any way) would be using Laravels HTML link helper and trans() (Alias for Lang::get()

<p>
    {{ trans('contact.1') }}
    {{ link_to_route('home', trans('contact.2')) }}
    {{ trans('contact.3') }}
</p>
like image 32
lukasgeiter Avatar answered Dec 05 '22 22:12

lukasgeiter