php - Twig "trans with"不工作

标签 php twig translation

我在我的模板中使用了 twig trans 标签,我想像这样在其中传递变量:

{% trans with {
   '%link_start%': '<a href="http://www.google.nl/">',
   '%link_end%': '</a>'
} %}
    This %link_start%disclaimer%link_end% applies to all of our messages.
{% endtrans %}

但这给了我以下异常,它指向 {% trans with 行的 twig 模板:

PHP Fatal error: Uncaught exception 'Twig_Error_Syntax' with message 'Unexpected token "punctuation" of value "{" ("end of statement block" expected).' in [twig-template:line]

即使我复制并粘贴了 Symfony documentation 中的示例我得到同样的异常(exception)。所以我很茫然,我在这里做错了什么?

仅供引用:我使用的是启用了 i18n 扩展的 Twig 1.33(我没有使用 Symfony 框架)

最佳答案

Twig 不支持开箱即用的trans with。它是 Symfony 翻译扩展的一部分。这就解释了为什么即使是官方的 Symfony 文档也不起作用——你没有使用 Symfony。

查看本期:https://github.com/twigphp/Twig-extensions/issues/74 .有一个 pull request支持trans with,但还没有被合并。

您可能想使用 Symfony Translation Component在你的应用程序中。您可以在您的应用程序中使用 Symfony 组件,即使不使用完整的 Symfony(框架)堆栈。

我没试过,不过你可以试试用jhogervorst/Twi18n反而。

关于php - Twig "trans with"不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43300051/

相关文章:

php - Xampp/Lampp Mysql访问

php - mysql multiple COUNT() from multiple tables with LEFT JOIN

php - Eloquent laravel : How to get a row count from a ->get()

java - 使用 SAML 作为未加密 HTTP header 保护站点的桥梁

c++ - 如果在 Qt 中使用国际化,则翻译 "dynamic"内容

php - Woocommerce 允许选择一种变体

php - Twig - 为对象使用变量键

php - Symfony 翻译不起作用

php - "replace"过滤器需要一个数组或 "Traversable"作为替换值,得到 "string"

javascript - 翻译使用 Javascript 中的 Algolia 提取的动态内容