Django:切换从管理面板发送的消息的语言

发布于 2024-08-29 13:54:33 字数 1438 浏览 7 评论 0原文

我有一个模型“订单”,它在管理面板中有一个操作,允许管理员将有关订单的信息发送给列出该订单的某些人员。每个人都有语言设置,这就是发送消息所用的语言。

我正在使用的简短版本:

from django.utils.translation import ugettext as _
from django.core.mail import EmailMessage

lang = method_that_gets_customer_language()

body = _("Dear mister X, here is the information you requested\n")
body += some_order_information

subject = _("Order information")

email = EmailMessage(subject, body, '[email protected]', ['[email protected]'])
email.send()

有关他使用的语言的客户信息可以在 lang 中找到。默认语言为 en-us,翻译为法语 (fr) 和德语 (de)。

有没有办法对 bodysubject 使用 lang 中指定的语言的翻译,然后切换回 en-us?例如:lang 是“de”。主题和正文应获取“de”翻译文件中指定的字符串。

编辑:

找到了解决方案。

from django.utils import translation
from django.utils.translation import ugettext as _


body = "Some text in English"
translation.activate('de')
print "%s" % _(body)
translation.activate('en')

它的作用是获取 body 变量,将其翻译为德语,打印出来,然后将语言返回为英语。

不过,类似的东西

body = _("Some text in English")
translation.activate('de')
print "%s" % body

会用英文打印文本。

I have a model, Order, that has an action in the admin panel that lets an admin send information about the order to certain persons listed that order. Each person has language set and that is the language the message is supposed to be sent in.

A short version of what I'm using:

from django.utils.translation import ugettext as _
from django.core.mail import EmailMessage

lang = method_that_gets_customer_language()

body = _("Dear mister X, here is the information you requested\n")
body += some_order_information

subject = _("Order information")

email = EmailMessage(subject, body, '[email protected]', ['[email protected]'])
email.send()

The customer information about the language he uses is available in lang. The default language is en-us, the translations are in french (fr) and german (de).

Is there a way to use the translation for the language specified in lang for body and subject then switch back to en-us? For example: lang is 'de'. The subject and body should get the strings specified in the 'de' translation files.

edit:

Found a solution.

from django.utils import translation
from django.utils.translation import ugettext as _


body = "Some text in English"
translation.activate('de')
print "%s" % _(body)
translation.activate('en')

What this does it take the body variable, translates it to German, prints it then returns the language to English.

Something like

body = _("Some text in English")
translation.activate('de')
print "%s" % body

prints the text in English though.

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

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

发布评论

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

评论(2

铁轨上的流浪者 2024-09-05 13:54:33

如果您使用的是 Python 2.6(或从 __future__ 导入 with_statement 后的 Python 2.5),为了方便起见,您可以使用以下上下文管理器。

from contextlib import contextmanager
from django.utils import translation

@contextmanager
def language(lang):
    if lang and translation.check_for_language(lang):
        old_lang = translation.get_language()
        translation.activate(lang)

    try:
        yield
    finally:
        if lang:
            translation.activate(old_lang)

使用示例:

message = _('English text')
with language('fr'):
    print unicode(message)

这样做的好处是在抛出异常时是安全的,以及恢复线程的旧语言而不是 Django 默认语言。

If you're using Python 2.6 (or Python 2.5 after importing with_statement from __future__) you can use the following context manager for convenience.

from contextlib import contextmanager
from django.utils import translation

@contextmanager
def language(lang):
    if lang and translation.check_for_language(lang):
        old_lang = translation.get_language()
        translation.activate(lang)

    try:
        yield
    finally:
        if lang:
            translation.activate(old_lang)

Example of usage:

message = _('English text')
with language('fr'):
    print unicode(message)

This has the benefit of being safe in case something throws an exception, as well as restoring the thread's old language instead of the Django default.

高跟鞋的旋律 2024-09-05 13:54:33

不确定激活/停用翻译是否是解决该问题的正确方法(?)

如果我面临这个问题,我会尝试构建一些模型来存储主题/正文/语言/类型字段。一些代码草案:

class ClientMessageTemplate(models.Model):
    language = model.CharField(choices=AVAIALBLE_LANGUAGES,...)
    subject = models.CharField(...)
    body = models.CharField(...)
    type = models.CharField(choices=AVAILABLE_MESSAGE_TYPES)

然后您可以根据类型和客户端语言轻松检索您需要的 ClientMessageTemplate。

该解决方案的优点是,您可以通过管理界面维护所有数据,并且无需在每次发生更改时重新编译消息文件。

Not sure if activating/deactivating translation is proper way to solve that problem(?)

If I were facing that problem I would try to build some model for storing subjects/body/language/type fields. Some code draft:

class ClientMessageTemplate(models.Model):
    language = model.CharField(choices=AVAIALBLE_LANGUAGES,...)
    subject = models.CharField(...)
    body = models.CharField(...)
    type = models.CharField(choices=AVAILABLE_MESSAGE_TYPES)

Then you can retreive easily ClientMessageTemplate you need base on type and client's language.

Advantage of this solution is that you can have all data maintainable via admin interface and do not need to recompile message files each time something changed.

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