"""Util functions to render a template.""" from babel.support import Translations from htmlmin.minify import html_minify from jinja2 import Environment from jinja2 import FileSystemLoader import config env = Environment( loader=FileSystemLoader(config.TEMPLATE_PATH), extensions=['jinja2.ext.i18n']) def render_template(template_name, context, user_locale): """Render a Jinja2 template. Args: template_name (str): the template name context (dict): the context to extend the template user_locale (str): the user's locale (e.g. "en") Returns: str: the rendered template """ translations = Translations.load(config.I18N_FOLDER_NAME, [user_locale]) env.install_gettext_translations(translations) template_filename = template_name + '.html' template = env.get_template(template_filename) return html_minify(template.render(**context))