Django:将懒惰的翻译与模型选择中的安全标记结合起来 [英] Django: combine lazy translation with mark safe in model choices
问题描述
是的,所以,我想为我的模型存储已翻译的选项,但Django在这一点上不同意我的看法。 Django版本是1.3,模型和选择如下所示:
from django.db import models
from django.utils.safestring import mark_safe $ b $ from django.utils.translation import ugettext_lazy as _
$ b RATE_CHOICES =(
('',_('Choose service rate')),
('5cpm_EUR',mark_safe(string_concat('& euro; 0,05',_('per minute')))),
('1cpm_EUR',mark_safe(string_concat('&欧元; 0,01',_('每分钟')))),
)
类Product(models.Model):
service_rate = models.CharField(_ ('service rate'),max_length = 10,blank = True,options = RATE_CHOICES)
这些选项用于模型(对于另一个模型,所以我必须重新声明该字段),如下所示:
service_rate = forms。 ChoiceField(options = RATE_CHOICES,widget = forms.Select(attrs = {'class':'chzn-select rate-select'}),required = False)
问题是,无论我尝试什么,遵循django文档中的内容,颠倒mark_safe和翻译的顺序,不使用懒惰翻译等等,它总是归结为mark_safe工作或翻译工作。但是从来都没有......
我如何正确地将这两个函数结合起来?
from django.utils。从django.utils.safestring导入mark_safe函数导入lazy
从django.utils.translation导入mark_safe
导入ugettext_lazy作为_
$ b mark_safe_lazy = lazy(mark_safe,six.text_type)
然后:
mark_safe_lazy(string_concat('& euro; 0,05',_('per minute')))
这已添加到Django 1.4 docs 。
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)
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?
Add:
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)
And then:
mark_safe_lazy(string_concat('€ 0,05 ', _('per minute')))
This was added to Django 1.4 docs.
这篇关于Django:将懒惰的翻译与模型选择中的安全标记结合起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!