简体   繁体   English

对象内树枝中的翻译字符串

[英]translation strings in twig inside an object

I'm trying to put translation strings inside an object within Twig. 我正在尝试将翻译字符串放在Twig中的对象内。 So far I haven't got it right and can't figure out how to properly do it. 到目前为止,我还没有做对,也无法弄清楚如何正确地做到这一点。 I didn't think this would work, but it was my best effort so far. 我认为这行不通,但这是迄今为止我最大的努力。

{% set options = {
    0 : {{ 'user.first_name'|trans }},
    1 : {{ 'user.surname'|trans }},
    2 : {{ 'user.initials'|trans }}
} %}

I get the error: 我得到错误:

A hash key must be a quoted string, a number, a name, or an expression enclosed in parentheses (unexpected token "punctuation" of value "{". 哈希键必须是带引号的字符串,数字,名称或用括号括起来的表达式(值“ {”的意外标记“标点”)。

Any one any ideas? 有任何想法吗? Thanks in advance. 提前致谢。

The syntax {{ ... }} is used to output content. 语法{{ ... }}用于输出内容。 You don't need to interpolate the variable in order to add it to the object 您无需插值变量即可将其添加到对象

{% set options = {
    0 : user.first_name|trans,
    1 : user.surname|trans,
    2 : user.initials|trans,
} %}

Some extra notes. 一些额外的笔记。 As you are using numeric indices you could use the following snippet 当您使用数字索引时,可以使用以下代码段

{% set options = [
    user.first_name|trans,
    user.surname|trans,
    user.initials|trans,
] %}

demo 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM