问题描述
是的,所以,我想为我的模型存储翻译的选择,但 Django 不同意我的观点.Django 的版本是 1.3,模型和选项看起来像这样:
Yeah, so, I want to store translated choices for my model, but Django disagrees with me on this one. Version of Django is 1.3 and the model and choices look something like this:
from django.db import models
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _
RATE_CHOICES = (
('', _('Choose service rate')),
('5cpm_EUR', mark_safe(string_concat('€ 0,05 ', _('per minute')))),
('1cpm_EUR', mark_safe(string_concat('€ 0,01 ', _('per minute')))),
)
class Product(models.Model):
service_rate = models.CharField(_('service rate'), max_length=10, blank=True, choices=RATE_CHOICES)
此外,这些选择用于模型形式(对于另一个模型,因此我不得不重新声明该字段),如下所示:
Also, the choices are used in a modelform (for another model so i had to redeclare the field) like so:
service_rate = forms.ChoiceField(choices=RATE_CHOICES, widget=forms.Select(attrs={'class': 'chzn-select rate-select'}), required=False)
问题是无论我尝试什么;按照 django docs 上的内容,颠倒 mark_safe 和翻译的顺序,不使用延迟翻译等.它总是归结为 mark_safe 工作或翻译工作.但从来没有两者...
Problem is that no matter what I try; following the stuff on django docs, reversing order of mark_safe and translation, using no lazy translation etc. etc. it always comes down to either the mark_safe working or the translation working. But never both...
如何正确组合这两个功能?
How do I combine the two functions properly?
推荐答案
添加:
from django.utils import six # Python 3 compatibility
from django.utils.functional import lazy
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _
mark_safe_lazy = lazy(mark_safe, six.text_type)
然后:
mark_safe_lazy(string_concat('€ 0,05 ', _('per minute')))
这已添加到 Django 1.4 文档.
This was added to Django 1.4 docs.
这篇关于Django:在模型选择中将惰性翻译与标记安全相结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!