在 Twig 中将变量转换为字符串 [英] Convert variable to string in Twig
问题描述
我无法在 Twig 中翻译字符串.我正在为多语言网上商店构建一个主题.
I'm having trouble to translate a string in Twig. I'm building a theme for a multilingual webshop.
用户可以创建 USP(独特卖点).问题是当你使用不同的语言时它不会翻译.
A user can create a USP (Unique Selling Point). Problem is that it won't translate when you have a different language.
所以usp在模板{{theme.usp}}
中是这样调用的其结果是例如免费送货".
So the usp is called like this in the template {{ theme.usp }}
The outcome of that is for example "Free shipping".
要在系统中翻译字符串,您必须使用 {{ 'Free shipping' |t }}
过滤器.
To translate a string in the system you have to use a {{ 'Free shipping' | t }}
filter.
有什么方法可以翻译{{ theme.usp }}
.我认为这会很有用,但我不知道如何合并它.如何在 twig 中连接字符串
Is there any way to get {{ theme.usp }}
translated. I thought this would be useful but I don't know how to incorporate this. How to concatenate strings in twig
我所做的是:
{% set usp = {{ theme.usp }} %}
{{ usp | t }}
这样做会给我一个错误,因为 {{ theme.usp }}
必须在 ''
之间.这样做不会给我 '免费送货'
作为结果,而是 'theme.usp'
.
Doing this gives me an error since {{ theme.usp }}
has to be between ''
. Doing that doesn't gives me 'Free shipping'
as outcome but 'theme.usp'
.
有人有什么建议吗?
推荐答案
尝试:
{{ theme.usp|trans }}
或者如果它必须被t
过滤然后
or if it must be filtered by t
then
{{ theme.usp|t }}
这篇关于在 Twig 中将变量转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!