如何在Slim框架中使用Twig模板使用GetText?
我正在使用Slim 4 Framework,其中包括用于模板的树枝引擎。
不久前,Twig具有{%trans%}
令牌的扩展名,| trans
过滤器直接链接到getText函数。
现在,事情似乎更加复杂,树枝链接到符号翻译器...您知道如何将树枝链接到getText吗?
我试图创建一个自定义过滤器,并且可以正常工作,但是我不知道如何创建一个tokenparser只是为了调用getText()函数。
I'm working with Slim 4 Framework with a Twig engine for templates.
Some time ago, Twig had an extension with {% trans %}
tokens and |trans
filter directly linked to gettext function.
Now things seems to be more complicated, Twig is linked to a Symfony Translator... do you know how can I link Twig to gettext?
I tried to create a custom filter, and it works, but I don't know how to create a tokenparser just to call gettext() function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“官方”方法是使用 twig-bridge 提供Twig 3 <代码> TranslationExtension 用 trans
筛选。为此,您必须安装 Symfony Translator component。
TranslationExtension
可以使用自定义Translator
必须实现TranslatorInterface
。 Symfony Translator能够将MO文件用作源。因此,您不必再使用getText
扩展名。如果您仍然想使用
getText
函数,也可以尝试创建一个自定义twig函数:https://symfony.com/doc/current/current/templating/twig_extension.html
用法
{{__ __('Hello hello world')}}
您的第二种方法是您的第二个方法需要一个自定义文本解析器。
The "official" way would be to use the Twig-Bridge component that provides a Twig 3
TranslationExtension
to translate messages with the transfilter. For this, you have to install the Symfony translator component.
The
TranslationExtension
can be configured with a customTranslator
that must implement theTranslatorInterface
. The Symfony Translator is able to use mo files as source. So you don't have to use thegettext
extension anymore.If you still want to use the
gettext
function, you could also try to create a custom Twig function:https://symfony.com/doc/current/templating/twig_extension.html
Usage
{{ __('Hello world') }}
The downside of the second approach is that you need a custom text parser.
我已经解决了将此依赖性添加到Composer:
twig/Extensenions
,并将其扩展到twig config:
I have solved adding this dependency to composer:
twig/extensions
And this extension to Twig config: