[英]Django tuple/choices field is not translating
I have an Integer ModelField with a set of choices.我有一个带有一组选择的整数模型字段。
field = models.IntegerField(choices=CHOICES_CONSTANT)
This CHOICES_CONSTANT
is defined in another file and imported此CHOICES_CONSTANT
在另一个文件中定义并导入
CHOICES_CONSTANT = (
(0, _('One')),
(1, _('Two')),
...
)
The _
is from from django.utils.translation import ugettext_lazy as _
as the model field requires you to use lazy translations. _
来自from django.utils.translation import ugettext_lazy as _
因为模型字段要求您使用延迟翻译。
I also import the CHOICES_CONSTANT
into other files (such as reports) where in a report I might say str(dict(CHOICE_CONSTANT)[object.field]) if object.field else ''
.我还将CHOICES_CONSTANT
导入其他文件(例如报告),在报告中我可能会说str(dict(CHOICE_CONSTANT)[object.field]) if object.field else ''
。
This str(dict(CHOICES_CONSTANT)[object.field]) if object.field else ''
does not get translated.这个str(dict(CHOICES_CONSTANT)[object.field]) if object.field else ''
没有被翻译。 In fact, if I just print(CHOICES_CONSTANT)
it immediately evaluates to the default language (English).事实上,如果我只是print(CHOICES_CONSTANT)
它会立即评估为默认语言(英语)。
In other places I would do object.get_field_display()
and that also does not translate.在其他地方我会做object.get_field_display()
并且这也不会翻译。
Any help would be much appreciated.任何帮助将非常感激。
I spent too long on this looking in the wrong direction...我在这个错误的方向上花了太长时间......
The lazy loaded translations were working correctly.延迟加载的翻译工作正常。 The issue was that LOCALE_PATHS
in settings.py
did not include the root directory locale/
folder.问题是, LOCALE_PATHS
在settings.py
不包括根目录下locale/
文件夹。 The constants CHOICES_CONSTANT
and others were store in the project directory, rather than an app so the app was not getting the translations from the root directory.常量CHOICES_CONSTANT
和其他常量存储在项目目录中,而不是应用程序中,因此应用程序不会从根目录获取翻译。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.