Пример #1
0
def get_env():
    """Configure and return a jinja2 Environment."""
    # Mimic Django's setup by loading templates from directories in
    # TEMPLATE_DIRS and packages in INSTALLED_APPS.
    x = ((jinja2.FileSystemLoader, settings.TEMPLATE_DIRS),
         (jinja2.PackageLoader, settings.INSTALLED_APPS))
    loaders = [loader(p) for loader, places in x for p in places]

    opts = {'trim_blocks': True,
            'extensions': ['jinja2.ext.i18n'],
            'autoescape': True,
            'auto_reload': settings.DEBUG,
            'loader': jinja2.ChoiceLoader(loaders),
            }

    if hasattr(settings, 'JINJA_CONFIG'):
        if hasattr(settings.JINJA_CONFIG, '__call__'):
            config = settings.JINJA_CONFIG()
        else:
            config = settings.JINJA_CONFIG
        opts.update(config)

    e = Environment(**opts)
    # Install null translations since gettext isn't always loaded up during
    # testing.
    if ('jinja2.ext.i18n' in e.extensions or
            'jinja2.ext.InternationalizationExtension' in e.extensions):
        e.install_null_translations()
    return e
Пример #2
0
def get_env():
    """Configure and return a jinja2 Environment."""
    # Mimic Django's setup by loading templates from directories in
    # TEMPLATE_DIRS and packages in INSTALLED_APPS.
    x = ((jinja2.FileSystemLoader, settings.TEMPLATE_DIRS),
         (jinja2.PackageLoader, settings.INSTALLED_APPS))
    loaders = []
    for loader, places in x:
        for p in places:
            try:
                loader(p)
            except ImportError:
                # Django 1.7 allows for speciying a path to an AppConfig class
                if django.VERSION[:2] >= (1, 7):
                    from django.apps import AppConfig
                    app_config = AppConfig.create(p)
                    p = app_config.name
            finally:
                loaders.append(loader(p))

    opts = {
        'trim_blocks': True,
        'extensions': ['jinja2.ext.i18n'],
        'autoescape': True,
        'auto_reload': settings.DEBUG,
        'loader': jinja2.ChoiceLoader(loaders),
    }

    if hasattr(settings, 'JINJA_CONFIG'):
        if hasattr(settings.JINJA_CONFIG, '__call__'):
            config = settings.JINJA_CONFIG()
        else:
            config = settings.JINJA_CONFIG
        opts.update(config)

    e = Environment(**opts)
    # Install null translations since gettext isn't always loaded up during
    # testing.
    if ('jinja2.ext.i18n' in e.extensions
            or 'jinja2.ext.InternationalizationExtension' in e.extensions):
        e.install_null_translations()
    return e
Пример #3
0
from babel.messages.extract import (DEFAULT_KEYWORDS, extract_from_dir)
from translate.storage import po

from tower import split_context

# These are here for backwards compatibility with older iterations of
# tower.
from tower import extract_tower_python, extract_tower_template

DEFAULT_DOMAIN = 'all'

TEXT_DOMAIN = getattr(settings, 'TEXT_DOMAIN', 'messages')

# JINJA_CONFIG can be a callable or a dict.
if hasattr(settings.JINJA_CONFIG, '__call__'):
    JINJA_CONFIG = settings.JINJA_CONFIG()
else:
    JINJA_CONFIG = settings.JINJA_CONFIG

# By default, all the domains you speficy will be merged into one big
# messages.po file.  If you want to separate a domain from the main .po file,
# specify it in this list.  Make sure to include TEXT_DOMAIN in this list, even
# if you have other .po files you're generating
try:
    standalone_domains = settings.STANDALONE_DOMAINS
except AttributeError:
    standalone_domains = [TEXT_DOMAIN]

TOWER_KEYWORDS = dict(DEFAULT_KEYWORDS)

if hasattr(settings, 'TOWER_KEYWORDS'):