如何在 laravel blade 模板中的 php 代码中检索翻译字符串
How to retrieve translation strings inside a php code in laravel blade template
我正在尝试在 laravel 8 中的 blade 模板中的 php foreach 循环代码中使用字符串的本地化检索。
在 foreach 循环内,我试图操纵一个名为 $item['label']
的值,并使用 laravel 具有的语言本地化等同于翻译它的值。
这是我当前的代码。
@foreach ($items as $item)
@php
$item['label'] = "{{ __($item['label']) }}"
@endphp
@endforeach
但是我得到一个错误
ParseError
syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)
首先,我可以在 @php
中使用 {{ __ ('string') }}
或 @lang('string')
吗?
如果我做不到,还有其他方法吗?
非常感谢!
在使用 foreach 时你不能在这里改变它的值试试这个如果 $items
是数组而不是 stdClass
@foreach ($items as $key => $item)
@php
$items[$key]['label'] = __($item['label']);
@endphp
@endforeach
@php 和@endphp 是一个 blade 语法,和写法一样:
<?php ?>
所以你可以这样做:
<?php
echo __('profile/username');
?>
或者您可以使用 Blade 模板引擎编写它:
@php
echo __('profile/username');
@endphp
要打印项目,您可以这样做:
@foreach ($items as $key => $item)
{{ __($item) }}
@endforeach
这里有一个数据示例:
@php
$items = ['engine_1' => ['label' => 'Google'], 'engine_2' => ['label' => 'Bing']];
@endphp
@foreach ($items as $key => $item)
{{ __($item['label']) }}
@endforeach
// The output will be => Google Bing
为了保存项目的翻译,删除“{{ }}”并使用键来检测要在哪个索引上应用更改,如下所示:
@foreach ($items as $key => $item)
@php
$items[$key]['label'] = __($item['label'])
@endphp
@endforeach
注意@Nurbek Boymurodov 写给您的内容,您需要使用 $key,因为这样做不会覆盖 foreach 循环中的数据:
@foreach ($items as $key => $item)
@php
$item['label'] = __($item['label']); // Wrong way of overriding data
@endphp
@endforeach
谢谢@Nurbek Boymurodov!
你的评论回答了我的问题。
现在是代码:
@foreach ($items as $item)
@php
$item['label'] = __($item['label']);
@endphp
//other codes where I used the manipulated $item['label']
@endforeach
通过删除 {{ }}
我已经操纵了我想要的值,谢谢!
我正在尝试在 laravel 8 中的 blade 模板中的 php foreach 循环代码中使用字符串的本地化检索。
在 foreach 循环内,我试图操纵一个名为 $item['label']
的值,并使用 laravel 具有的语言本地化等同于翻译它的值。
这是我当前的代码。
@foreach ($items as $item)
@php
$item['label'] = "{{ __($item['label']) }}"
@endphp
@endforeach
但是我得到一个错误
ParseError syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)
首先,我可以在 @php
中使用 {{ __ ('string') }}
或 @lang('string')
吗?
如果我做不到,还有其他方法吗?
非常感谢!
在使用 foreach 时你不能在这里改变它的值试试这个如果 $items
是数组而不是 stdClass
@foreach ($items as $key => $item)
@php
$items[$key]['label'] = __($item['label']);
@endphp
@endforeach
@php 和@endphp 是一个 blade 语法,和写法一样:
<?php ?>
所以你可以这样做:
<?php
echo __('profile/username');
?>
或者您可以使用 Blade 模板引擎编写它:
@php
echo __('profile/username');
@endphp
要打印项目,您可以这样做:
@foreach ($items as $key => $item)
{{ __($item) }}
@endforeach
这里有一个数据示例:
@php
$items = ['engine_1' => ['label' => 'Google'], 'engine_2' => ['label' => 'Bing']];
@endphp
@foreach ($items as $key => $item)
{{ __($item['label']) }}
@endforeach
// The output will be => Google Bing
为了保存项目的翻译,删除“{{ }}”并使用键来检测要在哪个索引上应用更改,如下所示:
@foreach ($items as $key => $item)
@php
$items[$key]['label'] = __($item['label'])
@endphp
@endforeach
注意@Nurbek Boymurodov 写给您的内容,您需要使用 $key,因为这样做不会覆盖 foreach 循环中的数据:
@foreach ($items as $key => $item)
@php
$item['label'] = __($item['label']); // Wrong way of overriding data
@endphp
@endforeach
谢谢@Nurbek Boymurodov!
你的评论回答了我的问题。
现在是代码:
@foreach ($items as $item)
@php
$item['label'] = __($item['label']);
@endphp
//other codes where I used the manipulated $item['label']
@endforeach
通过删除 {{ }}
我已经操纵了我想要的值,谢谢!