You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用django manage.py makemessages命令仅生成.pot文件

实现仅生成.pot文件的可行方案

以下两种方案均可配合你提到的Weblate工作流使用,生成的.pot文件可直接被「Update PO files to match POT (msgmerge)」插件识别,自动完成.po文件的创建更新。

方案1:原生命令配合后置清理(无需修改代码,适合临时使用)

  • 首先执行带--keep-pot参数的makemessages命令,示例:
python manage.py makemessages --keep-pot -l zh_Hans

其中-l参数可指定任意语言,命令执行后会同时生成.pot文件和对应语言的.po文件

  • 执行清理命令删除所有.po及临时备份文件:
    • Unix/macOS环境:
    find locale/ -name "*.po" -o -name "*.po~" | xargs rm -f
    
    • Windows PowerShell环境:
    Get-ChildItem locale/ -Recurse -Include *.po,*.po~ | Remove-Item
    

方案2:自定义Django管理命令(一劳永逸,适合长期使用)

你可以基于Django原生makemessages封装专用命令,每次执行直接输出纯.pot文件,无需额外清理:

  1. 在你的任意Django应用下创建management/commands/makepot.py文件,注意确保management和commands目录下都有__init__.py文件
  2. 写入以下代码:
import os
from django.core.management.commands.makemessages import Command as MakeMessagesCommand

class Command(MakeMessagesCommand):
    help = "仅生成.pot翻译模板文件,不生成.po文件"
    
    def handle(self, *args, **options):
        # 强制开启保留pot配置
        options["keep_pot"] = True
        # 执行原生生成逻辑
        super().handle(*args, **options)
        # 遍历清理所有生成的po文件
        for locale_path in self.locale_paths:
            for root, _, files in os.walk(locale_path):
                for file in files:
                    if file.endswith(".po") or file.endswith(".po~"):
                        os.remove(os.path.join(root, file))
        self.stdout.write(self.style.SUCCESS("成功生成.pot翻译模板文件"))
  1. 后续直接执行以下命令即可得到纯.pot文件:
python manage.py makepot

内容的提问来源于stack exchange,提问作者Stefan_EOX

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 12:39:00