1 # -*- coding: utf-8 -*-
2 # This file is part of FNPDjango, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 from __future__ import print_function
8 from optparse import make_option
9 from django.core.management.base import BaseCommand
11 class Command(BaseCommand):
12 help = 'Builds .po files for contrib apps.'
14 def handle(self, **options):
15 from django.conf import settings
17 if not hasattr(settings, 'CONTRIB_LOCALE_APPS') or not settings.CONTRIB_LOCALE_APPS:
18 print("CONTRIB_LOCALE_APPS not set, no contrib locale needed.")
21 from subprocess import call
24 app_names = settings.CONTRIB_LOCALE_APPS
25 print('L10n for:', ", ".join(app_names))
26 app_dirs = [os.path.dirname(__import__(app).__file__)
28 assert settings.LOCALE_PATHS
29 locale_path = settings.LOCALE_PATHS[0]
30 print('Using:', locale_path)
32 # Create the POT file.
33 babel_cfg = os.path.join(locale_path, "babel.cfg")
34 if not os.path.exists(babel_cfg):
35 babel_cfg = os.path.join(os.path.dirname(__file__), 'babel.cfg')
36 pot_path = os.path.join(locale_path, "django.pot")
37 call(["pybabel", "extract",
39 "-o", pot_path] + app_dirs)
41 # Lose the unneeded absolute file paths in the POT.
42 with open(pot_path) as f:
44 for i, app_dir in enumerate(app_dirs):
45 pot = pot.replace("\n#: " + app_dir, "\n#: " + app_names[i])
46 with open(pot_path, 'w') as f:
49 # Create/update the PO files.
50 call(["pybabel", "update", "-D", "django",