Django翻译compilemessages错误:'msgstr' 不是有效的Python格式字符串

7 投票
1 回答
4936 浏览
提问于 2025-04-18 05:16

我遇到了一个错误,原因是因为有一个%符号。有没有什么办法可以解决这个错误呢?

我在运行这个命令:django-admin.py compilemessages -l tr

它在处理这个文件:django.po,路径是/.../locale/tr/LC_MESSAGES

出现了一个错误:CommandError: 执行msgfmt失败:在文件/.../locale/tr/LC_MESSAGES/django.po的第139行,'msgstr' 不是一个有效的Python格式字符串,和'msgid'不一样。原因是:在指令的第1个位置,字符'{' 不是一个有效的转换说明符。

在/.../locale/tr/LC_MESSAGES/django.po的第146行,'msgstr' 也不是一个有效的Python格式字符串,和'msgid'不一样。原因同样是:在指令的第1个位置,字符'{' 不是一个有效的转换说明符。

msgfmt: 找到了2个致命错误

这是我的代码文件:models.py

def get_full_title(self):
    return _(u"{discounted_price} TL for {original_price} TL worth of {product_name} at {place_name} {locality} {city} ({discount_percentage}% off)").format(
        discounted_price=int(self.discounted_price),
        original_price=int(self.original_price),
        product_name=self.product.name,
        place_name=self.product.place.name,
        locality=self.product.place.locality,
        city=self.product.place.get_city_display(),
        discount_percentage=self.get_rounded_discount_percentage(),
    )


def get_short_title(self):
    return _(u"{product_name} at {place_name} ({discount_percentage}% off)").format(
        product_name=self.product.name,
        place_name=self.product.place.name,
        discount_percentage=self.get_rounded_discount_percentage(),
    )

这是我的翻译文件:django.po

#: deals/models.py:169
#, python-format, python-brace-format
msgid ""
"{discounted_price} TL for {original_price} TL worth of {product_name} at "
"{place_name} {locality} {city} ({discount_percentage}% off)"
msgstr ""
"{discounted_price} TL for {original_price} TL worth of {product_name} at "
"{place_name} {locality} {city} (%{discount_percentage} indirim)"

#: deals/models.py:181
#, python-format, python-brace-format
msgid "{product_name} at {place_name} ({discount_percentage}% off)"
msgstr "{product_name} at {place_name} (%{discount_percentage} indirim)"

1 个回答

13

你可以通过把%写成两个%%来进行转义。

撰写回答