如何在Slim框架中使用Twig模板使用GetText?

发布于 2025-01-26 20:08:36 字数 257 浏览 3 评论 0原文

我正在使用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 技术交流群。

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

发布评论

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

评论(2

谁的年少不轻狂 2025-02-02 20:08:36

“官方”方法是使用 twig-bridge 提供Twig 3 <代码> TranslationExtension 用 trans
筛选。为此,您必须安装 Symfony Translator component。

TranslationExtension可以使用自定义Translator必须实现TranslatorInterface。 Symfony Translator能够将MO文件用作源。因此,您不必再使用getText扩展名。

use Symfony\Bridge\Twig\Extension\TranslationExtension;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\Formatter\MessageFormatter;
use Symfony\Component\Translation\IdentityTranslator;
use Symfony\Component\Translation\Loader\MoFileLoader;
use Slim\Views\Twig;
// ...

$twig = Twig::create($paths, $options);

$translator = new Translator(
    'en_US',
    new MessageFormatter(new IdentityTranslator())
);

$translator->addLoader('mo', new MoFileLoader());
$twig->addExtension(new TranslationExtension($translator));

如果您仍然想使用getText函数,也可以尝试创建一个自定义twig函数:

https://symfony.com/doc/current/current/templating/twig_extension.html

use Twig\TwigFunction;

$environment = $twig->getEnvironment();

$environment->addFunction(new TwigFunction('__', function ($message) {
    return __($message);
}));

用法

{{__ __('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 trans
filter. For this, you have to install the Symfony translator component.

The TranslationExtension can be configured with a custom Translator that must implement the TranslatorInterface. The Symfony Translator is able to use mo files as source. So you don't have to use the gettext extension anymore.

use Symfony\Bridge\Twig\Extension\TranslationExtension;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\Formatter\MessageFormatter;
use Symfony\Component\Translation\IdentityTranslator;
use Symfony\Component\Translation\Loader\MoFileLoader;
use Slim\Views\Twig;
// ...

$twig = Twig::create($paths, $options);

$translator = new Translator(
    'en_US',
    new MessageFormatter(new IdentityTranslator())
);

$translator->addLoader('mo', new MoFileLoader());
$twig->addExtension(new TranslationExtension($translator));

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

use Twig\TwigFunction;

$environment = $twig->getEnvironment();

$environment->addFunction(new TwigFunction('__', function ($message) {
    return __($message);
}));

Usage

{{ __('Hello world') }}

The downside of the second approach is that you need a custom text parser.

幼儿园老大 2025-02-02 20:08:36

我已经解决了将此依赖性添加到Composer:

twig/Extensenions

,并将其扩展到twig config:

 $twig = Twig::create($twigSettings['path'], $twigSettings['settings']);
 $twig->addExtension(new I18nExtension());

I have solved adding this dependency to composer:

twig/extensions

And this extension to Twig config:

 $twig = Twig::create($twigSettings['path'], $twigSettings['settings']);
 $twig->addExtension(new I18nExtension());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文