コード例 #1
0
def xblock_translate(parser, token):
    """
    This is a proxy implementation of the i18n `trans` tag.
    It takes the result of using the regular translate block and passes it to
    the ProxyTransNode for rendering
    """
    return ProxyTransNode(do_translate(parser, token))
コード例 #2
0
def context_id_do_translate(parser, token):
    """ Overwriting the original tag (if you load `cosinnus_tags` after `i18n`.
        Adds in a settings switch to display an identifier if you include it in parentheses
        as context for the translated string.
        Example: {% trans "My String" context "(MS1)" %} renders as "My String [MS1] """
    translate_node = do_translate(parser, token)
    return ContextIdTranslateNode(translate_node)
コード例 #3
0
ファイル: fluent.py プロジェクト: adamchainz/fluent-2.0
def trans_override(parser, token):
    """
        Wraps around Django's trans tag, but allows for 'group "Thing"' to be
        specified (group is only used for exporting) not for translation.
    """
    contents = token.split_contents()
    if "group" in contents:
        #Remove the group tag from the token
        idx = contents.index("group")
        group = contents[idx + 1]
        contents.remove("group")
        contents.remove(group)
        token.contents = " ".join(contents)

    return do_translate(parser, token)
コード例 #4
0
ファイル: fluent.py プロジェクト: adamchainz/fluent-2.0
def trans_override(parser, token):
    """
        Wraps around Django's trans tag, but allows for 'group "Thing"' to be
        specified (group is only used for exporting) not for translation.
    """
    contents = token.split_contents()
    if "group" in contents:
        #Remove the group tag from the token
        idx = contents.index("group")
        group = contents[idx + 1]
        contents.remove("group")
        contents.remove(group)
        token.contents = " ".join(contents)

    return do_translate(parser, token)
コード例 #5
0
ファイル: templatetags.py プロジェクト: iske/transifex-python
    def _parse_trans(self, token, parser, original_string):
        """Parse a {% trans %} token and return a migration object.

        :param django.template.base.Token token: the token object
        :param django.template.base.parser: the parser object
        :param unicode original_string: the string found in the template
        """

        # Use Django's do_translate() method to parse the token
        trans_node = do_translate(parser, token)

        confidence = Confidence.LOW if trans_node.noop else Confidence.HIGH

        message_context = trans_node.message_context
        # Our SDK supports filter expressions
        text = trans_node.filter_expression.token

        # Source strings that contain XML symbols should use 'ut'. We determine
        # whether the string contains XML symbols by testing if an escaping
        # attempt changes it in any way.
        # eg `{% trans "a b" %}`            => `{% t "a b" %}`
        #    `{% trans "<xml>a</xml> b" %}` => `{% ut "<xml>a</xml> b" %}`
        if isinstance(trans_node.filter_expression.var, string_types):
            literal = trans_node.filter_expression.var
        else:
            literal = trans_node.filter_expression.var.literal
        if (isinstance(literal, string_types)
                and escape_html(literal) != literal):
            tag_name = "ut"
        else:
            tag_name = "t"

        params = {'_context': message_context, '_comment': self._comment}
        # Reset the stored comment, so that it doesn't leak to the next token
        self._comment = None

        # Render the final output
        t_tag = ['{%', tag_name, text, _render_params(params)]
        if trans_node.asvar:
            t_tag.extend(['as', trans_node.asvar])
        t_tag.append('%}')
        t_tag = ' '.join((thing.strip() for thing in t_tag if thing.strip()))

        return self._final_string_migration(original_string,
                                            t_tag,
                                            confidence=confidence)
コード例 #6
0
ファイル: fluent.py プロジェクト: robcharlwood/fluent-2.0
def trans_override(parser, token):
    """
        Wraps around Django's trans tag, but allows for 'group "Thing"' to be
        specified (group is only used for exporting) not for translation.
    """
    contents = token.split_contents()

    escape = True
    if "noescape" in contents:
        contents.remove("noescape")
        escape = False

    if "group" in contents:
        #Remove the group tag from the token
        idx = contents.index("group")
        group = contents[idx + 1]
        contents.remove("group")
        contents.remove(group)

    token.contents = " ".join(contents)

    result = do_translate(parser, token)

    if escape:
        # If the 'noescape' option has NOT been passed, then we treat both the default text and the
        # translated text as not HTML safe.
        return EscapedTranslateNode(
            result.filter_expression,
            result.noop,
            result.asvar,
            result.message_context
        )
    else:
        # If the 'noescape' option has been passed to the tag then we need to tell Django not to
        # escape the result. If the result is just the default text from the string literal inside
        # the `{% trans %}` tag instance then Django will treat it as safe anyway. But if the
        # result has come from a translation then Django will not treat it as safe, so we need to
        # add the |safe filter to tell Django not to escape it.
        result.filter_expression.filters.append((safe_filter, []))
        return result
コード例 #7
0
def trans_override(parser, token):
    """
        Wraps around Django's trans tag, but allows for 'group "Thing"' to be
        specified (group is only used for exporting) not for translation.
    """
    contents = token.split_contents()

    escape = True
    if "noescape" in contents:
        contents.remove("noescape")
        escape = False

    if "group" in contents:
        #Remove the group tag from the token
        idx = contents.index("group")
        group = contents[idx + 1]
        contents.remove("group")
        contents.remove(group)

    token.contents = " ".join(contents)

    result = do_translate(parser, token)

    if escape:
        # If the 'noescape' option has NOT been passed, then we treat both the default text and the
        # translated text as not HTML safe.
        return EscapedTranslateNode(result.filter_expression, result.noop,
                                    result.asvar, result.message_context)
    else:
        # If the 'noescape' option has been passed to the tag then we need to tell Django not to
        # escape the result. If the result is just the default text from the string literal inside
        # the `{% trans %}` tag instance then Django will treat it as safe anyway. But if the
        # result has come from a translation then Django will not treat it as safe, so we need to
        # add the |safe filter to tell Django not to escape it.
        result.filter_expression.filters.append((safe_filter, []))
        return result
コード例 #8
0
def xblock_translate(parser, token):
    """
    Proxy implementation of the i18n `trans` tag.
    """
    return ProxyTransNode(i18n.do_translate(parser, token))
コード例 #9
0
ファイル: i18n_utils.py プロジェクト: ebrelsford/596acres
def do_ascii_translate(parser, token):
    return AsciiTranslateNode(do_translate(parser, token))
コード例 #10
0
ファイル: i18n.py プロジェクト: edx/xblock-utils
def xblock_translate(parser, token):
    """
    Proxy implementation of the i18n `trans` tag.
    """
    return ProxyTransNode(i18n.do_translate(parser, token))
コード例 #11
0
def do_ascii_translate(parser, token):
    return AsciiTranslateNode(do_translate(parser, token))