我的模型有这样的属性:
new = models.BooleanField(default=False, verbose_name=_("Is new"),
help_text=_("By default product is marked as new by {0} days since creation.")
.format(settings.PRODUCT_IS_NEW_EXPIRATION_DAYS))
这不幸适用于runserver命令但是当我想运行测试时它会失败并且我收到以下错误:
django.core.exceptions.AppRegistryNotReady:在应用注册表准备就绪之前,无法初始化转换基础结构。检查您是否在导入时不进行非延迟的gettext调用。
此错误很明显。我正准备在应用程序准备好之前使用翻译。
可以在导入时进行需要字符串格式化的翻译吗?
编辑: 回溯:
File "./manage.py", line 9, in <module>
execute_from_command_line(sys.argv)
File "django/core/management/__init__.py", line 354, in execute_from_command_line
utility.execute()
File "django/core/management/__init__.py", line 328, in execute
django.setup()
File "django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "django/apps/config.py", line 86, in create
module = import_module(entry)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "haystack/__init__.py", line 57, in <module>
signal_processor_class = loading.import_class(signal_processor_path)
File "haystack/utils/loading.py", line 32, in import_class
module_itself = importlib.import_module(module_path)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "utils/signals.py", line 12, in <module>
from products.models import Products
File "products/models/__init__.py", line 2, in <module>
from .carousel import ProductCarousel, ProductCarouselImage
File "products/models/carousel.py", line 10, in <module>
from products.models.product import Products
File "products/models/product.py", line 210, in <module>
class Products(models.Model):
File "products/models/product.py", line 241, in Products
help_text=(ugettext("By default product is marked as new by {0} days since creation.")).format(
File "django/utils/translation/__init__.py", line 84, in ugettext
return _trans.ugettext(message)
File "django/utils/translation/trans_real.py", line 330, in ugettext
return do_translate(message, 'ugettext')
File "django/utils/translation/trans_real.py", line 307, in do_translate
_default = _default or translation(settings.LANGUAGE_CODE)
File "django/utils/translation/trans_real.py", line 209, in translation
_translations[language] = DjangoTranslation(language)
File "django/utils/translation/trans_real.py", line 118, in __init__
self._add_installed_apps_translations()
File "django/utils/translation/trans_real.py", line 159, in _add_installed_apps_translations
"The translation infrastructure cannot be initialized before the "
django.core.exceptions.AppRegistryNotReady: The translation infrastructure cannot be initialized before the apps registry is ready. Check that you don't make non-lazy gettext calls at import time.
答案 0 :(得分:0)
我试图评论这篇文章,但我不能,因为我没有足够的声誉。您是否先尝试格式化help_text然后进行翻译?像这样:
help_text = _(
"By default product is marked as new by {} days since creation.".format(
settings.PRODUCT_IS_NEW_EXPIRATION_DAYS
)
)
new = models.BooleanField(default=False, verbose_name=_("Is new"),
help_text=help_text)
注意: .format在ugettext
中