How to use localization in Blade tag templates?

依然范特西╮ 提交于 2020-01-01 12:33:12

问题


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.


回答1:


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>



回答2:


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.




回答3:


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>. 



回答4:


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




回答5:


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.




回答6:


You alse can use __() method

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



来源:https://stackoverflow.com/questions/18220755/how-to-use-localization-in-blade-tag-templates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!