1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
8 from django.conf import settings
9 from django.core.management.base import BaseCommand
10 from django.core.management import call_command
11 from wolnelektury.utils import makedirs
12 from .translation2po import get_languages
15 ROOT = settings.ROOT_DIR
19 return mod.__path__[0].startswith(ROOT)
23 def save(self, output_directory, languages):
29 def generate(self, languages):
34 makedirs(os.path.dirname(to))
35 shutil.copyfile(frm, to)
38 class AppLocale(Locale):
39 def __init__(self, appmod):
41 if not os.path.exists(os.path.join(self.path, 'locale')):
42 raise LookupError('No locale for app %s' % appmod)
46 return self.app.__path__[0]
50 return self.app.__name__
52 def save(self, output_directory, languages):
55 if os.path.exists(os.path.join(self.path, 'locale', lc)):
56 copy_f(os.path.join(self.path, 'locale', lc, 'LC_MESSAGES', 'django.po'),
57 os.path.join(output_directory, lc, self.name + '.po'))
59 def load(self, input_directory, languages):
60 for lc in zip(*languages)[0]:
61 if os.path.exists(os.path.join(input_directory, lc, self.name + '.po')):
62 out = os.path.join(self.path, 'locale', lc, 'LC_MESSAGES', 'django.po')
63 makedirs(os.path.dirname(out))
64 copy_f(os.path.join(input_directory, lc, self.name + '.po'), out)
71 call_command('compilemessages', verbosity=0, settings='wolnelektury.settings')
77 def generate(self, languages):
81 call_command('makemessages', all=True)
88 class ModelTranslation(Locale):
89 def __init__(self, appname, poname=None):
90 self.appname = appname
91 self.poname = poname and poname or appname
93 def save(self, output_directory, languages):
94 call_command('translation2po', self.appname, directory=output_directory, poname=self.poname)
96 def load(self, input_directory, languages):
97 call_command('translation2po', self.appname, directory=input_directory,
98 load=True, lang=','.join(zip(*languages)[0]), poname=self.poname, keep_running=True)
103 for appn in settings.INSTALLED_APPS:
104 app = __import__(appn)
107 SOURCES.append(AppLocale(app))
108 except LookupError as e:
109 print("no locales in %s" % app.__name__)
111 SOURCES.append(ModelTranslation('infopages', 'infopages_db'))
114 class Command(BaseCommand):
115 help = 'Make a locale pack'
117 def add_arguments(self, parser):
119 '-l', '--load', help='load locales back to source',
120 action='store_true', dest='load', default=False)
122 '-c', '--compile', help='compile messages',
123 action='store_true', dest='compile', default=False)
125 '-g', '--generate', help='generate messages',
126 action='store_true', dest='generate', default=False)
128 '-L', '--lang', help='load just one language',
129 dest='lang', default=None)
131 '-d', '--directory', help='load from this directory',
132 dest='directory', default=None)
134 '-o', '--outfile', help='Resulting zip file',
135 dest='outfile', default='./wl-locale.zip')
137 '-m', '--merge', action='store_true',
138 dest='merge', default=False,
139 help='Use git to merge. Please use with clean working directory.')
141 '-M', '--message', help='commit message',
142 dest='message', default='New locale')
144 def current_rev(self):
145 return os.popen('git rev-parse HEAD').read()
147 def current_branch(self):
148 return os.popen("git branch |grep '^[*]' | cut -c 3-").read()
150 def save(self, options):
151 packname = options.get('outfile')
152 packname_b = os.path.basename(packname).split('.')[0]
153 fmt = '.'.join(os.path.basename(packname).split('.')[1:])
156 raise NotImplementedError('Sorry. Only zip format supported at the moment.')
158 tmp_dir = tempfile.mkdtemp('-wl-locale')
159 out_dir = os.path.join(tmp_dir, packname_b)
163 for lang in settings.LANGUAGES:
164 os.mkdir(os.path.join(out_dir, lang[0]))
167 src.generate(settings.LANGUAGES)
168 src.save(out_dir, settings.LANGUAGES)
169 # src.save(settings.LANGUAGES)
172 rev = self.current_rev()
173 rf = open(os.path.join(out_dir, '.revision'), 'w')
179 os.chdir(os.path.dirname(out_dir))
180 self.system('zip -r %s %s' % (os.path.join(cwd, packname_b+'.zip'), os.path.basename(out_dir)))
183 # shutil.make_archive(packname_b, fmt, root_dir=os.path.dirname(out_dir),
184 # base_dir=os.path.basename(out_dir))
186 shutil.rmtree(tmp_dir, ignore_errors=True)
190 src.generate(settings.LANGUAGES)
192 def load(self, options):
193 langs = get_languages(options['lang'])
196 src.load(options['directory'], langs)
202 def handle(self, **options):
204 if not options['directory'] or not os.path.exists(options['directory']):
205 print("Directory not provided or does not exist, please use -d")
209 self.merge_setup(options['directory'])
212 self.merge_finish(options['message'])
213 elif options['generate']:
215 elif options['compile']:
220 merge_branch = 'wl-locale-merge'
223 def merge_setup(self, directory):
224 self.last_branch = self.current_branch()
225 rev = open(os.path.join(directory, '.revision')).read()
227 self.system('git checkout -b %s %s' % (self.merge_branch, rev))
229 def merge_finish(self, message):
230 self.system('git commit -a -m "%s"' % message.replace('"', '\\"'))
231 self.system('git checkout %s' % self.last_branch)
232 self.system('git merge -s recursive -X theirs %s' % self.merge_branch)
233 self.system('git branch -d %s' % self.merge_branch)
235 def system(self, fmt, *args):
236 code = os.system(fmt % args)
238 raise OSError('Command %s returned with exit code %d' % (fmt % args, code))