2 from optparse import make_option
3 from django.conf import settings
4 from django.core.management.base import BaseCommand
5 from django.core.management.color import color_style
6 from django.core.management import call_command
14 ROOT = os.path.dirname(settings.PROJECT_DIR)
18 return mod.__path__[0].startswith(ROOT)
22 def save(self, output_directory, languages):
25 def generate(self, languages):
29 class AppLocale(Locale):
30 def __init__(self, appmod):
32 if not os.path.exists(os.path.join(self.path, 'locale')):
33 raise LookupError('No locale for app %s' % appmod)
37 return self.app.__path__[0]
41 return self.app.__name__
43 def save(self, output_directory, languages):
46 if os.path.exists(os.path.join(self.path, 'locale', lc)):
47 shutil.copy2(os.path.join(self.path, 'locale', lc, 'LC_MESSAGES', 'django.po'),
48 os.path.join(output_directory, lc, self.name + '.po'))
50 def load(self, input_directory, languages):
51 for lc in zip(*languages)[0]:
52 shutil.copy2(os.path.join(input_directory, lc, self.name + '.po'),
53 os.path.join(self.path, 'locale', lc, 'LC_MESSAGES', 'django.po'))
55 def generate(self, languages):
57 print "in %s" % os.getcwd()
59 call_command('makemessages', all=True)
64 class ModelTranslation(Locale):
65 def __init__(self, appname):
66 self.appname = appname
68 def save(self, output_directory, languages):
69 call_command('translation2po', self.appname, directory=output_directory)
71 def load(self, input_directory, languages):
72 call_command('translation2po', self.appname, directory=input_directory, load=True)
75 class CustomLocale(Locale):
76 def __init__(self, app_dir,
77 config=os.path.join(ROOT, "babel.cfg"),
78 out_file=os.path.join(ROOT, 'wolnelektury/locale-contrib/django.pot'),
80 self.app_dir = app_dir
82 self.out_file = out_file
85 def generate(self, languages):
86 os.system('pybabel extract -F "%s" -o "%s" "%s"' % (self.config, self.out_file, self.app_dir))
87 os.system('pybabel update -D django -i %s -d %s' % (self.out_file, os.path.dirname(self.out_file)))
89 def po_file(self, language):
90 d = os.path.dirname(self.out_file)
91 n = os.path.basename(self.out_file).split('.')[0]
92 return os.path.join(d, language, 'LC_MESSAGES', n + '.po')
94 def save(self, output_directory, languages):
95 for lc in zip(*languages)[0]:
96 if os.path.exists(self.po_file(lc)):
97 shutil.copy2(self.po_file(lc),
98 os.path.join(output_directory, lc, self.name + '.po'))
100 def load(self, input_directory, languages):
101 for lc in zip(*languages)[0]:
102 shutil.copy2(os.path.join(input_directory, lc, self.name + '.po'),
104 os.system('pybabel compile -D django -d %s' % os.dirname(self.out_file))
109 for appn in settings.INSTALLED_APPS:
110 app = __import__(appn)
113 SOURCES.append(AppLocale(app))
114 except LookupError, e:
115 print "no locales in %s" % app
117 SOURCES.append(ModelTranslation('infopages'))
118 SOURCES.append(CustomLocale(os.path.dirname(allauth.__file__), name='contrib'))
121 class Command(BaseCommand):
122 option_list = BaseCommand.option_list + (
123 make_option('-l', '--load', help='load locales back to source', action='store_true', dest='load', default=False),
124 make_option('-o', '--outfile', help='Resulting zip file', dest='outfile', default='./wl-locale.zip'),
126 help = 'Make a locale pack'
129 def handle(self, *a, **options):
130 tmp_dir = tempfile.mkdtemp('-wl-locale')
131 out_dir = os.path.join(tmp_dir, 'wl-locale')
135 for lang in settings.LANGUAGES:
136 os.mkdir(os.path.join(out_dir, lang[0]))
139 src.generate(settings.LANGUAGES)
140 src.save(out_dir, settings.LANGUAGES)
141 # src.save(settings.LANGUAGES)
143 packname = options.get('outfile')
144 packname_b = os.path.basename(packname).split('.')[0]
145 fmt = '.'.join(os.path.basename(packname).split('.')[1:])
146 shutil.make_archive(packname_b, fmt, root_dir=os.path.dirname(out_dir), base_dir=os.path.basename(out_dir))
148 shutil.rmtree(tmp_dir, ignore_errors=True)