How to use localization in Blade tag templates?
Asked Answered
W

7

6

1st question:

I've inserted the localization in many types of texts and things, but I don't know how to import it into in the following forms:

  {{ Form::label('name', 'here') }}
  {{ Form::text('name', null, array('placeholder' => 'here')) }}
  {{ Form::submit('here', array('class' => 'btn btn-success')) }}
  {{ Form::button('here', array('class' => 'btn btn-default')) }}

I want it to be in the form label 'here' and in the placeholder of the text 'here'.

2nd question:

I am not allowed to insert it with links in my language file: text here blah blah <a href="{{ URL::to('text') }}">BLAH</a>?

Is there anyway to insert it with links?

Thanks in advance.

Worthington answered 13/8, 2013 at 23:10 Comment(2)
Solved the 1st question, only couldn't figure out the placeholder. If anyone could help that and the 2nd question..Worthington
Just replace 'here' with Lang::get('here') for both questions.Burgwell
B
15

Supposing that your messages are stored in app/lang/en/message.php you can use the same way for all your cases:

In Blade template:

{{ Form::label('name', Lang::get('message.key')) }}

{{ Form::text('name', null, array('placeholder' => Lang::get('message.key'))) }}

{{ Form::submit(Lang::get('message.key'), array('class' => 'btn btn-success')) }}

In HTML tag mixed with some Blade expression:

<a href="{{ URL::to(Lang::get('message.key')) }}">BLAH</a>
Burgwell answered 14/8, 2013 at 2:56 Comment(4)
You can also use trans() helper function which wraps Lang::get(). More info laravel.com/docs/helpers#stringsClerkly
@Rubens Mariuzzo I don't think we understand eachother. I want the text be like: 'message' => 'There are no messages, would you like to <a href="{{ URL::to('messages/create') }}>create one?</a> in my language file, not to retrieve.Worthington
@RubensMariuzzo anything?Worthington
I was thinking about a better way to do so... but I just think that is weird to store bits of code inside a localized message. In any case, why you don't use placeholder? So you can replace them with the generated URL.Burgwell
W
7

You can also use localization in Blade templates using strictly Blade syntax. Given that you have a message in your /app/lang/en/messages.php that corresponds to the key "message_key", you can do :

@lang('messages.message_key')

to render the message in the locale that your application is configured to use.

Watchtower answered 20/4, 2015 at 16:10 Comment(1)
For a specific translation pls make sure to use these arguments below @lang('translationfile.mylabel', array(), 'en')Invoke
A
5

So, The answers for both of your questions are:-

1) {{ Form::label('name', 'here') }}

  Here, you need to change the "here" text hence laravel localization method can be used.For eg:-
{{ Form::label('name', '__("Here")' }} or
{{ Form::label('name', '__('message.here') }}. 

2)< a href="{{ URL::to('text') }}">BLAH< /a>

Here, you need to change the label instead of link.

< a href="URL::to('text') ">{{__('message.BLAH')}}< /a>. 
Adonic answered 7/7, 2018 at 6:41 Comment(0)
Q
2

A possible answer to your second question:

You could set up a language file resources/lang/en/page.php like this:

return [
    'sentence' => 'A sentence with a :link in the middle.',
    'link_text' => 'link to a another page'
];

And use it in a Blade template like this:

{!! trans('page.sentence', [
    'link' => '<a href="/">' . trans('page.link_text') . '</a>'
]) !!}}

The result would be:

A sentence with a link to a another page in the middle.

Quieten answered 24/1, 2016 at 19:26 Comment(0)
H
2

This is the simplest which works for me !

{!! Form::label('title', trans('users.addNewRecordsNameFieldLabel'),['class' => 'control-label']) !!}

I feel, already blade parsing is started the moment {!! or {{ is started

Helminthology answered 19/3, 2016 at 18:8 Comment(0)
R
1

You alse can use __() method

{{ __('app.name') }}

Really answered 18/2, 2017 at 14:33 Comment(0)
D
0

You can use it like this:

{{__('Our work at :appname is focused on ....', ['appname' => config('app.name')])}}
Daubery answered 18/6 at 10:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.