annotate build_website.py @ 50:78f3f79c911b

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