我自己的 twig 扩展中的 i18n

发布于 2025-01-05 11:11:24 字数 171 浏览 1 评论 0原文

如何在我自己的 twig 扩展中使用 twigs i18n?

这就是我现在所拥有的: http://pastebin.com/yNhHuC6C 但需要在其中使用 i18n 。

谢谢

How can I use twigs i18n in my own twig extension?

This is what I have right now: http://pastebin.com/yNhHuC6C but need to use i18n within.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坐在坟头思考人生 2025-01-12 11:11:24

您肯定有这个 twig 扩展的服务定义,例如:

    <service id="test_post_extension" class="Test\PostBundle\Twig\Extensions\TestPostExtension">
        <tag name="twig.extension" />
    </service>

您可以在您的扩展中注入任何其他服务,例如 twig.extension.trans 服务。

xml 变为:

    <service id="test_post_extension" class="Test\PostBundle\Twig\Extensions\TestPostExtension">
        <tag name="twig.extension" />
        <argument type="service" id="twig.extension.trans" />
    </service>

您的 TestPostExtension 类将接收 Symfony\Bridge\Twig\Extension\TranslationExtension 作为其构造函数的第一个参数:

namespace Test\PostBundle\Twig\Extensions;

class TestPostExtension extends \Twig_Extension
{
    public function __construct(Symfony\Bridge\Twig\Extension\TranslationExtension $transExt) {
        $this->transExt = $transExt;
    }

     // some stuff
}

You surely have a service definition of this twig extension, something like:

    <service id="test_post_extension" class="Test\PostBundle\Twig\Extensions\TestPostExtension">
        <tag name="twig.extension" />
    </service>

You can inject any other service in your's, like the twig.extension.trans service.

The xml becomes:

    <service id="test_post_extension" class="Test\PostBundle\Twig\Extensions\TestPostExtension">
        <tag name="twig.extension" />
        <argument type="service" id="twig.extension.trans" />
    </service>

Your TestPostExtension class will then receive a Symfony\Bridge\Twig\Extension\TranslationExtension as first argument of its constructor:

namespace Test\PostBundle\Twig\Extensions;

class TestPostExtension extends \Twig_Extension
{
    public function __construct(Symfony\Bridge\Twig\Extension\TranslationExtension $transExt) {
        $this->transExt = $transExt;
    }

     // some stuff
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文