【问题标题】:How to use localization in Blade tag templates?如何在 Blade 标签模板中使用本地化?
【发布时间】:2013-08-15 18:10:58
【问题描述】:

第一个问题:

我已经在许多类型的文本和事物中插入了本地化,但我不知道如何将其导入以下形式:

  {{ 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')) }}

我希望它出现在表单标签“这里”和文本“这里”的占位符中。

第二个问题:

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

有没有用链接插入它?

提前致谢。

【问题讨论】:

  • 解决了第一个问题,只是找不到占位符。如果有人可以帮助这个和第二个问题..
  • 对于这两个问题,只需将 'here' 替换为 Lang::get('here')

标签: laravel laravel-4 blade


【解决方案1】:

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

在 Blade 模板中:

{{ 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')) }}

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

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

【讨论】:

  • 你也可以使用包裹Lang::get()trans()辅助函数。更多信息laravel.com/docs/helpers#strings
  • @Rubens Mariuzzo 我认为我们并不了解彼此。我希望文本像:'message' =&gt; 'There are no messages, would you like to &lt;a href="{{ URL::to('messages/create') }}&gt;create one?&lt;/a&gt; 在我的语言文件中,而不是检索。
  • @RubensMariuzzo 什么?
  • 我正在考虑一种更好的方法......但我只是认为将代码位存储在本地化消息中很奇怪。无论如何,为什么不使用占位符?所以你可以用生成的 URL 替换它们。
【解决方案2】:

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

@lang('messages.message_key')

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

【讨论】:

  • 对于特定的翻译,请确保在@lang('translationfile.mylabel', array(), 'en')下方使用这些参数
【解决方案3】:

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

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)BLAH

这里,你需要更改标签而不是链接。

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

【讨论】:

    【解决方案4】:

    这对我来说是最简单的!

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

    我感觉,刀片解析从 {!!或 {{ 开始

    【讨论】:

      【解决方案5】:

      第二个问题的可能答案:

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

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

      并在 Blade 模板中使用它,如下所示:

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

      结果是:

      中间有link to a another page的句子。

      【讨论】:

        【解决方案6】:

        你也可以使用__()方法

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

        【讨论】:

          猜你喜欢
          • 2018-08-18
          • 2016-06-19
          • 2022-01-04
          • 1970-01-01
          • 2022-11-25
          • 2015-09-12
          • 2014-03-20
          • 1970-01-01
          • 2011-09-17
          相关资源
          最近更新 更多