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>
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>'
]
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With