Django makemessages混淆翻译条目生成模糊PO条目问题求助
解决Django makemessages生成模糊翻译条目的问题(保留现有翻译)
问题场景
在Windows系统使用Django 5.0.3执行python manage.py makemessages后,django.po出现错误关联的模糊条目:
#: .\homepage\models.py:40
#, fuzzy
#| msgid "The logo of the site as appears on the top of each page."
msgid "The logo of the brand as appears on the homepage."
msgstr "لوگوی سایت که بالای هر صفحه نمایش داده میشود."
其中#|注释的旧msgid属于core应用models.py第20行,却被错误关联到homepage应用的新msgid上,且原core条目在PO文件中缺失,翻译文本也对应旧msgid。
解决步骤(无需删除PO文件)
1. 修正错误的模糊条目
直接编辑django.po文件中的该条目:
- 删除
, fuzzy标记,取消模糊状态 - 删除旧msgid的注释行
#| msgid "The logo of the site as appears on the top of each page." - 更新
msgstr为新msgid对应的翻译(比如改为对应品牌logo主页显示的波斯语翻译,若暂未完成可留空,但需确保与新msgid匹配)
修正后条目应为:
#: .\homepage\models.py:40 msgid "The logo of the brand as appears on the homepage." msgstr "[新的对应翻译文本]"
2. 恢复缺失的core应用翻译条目
在django.po文件中添加原core应用的条目(可放在同应用相关条目附近或按msgid排序位置):
#: .\core\models.py:20 msgid "The logo of the site as appears on the top of each page." msgstr "لوگوی سایت که بالای هر صفحه نمایش داده میشود."
3. 编译验证翻译
执行以下命令编译翻译,确保无错误:
python manage.py compilemessages
之后启动应用,检查两个翻译是否正确显示。
预防后续出现同类问题
为避免gettext混淆相似字符串,给不同应用的相似msgid添加上下文标记:
- 在
core/models.py中使用pgettext:from django.utils.translation import pgettext pgettext("core_site_logo", "The logo of the site as appears on the top of each page.") - 在
homepage/models.py中使用:from django.utils.translation import pgettext pgettext("homepage_brand_logo", "The logo of the brand as appears on the homepage.")
这样gettext会将两个字符串视为独立条目,不会错误关联。
内容的提问来源于stack exchange,提问作者Omid Shojaee
相关产品推荐
相关产品推荐

