Exemplo n.º 1
0
def get_jinja_env(template_loader, locale):
    """
    Set up the Jinja environment,

    (In the future we may have another system for providing theming;
    for now this is good enough.)
    """
    set_thread_locale(locale)

    # If we have a jinja environment set up with this locale, just
    # return that one.
    if locale in SETUP_JINJA_ENVS:
        return SETUP_JINJA_ENVS[locale]

    # jinja2.StrictUndefined will give exceptions on references
    # to undefined/unknown variables in templates.
    template_env = jinja2.Environment(
        loader=template_loader,
        autoescape=True,
        undefined=jinja2.StrictUndefined,
        extensions=["jinja2.ext.i18n", "jinja2.ext.autoescape", TemplateHookExtension],
    )

    template_env.install_gettext_callables(
        mg_globals.thread_scope.translations.ugettext, mg_globals.thread_scope.translations.ungettext
    )

    # All templates will know how to ...
    # ... fetch all waiting messages and remove them from the queue
    # ... construct a grid of thumbnails or other media
    # ... have access to the global and app config
    template_env.globals["fetch_messages"] = messages.fetch_messages
    template_env.globals["app_config"] = mg_globals.app_config
    template_env.globals["global_config"] = mg_globals.global_config
    template_env.globals["version"] = _version.__version__
    template_env.globals["auth"] = mg_globals.app.auth

    template_env.filters["urlencode"] = url_quote_plus

    # add human readable fuzzy date time
    template_env.globals["timesince"] = timesince

    # allow for hooking up plugin templates
    template_env.globals["get_hook_templates"] = get_hook_templates

    template_env.globals = hook_transform("template_global_context", template_env.globals)

    if exists(locale):
        SETUP_JINJA_ENVS[locale] = template_env

    return template_env
Exemplo n.º 2
0
def test_gettext_lazy_proxy():
    from mediagoblin.tools.translate import lazy_pass_to_ugettext as _
    from mediagoblin.tools.translate import pass_to_ugettext, set_thread_locale
    proxy = _(u"Password")
    orig = u"Password"

    set_thread_locale("es")
    p1 = unicode(proxy)
    p1_should = pass_to_ugettext(orig)
    assert p1_should != orig, "Test useless, string not translated"
    assert p1 == p1_should

    set_thread_locale("sv")
    p2 = unicode(proxy)
    p2_should = pass_to_ugettext(orig)
    assert p2_should != orig, "Test broken, string not translated"
    assert p2 == p2_should

    assert p1_should != p2_should, "Test broken, same translated string"
    assert p1 != p2
Exemplo n.º 3
0
def test_gettext_lazy_proxy():
    from mediagoblin.tools.translate import lazy_pass_to_ugettext as _
    from mediagoblin.tools.translate import pass_to_ugettext, set_thread_locale
    proxy = _(u"Password")
    orig = u"Password"

    set_thread_locale("es")
    p1 = unicode(proxy)
    p1_should = pass_to_ugettext(orig)
    assert p1_should != orig, "Test useless, string not translated"
    assert p1 == p1_should

    set_thread_locale("sv")
    p2 = unicode(proxy)
    p2_should = pass_to_ugettext(orig)
    assert p2_should != orig, "Test broken, string not translated"
    assert p2 == p2_should

    assert p1_should != p2_should, "Test broken, same translated string"
    assert p1 != p2
Exemplo n.º 4
0
def get_jinja_env(template_loader, locale):
    """
    Set up the Jinja environment,

    (In the future we may have another system for providing theming;
    for now this is good enough.)
    """
    set_thread_locale(locale)

    # If we have a jinja environment set up with this locale, just
    # return that one.
    if locale in SETUP_JINJA_ENVS:
        return SETUP_JINJA_ENVS[locale]

    # The default config does not require a [jinja2] block.
    # You may create one if you wish to enable additional jinja2 extensions,
    # see example in config_spec.ini
    jinja2_config = mg_globals.global_config.get('jinja2', {})
    local_exts = jinja2_config.get('extensions', [])

    # jinja2.StrictUndefined will give exceptions on references
    # to undefined/unknown variables in templates.
    template_env = jinja2.Environment(
        loader=template_loader,
        autoescape=True,
        undefined=jinja2.StrictUndefined,
        extensions=[
            'jinja2.ext.i18n', 'jinja2.ext.autoescape', TemplateHookExtension
        ] + local_exts)

    template_env.install_gettext_callables(
        mg_globals.thread_scope.translations.ugettext,
        mg_globals.thread_scope.translations.ungettext)

    # All templates will know how to ...
    # ... fetch all waiting messages and remove them from the queue
    # ... construct a grid of thumbnails or other media
    # ... have access to the global and app config
    template_env.globals['fetch_messages'] = messages.fetch_messages
    template_env.globals['app_config'] = mg_globals.app_config
    template_env.globals['global_config'] = mg_globals.global_config
    template_env.globals['version'] = _version.__version__
    template_env.globals['auth'] = mg_globals.app.auth

    template_env.filters['urlencode'] = url_quote_plus

    # add human readable fuzzy date time
    template_env.globals['timesince'] = timesince

    # allow for hooking up plugin templates
    template_env.globals['get_hook_templates'] = get_hook_templates

    template_env.globals = hook_transform('template_global_context',
                                          template_env.globals)

    #### THIS IS TEMPORARY, PLEASE FIX IT
    ## Notifications stuff is not yet a plugin (and we're not sure it will be),
    ## but it needs to add stuff to the context.  This is THE WRONG WAY TO DO IT
    from mediagoblin import notifications
    template_env.globals['get_notifications'] = notifications.get_notifications
    template_env.globals[
        'get_notification_count'] = notifications.get_notification_count
    template_env.globals[
        'get_comment_subscription'] = notifications.get_comment_subscription

    if exists(locale):
        SETUP_JINJA_ENVS[locale] = template_env

    return template_env
Exemplo n.º 5
0
def get_jinja_env(app, template_loader, locale):
    """
    Set up the Jinja environment,

    (In the future we may have another system for providing theming;
    for now this is good enough.)
    """
    set_thread_locale(locale)

    # If we have a jinja environment set up with this locale, just
    # return that one.
    if locale in SETUP_JINJA_ENVS:
        return SETUP_JINJA_ENVS[locale]

    # The default config does not require a [jinja2] block.
    # You may create one if you wish to enable additional jinja2 extensions,
    # see example in config_spec.ini 
    jinja2_config = app.global_config.get('jinja2', {})
    local_exts = jinja2_config.get('extensions', [])

    # jinja2.StrictUndefined will give exceptions on references
    # to undefined/unknown variables in templates.
    template_env = jinja2.Environment(
        loader=template_loader, autoescape=True,
        undefined=jinja2.StrictUndefined,
        extensions=[
            'jinja2.ext.i18n', 'jinja2.ext.autoescape',
            TemplateHookExtension] + local_exts)

    if six.PY2:
        template_env.install_gettext_callables(mg_globals.thread_scope.translations.ugettext,
                                           mg_globals.thread_scope.translations.ungettext)
    else:
        template_env.install_gettext_callables(mg_globals.thread_scope.translations.gettext,
                                           mg_globals.thread_scope.translations.ngettext)

    # All templates will know how to ...
    # ... fetch all waiting messages and remove them from the queue
    # ... construct a grid of thumbnails or other media
    # ... have access to the global and app config
    # ... determine if the language is rtl or ltr
    template_env.globals['fetch_messages'] = messages.fetch_messages
    template_env.globals['app_config'] = app.app_config
    template_env.globals['global_config'] = app.global_config
    template_env.globals['version'] = _version.__version__
    template_env.globals['auth'] = app.auth
    template_env.globals['is_rtl'] = is_rtl(locale)
    template_env.filters['urlencode'] = url_quote_plus

    # add human readable fuzzy date time
    template_env.globals['timesince'] = timesince

    # allow for hooking up plugin templates
    template_env.globals['get_hook_templates'] = get_hook_templates

    template_env.globals = hook_transform(
        'template_global_context', template_env.globals)

    #### THIS IS TEMPORARY, PLEASE FIX IT
    ## Notifications stuff is not yet a plugin (and we're not sure it will be),
    ## but it needs to add stuff to the context.  This is THE WRONG WAY TO DO IT
    from mediagoblin import notifications
    template_env.globals['get_notifications'] = notifications.get_notifications
    template_env.globals[
        'get_notification_count'] = notifications.get_notification_count
    template_env.globals[
        'get_comment_subscription'] = notifications.get_comment_subscription

    if exists(locale):
        SETUP_JINJA_ENVS[locale] = template_env

    return template_env