laravel 如何在 Blade 标签模板中使用本地化?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18220755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:16:38  来源:igfitidea点击:

How to use localization in Blade tag templates?

laravellaravel-4blade

提问by erm_durr

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

我不允许在我的语言文件中插入它的链接: text here blah blah <a href="{{ URL::to('text') }}">BLAH</a>?

Is there anyway to insert it with links?

反正有没有用链接插入它?

Thanks in advance.

提前致谢。

回答by Rubens Mariuzzo

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

假设您的消息存储在app/lang/en/message.php您可以对所有案例使用相同的方式:

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:

在混合了一些 Blade 表达式的 HTML 标签中:

<a href="{{ URL::to(Lang::get('message.key')) }}">BLAH</a>

回答by voitenkos

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

您还可以使用严格的 Blade 语法在 Blade 模板中使用本地化。鉴于您有一条/app/lang/en/messages.php对应于“message_key”键的消息,您可以执行以下操作:

@lang('messages.message_key')

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

以您的应用程序配置为使用的语言环境呈现消息。

回答by Austin

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

所以,你的两个问题的答案是:-

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

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>

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

回答by psgganesh

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

我觉得,已经开始刀片解析{!! 或 {{ 开始

回答by Феннур Мезитов

You alse can use __() method

您也可以使用 __() 方法

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

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

回答by M165437

A possible answer to your second question:

您的第二个问题的可能答案:

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

您可以resources/lang/en/page.php像这样设置语言文件:

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:

并在这样的 Blade 模板中使用它:

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

The result would be:

结果将是:

A sentence with a link to a another pagein the middle.

一个句子,中间有一个指向另一个页面链接