如何使用变量处理树枝文件中的翻译? [英] How to handle Translation in twig file using variables?
问题描述
我有一个树枝文件和yml,其中定义了翻译变量
I have a twig file and a yml, in which I define the variables for translation
例如:
YML文件-
variable.for.translation:不可靠,paratí
树枝文件的内容-
<h2>"Hola, Follow {{ variableName }} en Twitter</h2>
上关注{{variableName}}}我想为
I wanted to make a variable for
Hola, Follow {{ variableName }} en Twitter
在翻译文件(即我的yml文件)中。
当前我正在这样做:
在YML中-
in translation file (i.e my yml file).
Currently i am doing it like this :
In YML -
follow.us.twitter: Hola, Follow
follow.us.twitter1: en Twitter
在树枝上-
<h2>{{ "follow.us.twitter"|trans([], "workend") }} {{ variableName }} {{ "follow.us.twitter1"|trans([], "workend") }}</h2>
I ts工作正常,但问题是我现在在树枝上有3个变量,它们太多了:
Its working fine, but the problem is that i now have 3 variables in a twig which are too much :
1. follow.us.twitter
2. follow.us.twitter1
3. {{ variableName }}
我试图用一个变量来做,就像这样:
I tried to do it with a single variable, like so:
follow.us.in.twitter : Hola, Follow {{ variableName }} en Twitter
and
<h2>{{ "follow.us.in.twitter"|trans([], "workend") }}</h2>
,但是没有用。问题是我正在使用的变量,即{{variableName}} 。
but it didn't work. The problem is the variable i.e {{ variableName }} I am using.
有什么方法可以处理预定义变量并在翻译文件中定义它?
Is there any way to do handle a predefined variable and define it in translation file?
推荐答案
您正在正确的轨道上,错过的只是通过 someVariable
作为参数来转换为 Twig文件中的参数:
You were going on right track, what you missed is just to pass someVariable
as a parameter to trans() in your Twig file as:
<h2>{{ "follow.us.in.twitter"|trans({'%someVariable%': someVariable}, "workend") }}</h2>
现在 Yml文件中的消息应为:
follow.us.in.twitter: Hola, Follow %someVariable% en Twitter
这应该有效。
有关更多详细信息和清晰度,请参阅以下内容:
Symfony图书
祝你好运。
这篇关于如何使用变量处理树枝文件中的翻译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!