强制 Twig 语言环境 [英] Forcing the Twig Locale

查看:28
本文介绍了强制 Twig 语言环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Twig 模板系统来模板化我的电子邮件.电子邮件的区域设置应基于用户设置,而不是会话或请求区域设置.渲染 Twig 模板时如何强制语言环境?

手册确实提到如何强制翻译器的语言环境.但我想将此语言环境传递给 render() 方法,以便在此语言环境中呈现树枝模板内的翻译.

这与在模板中使用 into 不同,因为我认为这会强制在特定语言环境中的模板内部进行翻译.

所以,以 Symfony 为例,我正在寻找这样的东西:

公共函数 indexAction($name){$message = \Swift_Message::newInstance()->setSubject('你好电子邮件')->setFrom('send@example.com')->setTo('recipient@example.com')->setBody($this->renderView('HelloBundle:Hello:email.txt.twig',数组('name' => $name),'nl_NL'//<-- 这会很好!));$this->get('mailer')->send($message);返回 $this->render(...);}

解决方案

当您使用 trans 过滤器时,您可以将语言环境作为参数传递(参见差异:https://github.com/symfony/symfony/commit/3ea31a02630412b1c732ee1647a0724665>

因此,您可以在控制器的渲染方法中传递另一个 user_locale 变量(或传递整个用户对象,而不是分别传递 name 和 user_locale,或者如果用户将登录,则在模板中使用 app.user 等...(显然取决于您的应用程序)),然后在您的电子邮件模板中,您可以拥有如下内容:

{{ '问候' |trans({}, "messages", user_locale) }} {{ name |标题 }}{# 带有更多翻译字符串的电子邮件模板的其余部分 #}

然后在该语言环境的翻译文件中(假设您使用的是 yaml),只需要有这样的内容,翻译就会立即为您工作:

# messages.fr.yml问候语:'卓悦'

I would like to use the Twig template system to template my e-mails. The locale of the e-mail should be based on a user setting, not from the session or request locale. How can I force the locale when rendering a Twig template?

The manual does mention how to force the locale for the Translator. But i'd like pass this locale to the render() method, in order to have the translations inside the twig template to be rendered in this locale.

This is different from using into in the template, because I think this forces a translation inside the template in a specific locale.

So, taking the example from Symfony, I'm looking for something like this:

public function indexAction($name)
{
    $message = \Swift_Message::newInstance()
        ->setSubject('Hello Email')
        ->setFrom('send@example.com')
        ->setTo('recipient@example.com')
        ->setBody(
            $this->renderView(
                'HelloBundle:Hello:email.txt.twig',
                array('name' => $name),
                'nl_NL' // <-- This would be nice!
            )
        )
    ;
    $this->get('mailer')->send($message);

    return $this->render(...);
}

解决方案

You can pass the locale through as an argument when you use the trans filter (see diff: https://github.com/symfony/symfony/commit/3ea31a02630412b1c732ee1647a0724378f67665).

So you could pass another user_locale variable in your render method in your controller (or pass through the whole user object instead of passing name and user_locale separately, or use app.user in your template if the user will be logged in, etc... (depending on your application obviously)), then in your email template you can have something like this:

{{ 'greeting' | trans({}, "messages", user_locale) }} {{ name | title }}
{# rest of email template with more translation strings #}

Then in your translation file for that locale (assuming you're using yaml) just have something like this and the translations will work nicely for you on the fly:

# messages.fr.yml    
greeting: 'Bonjour'

这篇关于强制 Twig 语言环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆