comparison build_website.py @ 49:d3b943ac148f

Add translations Build script + {% trans %} in index.html
author mathieui
date Sat, 23 May 2020 20:33:38 +0200
parents
children 78f3f79c911b
comparison
equal deleted inserted replaced
48:021185105e2f 49:d3b943ac148f
1 #!/usr/bin/env python3
2 '''Locale build script for XAM'''
3
4 import sys
5 from os import makedirs, path
6 from django.conf import settings
7 from shutil import copytree, copy
8
9 AVAILABLE_LANGUAGES = ['en', 'fr']
10
11
12 def make_messages():
13 '''Extract translatable strings from the template.'''
14
15 from django.core.management.commands import makemessages
16
17 command = makemessages.Command()
18 command.stdout = sys.stdout
19 command.handle(
20 locale=AVAILABLE_LANGUAGES, exclude=[], domain='django', verbosity=True,
21 all=True, extensions=['xhtml', 'js'], symlinks=False, ignore_patterns=[],
22 use_default_ignore_patterns=True, no_wrap=True, no_location=True,
23 no_obsolete=True, keep_pot=True, add_location=False
24 )
25
26
27 def compile_messages():
28 '''For each locale we support, compile the po file into a mo.'''
29
30 from django.core.management.commands import compilemessages
31
32 command = compilemessages.Command()
33 command.verbosity = 1
34 command.stdout = sys.stdout
35 command.compile_messages(('locale/%s/LC_MESSAGES' % lang, 'django.po')
36 for lang in AVAILABLE_LANGUAGES)
37
38
39 def main():
40 '''Call everything in the correct order.'''
41
42 from subprocess import call
43
44 settings.configure()
45 make_messages()
46 compile_messages()
47
48 for language in AVAILABLE_LANGUAGES:
49 call([sys.argv[0], language])
50
51
52 def generate_pages(language):
53 '''Write a single page, and gzip it for lower network usage.'''
54
55 assert language in AVAILABLE_LANGUAGES
56 target_dir = path.join('.', 'build', language)
57 makedirs(target_dir, exist_ok=True)
58
59 import django
60 from django.template import Template, Context
61 import gzip
62
63 settings.configure(LANGUAGE_CODE=language, LOCALE_PATHS=['locale'], TEMPLATES=[{'BACKEND': 'django.template.backends.django.DjangoTemplates'}])
64
65 django.setup()
66
67 static = ['share', 'prosody.css', 'prosody.svg', 'spinner.svg', 'ok.svg', 'error.svg']
68 files = [
69 'index.xhtml', 'client.js', 'roster.js', 'avatar.js', 'client.js', 'index.xhtml',
70 'mam.js', 'nickname.js', 'pep.js', 'roster.js', 'strophe.js', 'strophe.mam.js',
71 'strophe.rsm.js', 'util.js', 'vcard.js'
72 ]
73 for file_to_localize in files:
74 with open(file_to_localize) as template_file:
75 template = Template(template_file.read())
76 rendered = template.render(Context({'AVAILABLE_LANGUAGES':
77 AVAILABLE_LANGUAGES}))
78 filename = path.join(target_dir, file_to_localize)
79 with open(filename, 'w') as out:
80 out.write(rendered)
81 for static_resource in static:
82 if path.isdir(static_resource):
83 copytree(static_resource, target_dir, dirs_exist_ok=True)
84 else:
85 copy(static_resource, target_dir)
86
87 if __name__ == '__main__':
88 if len(sys.argv) == 1:
89 main()
90 else:
91 generate_pages(sys.argv[1])