1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
9 from django.conf import settings
10 from django.core.management.base import BaseCommand
11 from django.core.management import call_command
12 from wolnelektury.utils import makedirs
13 from .translation2po import get_languages
16 ROOT = settings.ROOT_DIR
20 return mod.__path__[0].startswith(ROOT)
24 def save(self, output_directory, languages):
30 def generate(self, languages):
35 makedirs(os.path.dirname(to))
36 shutil.copyfile(frm, to)
39 class AppLocale(Locale):
40 def __init__(self, appmod):
42 if not os.path.exists(os.path.join(self.path, 'locale')):
43 raise LookupError('No locale for app %s' % appmod)
47 return self.app.__path__[0]
51 return self.app.__name__
53 def save(self, output_directory, languages):
56 if os.path.exists(os.path.join(self.path, 'locale', lc)):
57 copy_f(os.path.join(self.path, 'locale', lc, 'LC_MESSAGES', 'django.po'),
58 os.path.join(output_directory, lc, self.name + '.po'))
60 def load(self, input_directory, languages):
61 for lc in zip(*languages)[0]:
62 if os.path.exists(os.path.join(input_directory, lc, self.name + '.po')):
63 out = os.path.join(self.path, 'locale', lc, 'LC_MESSAGES', 'django.po')
64 makedirs(os.path.dirname(out))
65 copy_f(os.path.join(input_directory, lc, self.name + '.po'), out)
72 call_command('compilemessages', verbosity=0, settings='wolnelektury.settings')
78 def generate(self, languages):
82 call_command('makemessages', all=True)
89 class ModelTranslation(Locale):
90 def __init__(self, appname, poname=None):
91 self.appname = appname
92 self.poname = poname and poname or appname
94 def save(self, output_directory, languages):
95 call_command('translation2po', self.appname, directory=output_directory, poname=self.poname)
97 def load(self, input_directory, languages):
98 call_command('translation2po', self.appname, directory=input_directory,
99 load=True, lang=','.join(zip(*languages)[0]), poname=self.poname, keep_running=True)
102 class CustomLocale(Locale):
103 def __init__(self, app_dir,
104 config=os.path.join(ROOT, "babel.cfg"),
105 out_file=os.path.join(ROOT, 'src/wolnelektury/locale-contrib/django.pot'),
107 self.app_dir = app_dir
109 self.out_file = out_file
112 def generate(self, languages):
113 os.system('pybabel extract -F "%s" -o "%s" "%s"' % (self.config, self.out_file, self.app_dir))
114 os.system('pybabel update -D django -i %s -d %s' % (self.out_file, os.path.dirname(self.out_file)))
116 def po_file(self, language):
117 d = os.path.dirname(self.out_file)
118 n = os.path.basename(self.out_file).split('.')[0]
119 return os.path.join(d, language, 'LC_MESSAGES', n + '.po')
121 def save(self, output_directory, languages):
122 for lc in zip(*languages)[0]:
123 if os.path.exists(self.po_file(lc)):
124 copy_f(self.po_file(lc),
125 os.path.join(output_directory, lc, self.name + '.po'))
127 def load(self, input_directory, languages):
128 for lc in zip(*languages)[0]:
129 copy_f(os.path.join(input_directory, lc, self.name + '.po'),
134 os.system('pybabel compile -D django -d %s' % os.path.dirname(self.out_file))
139 for appn in settings.INSTALLED_APPS:
140 app = __import__(appn)
143 SOURCES.append(AppLocale(app))
144 except LookupError as e:
145 print("no locales in %s" % app.__name__)
147 SOURCES.append(ModelTranslation('infopages', 'infopages_db'))
148 SOURCES.append(CustomLocale(os.path.dirname(allauth.__file__), name='contrib'))
151 class Command(BaseCommand):
152 help = 'Make a locale pack'
154 def add_arguments(self, parser):
156 '-l', '--load', help='load locales back to source',
157 action='store_true', dest='load', default=False)
159 '-c', '--compile', help='compile messages',
160 action='store_true', dest='compile', default=False)
162 '-g', '--generate', help='generate messages',
163 action='store_true', dest='generate', default=False)
165 '-L', '--lang', help='load just one language',
166 dest='lang', default=None)
168 '-d', '--directory', help='load from this directory',
169 dest='directory', default=None)
171 '-o', '--outfile', help='Resulting zip file',
172 dest='outfile', default='./wl-locale.zip')
174 '-m', '--merge', action='store_true',
175 dest='merge', default=False,
176 help='Use git to merge. Please use with clean working directory.')
178 '-M', '--message', help='commit message',
179 dest='message', default='New locale')
181 def current_rev(self):
182 return os.popen('git rev-parse HEAD').read()
184 def current_branch(self):
185 return os.popen("git branch |grep '^[*]' | cut -c 3-").read()
187 def save(self, options):
188 packname = options.get('outfile')
189 packname_b = os.path.basename(packname).split('.')[0]
190 fmt = '.'.join(os.path.basename(packname).split('.')[1:])
193 raise NotImplementedError('Sorry. Only zip format supported at the moment.')
195 tmp_dir = tempfile.mkdtemp('-wl-locale')
196 out_dir = os.path.join(tmp_dir, packname_b)
200 for lang in settings.LANGUAGES:
201 os.mkdir(os.path.join(out_dir, lang[0]))
204 src.generate(settings.LANGUAGES)
205 src.save(out_dir, settings.LANGUAGES)
206 # src.save(settings.LANGUAGES)
209 rev = self.current_rev()
210 rf = open(os.path.join(out_dir, '.revision'), 'w')
216 os.chdir(os.path.dirname(out_dir))
217 self.system('zip -r %s %s' % (os.path.join(cwd, packname_b+'.zip'), os.path.basename(out_dir)))
220 # shutil.make_archive(packname_b, fmt, root_dir=os.path.dirname(out_dir),
221 # base_dir=os.path.basename(out_dir))
223 shutil.rmtree(tmp_dir, ignore_errors=True)
227 src.generate(settings.LANGUAGES)
229 def load(self, options):
230 langs = get_languages(options['lang'])
233 src.load(options['directory'], langs)
239 def handle(self, **options):
241 if not options['directory'] or not os.path.exists(options['directory']):
242 print("Directory not provided or does not exist, please use -d")
246 self.merge_setup(options['directory'])
249 self.merge_finish(options['message'])
250 elif options['generate']:
252 elif options['compile']:
257 merge_branch = 'wl-locale-merge'
260 def merge_setup(self, directory):
261 self.last_branch = self.current_branch()
262 rev = open(os.path.join(directory, '.revision')).read()
264 self.system('git checkout -b %s %s' % (self.merge_branch, rev))
266 def merge_finish(self, message):
267 self.system('git commit -a -m "%s"' % message.replace('"', '\\"'))
268 self.system('git checkout %s' % self.last_branch)
269 self.system('git merge -s recursive -X theirs %s' % self.merge_branch)
270 self.system('git branch -d %s' % self.merge_branch)
272 def system(self, fmt, *args):
273 code = os.system(fmt % args)
275 raise OSError('Command %s returned with exit code %d' % (fmt % args, code))