view build_website.py @ 58:524a2538394e

Add AGPLv3
author mathieui
date Sun, 24 May 2020 12:47:31 +0200
parents 010b905a74d4
children d5cfc04b9718
line wrap: on
line source

#!/usr/bin/env python3
'''Locale build script for XAM'''

import sys
from os import makedirs, path
from django.conf import settings
from shutil import copytree, copy

AVAILABLE_LANGUAGES = ['en', 'fr']


def make_messages():
    '''Extract translatable strings from the template.'''

    from django.core.management.commands import makemessages

    command = makemessages.Command()
    command.stdout = sys.stdout
    command.handle(
        locale=AVAILABLE_LANGUAGES, exclude=[], domain='django', verbosity=True,
        all=True, extensions=['xhtml', 'js'], symlinks=False, ignore_patterns=[],
        use_default_ignore_patterns=True, no_wrap=True, no_location=True,
        no_obsolete=True, keep_pot=True, add_location=False
    )


def compile_messages():
    '''For each locale we support, compile the po file into a mo.'''

    from django.core.management.commands import compilemessages

    command = compilemessages.Command()
    command.verbosity = 1
    command.stdout = sys.stdout
    command.compile_messages(('locale/%s/LC_MESSAGES' % lang, 'django.po')
                             for lang in AVAILABLE_LANGUAGES)


def main():
    '''Call everything in the correct order.'''

    from subprocess import call

    settings.configure()
    make_messages()
    compile_messages()

    for language in AVAILABLE_LANGUAGES:
        call([sys.argv[0], language])


def generate_pages(language):
    '''Write a single page, and gzip it for lower network usage.'''

    assert language in AVAILABLE_LANGUAGES
    target_dir = path.join('.', 'build', language)
    makedirs(target_dir, exist_ok=True)

    import django
    from django.template import Template, Context
    import gzip

    settings.configure(LANGUAGE_CODE=language, LOCALE_PATHS=['locale'], TEMPLATES=[{'BACKEND': 'django.template.backends.django.DjangoTemplates'}])

    django.setup()

    static = ['share', 'prosody.css', 'prosody.svg', 'spinner.svg', 'ok.svg', 'error.svg']
    files = [
        'index.xhtml', 'client.js', 'roster.js', 'avatar.js', 'client.js', 'index.xhtml',
        'mam.js', 'nickname.js', 'pep.js', 'roster.js', 'strophe.js', 'strophe.mam.js',
        'strophe.rsm.js', 'util.js', 'vcard.js'
    ]
    for file_to_localize in files:
        with open(file_to_localize) as template_file:
            template = Template('{% load i18n %}' + template_file.read())
        rendered = template.render(Context({'AVAILABLE_LANGUAGES':
                                            AVAILABLE_LANGUAGES}))
        filename = path.join(target_dir, file_to_localize)
        with open(filename, 'w') as out:
            out.write(rendered)
    for static_resource in static:
        if path.isdir(static_resource):
            copytree(static_resource, path.join(target_dir, static_resource), dirs_exist_ok=True)
        else:
            copy(static_resource, target_dir)

if __name__ == '__main__':
    if len(sys.argv) == 1:
        main()
    else:
        generate_pages(sys.argv[1])