コード例 #1
0
ファイル: gettext.py プロジェクト: chrisjames-work/bedrock
def template_is_active(path, lang):
    """Given a template path, determine if it should be active for a locale.

    It is active if either the template's lang file, or the lang file
    specified in the "set_lang_files" template tag has the active tag.

    :param path: relative path to the template.
    :param lang: language code
    :return: boolean
    """
    if settings.DEV:
        return True

    cache_key = 'template_active:{lang}:{path}'.format(lang=lang, path=path)
    is_active = cache.get(cache_key)
    if is_active is None:
        # try the quicker and more efficient check first
        is_active = lang_file_is_active(get_lang_path(path), lang)

        if not is_active:
            template = get_template(path)
            lang_files = parse_template(template.filename)
            is_active = lang_files and lang_file_is_active(lang_files[0], lang)

        cache.set(cache_key, is_active, settings.DOTLANG_CACHE)

    return is_active
コード例 #2
0
ファイル: __init__.py プロジェクト: benrito/bedrock
def render(request, template, context=None, **kwargs):
    """
    Same as django's render() shortcut, but with l10n template support.
    If used like this::

        return l10n_utils.render(request, 'myapp/mytemplate.html')

    ... this helper will render the following template::

        l10n/LANG/myapp/mytemplate.html

    if present, otherwise, it'll render the specified (en-US) template.
    """
    context = {} if context is None else context

    # Make sure we have a single template
    if isinstance(template, list):
        template = template[0]

    # Every template gets its own .lang file, so figure out what it is
    # and pass it in the context
    context['langfile'] = get_lang_path(template)

    # Get the available translation list of the current page
    context['translations'] = get_translations(context['langfile'])

    # Look for localized template if not default lang.
    if hasattr(request, 'locale') and request.locale != settings.LANGUAGE_CODE:

        # redirect to default lang if locale not active
        if not (settings.DEV or
                lang_file_is_active(context['langfile'], request.locale)):
            return HttpResponseRedirect('/' + '/'.join([
                settings.LANGUAGE_CODE,
                split_path(request.get_full_path())[1]
            ]))

        localized_tmpl = '%s/templates/%s' % (request.locale, template)
        try:
            return django_render(request, localized_tmpl, context, **kwargs)
        except TemplateDoesNotExist:
            # If not found, just go on and try rendering the parent template.
            pass

    return django_render(request, template, context, **kwargs)
コード例 #3
0
ファイル: __init__.py プロジェクト: darkowlzz/bedrock
def render(request, template, context=None, **kwargs):
    """
    Same as django's render() shortcut, but with l10n template support.
    If used like this::

        return l10n_utils.render(request, 'myapp/mytemplate.html')

    ... this helper will render the following template::

        l10n/LANG/myapp/mytemplate.html

    if present, otherwise, it'll render the specified (en-US) template.
    """
    context = {} if context is None else context

    # Every template gets its own .lang file, so figure out what it is
    # and pass it in the context
    context['langfile'] = get_lang_path(template)

    # Get the available translation list of the current page
    context['translations'] = get_translations(context['langfile'])

    # Look for localized template if not default lang.
    if hasattr(request, 'locale') and request.locale != settings.LANGUAGE_CODE:

        # redirect to default lang if locale not active
        if not (settings.DEV
                or lang_file_is_active(context['langfile'], request.locale)):
            return HttpResponseRedirect('/' + '/'.join([
                settings.LANGUAGE_CODE,
                split_path(request.get_full_path())[1]
            ]))

        localized_tmpl = '%s/templates/%s' % (request.locale, template)
        try:
            return django_render(request, localized_tmpl, context, **kwargs)
        except TemplateDoesNotExist:
            # If not found, just go on and try rendering the parent template.
            pass

    return django_render(request, template, context, **kwargs)
コード例 #4
0
ファイル: __init__.py プロジェクト: DonnieThomas/bedrock
def render(request, template, context={}, **kwargs):
    """
    Same as django's render() shortcut, but with l10n template support.
    If used like this::

        return l10n_utils.render(request, 'myapp/mytemplate.html')

    ... this helper will render the following template::

        l10n/LANG/myapp/mytemplate.html

    if present, otherwise, it'll render the specified (en-US) template.
    """
    # Every template gets its own .lang file, so figure out what it is
    # and pass it in the context
    context['langfile'] = get_lang_path(template)

    # Look for localized template if not default lang.
    if request.locale != settings.LANGUAGE_CODE:

        # redirect to default lang if locale not active
        if not (settings.DEV or
                lang_file_is_active(context['langfile'], request.locale)):
            return HttpResponseRedirect('/' + '/'.join([
                settings.LANGUAGE_CODE,
                split_path(request.get_full_path())[1]
            ]))

        localized_tmpl = '%s/templates/%s' % (request.locale, template)
        try:
            return django_render(request, localized_tmpl, context, **kwargs)
        except TemplateNotFound:
            # If not found, just go on and try rendering the parent template.
            pass

    return django_render(request, template, context, **kwargs)