Django:切换从管理面板发送的消息的语言
我有一个模型“订单”,它在管理面板中有一个操作,允许管理员将有关订单的信息发送给列出该订单的某些人员。每个人都有语言设置,这就是发送消息所用的语言。
我正在使用的简短版本:
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)。
有没有办法对 body
和 subject
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用的是 Python 2.6(或从
__future__
导入with_statement
后的 Python 2.5),为了方便起见,您可以使用以下上下文管理器。使用示例:
这样做的好处是在抛出异常时是安全的,以及恢复线程的旧语言而不是 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.Example of usage:
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.
不确定激活/停用翻译是否是解决该问题的正确方法(?)
如果我面临这个问题,我会尝试构建一些模型来存储主题/正文/语言/类型字段。一些代码草案:
然后您可以根据类型和客户端语言轻松检索您需要的 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:
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.