gpt4 book ai didi

email - 如何明确指定要发送的电子邮件的语言?

转载 作者:行者123 更新时间:2023-12-04 15:39:57 27 4
gpt4 key购买 nike

我有一个自定义用户模型,它有一个 preferred_language 字段。我希望将所有电子邮件(激活和密码重置)翻译成用户在配置文件中指定的语言发送。

class CustomUser(AbstractBaseUser, PermissionsMixin):
...
LANGUAGE_CHOICES = (
(1, "English"),
(2, "Русский")
)

preferred_language = models.PositiveSmallIntegerField(choices=LANGUAGE_CHOICES, default=2,
verbose_name=_("Preferred language"))

我考虑过设置自定义电子邮件类,但在天真的 djoser 的类中没有看到任何可以显式设置结果电子邮件语言的地方,尽管电子邮件模板具有可翻译的样式:

class ActivationEmail(BaseEmailMessage):
template_name = 'email/activation.html'

def get_context_data(self):
context = super(ActivationEmail, self).get_context_data()

user = context.get('user')
context['uid'] = utils.encode_uid(user.pk)
context['token'] = default_token_generator.make_token(user)
context['url'] = settings.ACTIVATION_URL.format(**context)
return context

重置密码 View (引用是上面 ActivationEmail 类中使用的 settings.EMAIL.activation):

class ResetPassword(ActionViewMixin, generics.GenericAPIView):
...

def send_password_reset_email(self, user):
context = {'user': user}
to = [get_user_email(user)]
settings.EMAIL.password_reset(self.request, context).send(to)

def send_activation_email(self, user):
context = {'user': user}
to = [get_user_email(user)]
settings.EMAIL.activation(self.request, context).send(to)

最佳答案

在您的情况下,我会使用 override在输入时存储当前语言(以便发送电子邮件)并在退出时恢复它的上下文管理器。

from django.utils import translation


def send_password_reset_email(self, user):
context = {'user': user}
to = [get_user_email(user)]
lang_code = user.lang_code # retrieve user's language code here
with translation.override(lang_code):
settings.EMAIL.password_reset(self.request, context).send(to)

def send_activation_email(self, user):
context = {'user': user}
to = [get_user_email(user)]
lang_code = user.lang_code # retrieve user's language code here
with translation.override(lang_code):
settings.EMAIL.activation(self.request, context).send(to)

关于email - 如何明确指定要发送的电子邮件的语言?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58146547/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com