Fix packaging.
[fnpdjango.git] / fnpdjango / management / commands / makecontribmessages.py
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.
4 #
5 from __future__ import print_function
6
7 import os
8 from optparse import make_option
9 from django.core.management.base import BaseCommand
10
11 class Command(BaseCommand):
12     help = 'Builds .po files for contrib apps.'
13
14     def handle(self, **options):
15         from django.conf import settings
16
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.")
19             return
20
21         from subprocess import call
22         import babel
23
24         app_names = settings.CONTRIB_LOCALE_APPS
25         print('L10n for:', ", ".join(app_names))
26         app_dirs = [os.path.dirname(__import__(app).__file__)
27                         for app in app_names]
28         assert settings.LOCALE_PATHS
29         locale_path = settings.LOCALE_PATHS[0]
30         print('Using:', locale_path)
31
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",
38                 "-F", babel_cfg,
39                 "-o", pot_path] + app_dirs)
40
41         # Lose the unneeded absolute file paths in the POT.
42         with open(pot_path) as f:
43             pot = f.read()
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:
47             f.write(pot)
48
49         # Create/update the PO files.
50         call(["pybabel", "update", "-D", "django",
51                 "-i", pot_path,
52                 "-d", locale_path])