Ejemplo n.º 1
0
def fmttranslation(value, language=None, diff=None):
    '''
    Formats translation to show whitespace, plural forms or diff.
    '''
    # Get language
    if language is None:
        language = Language.objects.get_default()

    # Split plurals to separate strings
    plurals = split_plural(value)

    # Split diff plurals
    if diff is not None:
        diff = split_plural(diff)
        # Previous message did not have to be a plural
        while len(diff) < len(plurals):
            diff.append(diff[0])

    # We will collect part for each plural
    parts = []

    for idx, value in enumerate(plurals):

        # HTML escape
        value = escape(force_unicode(value))

        # Format diff if there is any
        if diff is not None:
            diffvalue = escape(force_unicode(diff[idx]))
            value = html_diff(diffvalue, value)

        # Normalize newlines
        value = NEWLINES_RE.sub('\n', value)

        # Split string
        paras = value.split('\n')

        # Format whitespace in each paragraph
        paras = [fmt_whitespace(p) for p in paras]

        # Show label for plural (if there are any)
        if len(plurals) > 1:
            value = '<span class="pluraltxt">%s</span><br />' % (
                language.get_plural_label(idx))
        else:
            value = ''

        # Join paragraphs
        newline = u'<span class="hlspace" title="%s">↵</span><br />' % (
            _('New line'))
        value += newline.join(paras)

        parts.append(value)

    value = '<hr />'.join(parts)

    return mark_safe('<span lang="%s" dir="%s" class="direction">%s</span>' %
                     (language.code, language.direction, value))
Ejemplo n.º 2
0
 def test_replace(self):
     self.assertEqual(
         html_diff('first old text', 'first new text'),
         'first <del>old</del><ins>new</ins> text'
     )
Ejemplo n.º 3
0
 def test_remove(self):
     self.assertEqual(
         html_diff('first old text', 'first text'),
         'first <del>old </del>text'
     )
Ejemplo n.º 4
0
 def test_add(self):
     self.assertEqual(
         html_diff('first text', 'first new text'),
         'first <ins>new </ins>text'
     )
Ejemplo n.º 5
0
 def test_same(self):
     self.assertEqual(
         html_diff('first text', 'first text'),
         'first text'
     )
Ejemplo n.º 6
0
def fmttranslation(value, language=None, diff=None, search_match=None):
    """
    Formats translation to show whitespace, plural forms or diff.
    """
    # Get language
    if language is None:
        language = Language.objects.get_default()

    # Split plurals to separate strings
    plurals = split_plural(value)

    # Split diff plurals
    if diff is not None:
        diff = split_plural(diff)
        # Previous message did not have to be a plural
        while len(diff) < len(plurals):
            diff.append(diff[0])

    # We will collect part for each plural
    parts = []

    for idx, value in enumerate(plurals):

        # HTML escape
        value = escape(force_unicode(value))

        # Format diff if there is any
        if diff is not None:
            diffvalue = escape(force_unicode(diff[idx]))
            value = html_diff(diffvalue, value)

        # Format search term
        if search_match is not None:
            # Since the search ignored case, we need to highlight any
            # combination of upper and lower case we find. This is too
            # advanced for str.replace().
            caseless = re.compile(re.escape(search_match), re.IGNORECASE)
            for variation in re.findall(caseless, value):
                value = re.sub(caseless, '<span class="hlmatch">%s</span>' % (variation), value)

        # Normalize newlines
        value = NEWLINES_RE.sub("\n", value)

        # Split string
        paras = value.split("\n")

        # Format whitespace in each paragraph
        paras = [fmt_whitespace(p) for p in paras]

        # Show label for plural (if there are any)
        if len(plurals) > 1:
            value = '<span class="pluraltxt">%s</span><br />' % (language.get_plural_label(idx))
        else:
            value = ""

        # Join paragraphs
        newline = u'<span class="hlspace" title="%s">↵</span><br />' % (_("New line"))
        value += newline.join(paras)

        parts.append(value)

    value = "<hr />".join(parts)

    return mark_safe(
        '<span lang="%s" dir="%s" class="direction">%s</span>' % (language.code, language.direction, value)
    )
Ejemplo n.º 7
0
def fmttranslation(value, language=None, diff=None):
    '''
    Formats translation to show whitespace, plural forms or diff.
    '''
    # Get language
    if language is None:
        language = Language.objects.get_default()

    # Split plurals to separate strings
    plurals = split_plural(value)

    # Split diff plurals
    if diff is not None:
        diff = split_plural(diff)
        # Previous message did not have to be a plural
        while len(diff) < len(plurals):
            diff.append(diff[0])

    # We will collect part for each plural
    parts = []

    for idx, value in enumerate(plurals):

        # HTML escape
        value = escape(force_unicode(value))

        # Format diff if there is any
        if diff is not None:
            diffvalue = escape(force_unicode(diff[idx]))
            value = html_diff(diffvalue, value)

        # Normalize newlines
        value = NEWLINES_RE.sub('\n', value)

        # Split string
        paras = value.split('\n')

        # Format whitespace in each paragraph
        paras = [fmt_whitespace(p) for p in paras]

        # Show label for plural (if there are any)
        if len(plurals) > 1:
            value = '<span class="pluraltxt">%s</span><br />' % (
                language.get_plural_label(idx)
            )
        else:
            value = ''

        # Join paragraphs
        newline = u'<span class="hlspace" title="%s">↵</span><br />' % (
            _('New line')
        )
        value += newline.join(paras)

        parts.append(value)

    value = '<hr />'.join(parts)

    return mark_safe(
        '<span lang="%s" dir="%s" class="direction">%s</span>' %
        (language.code, language.direction, value)
    )
Ejemplo n.º 8
0
 def test_replace(self):
     self.assertEqual(html_diff('first old text', 'first new text'),
                      'first <del>old</del><ins>new</ins> text')
Ejemplo n.º 9
0
 def test_remove(self):
     self.assertEqual(html_diff('first old text', 'first text'),
                      'first <del>old </del>text')
Ejemplo n.º 10
0
 def test_add(self):
     self.assertEqual(html_diff('first text', 'first new text'),
                      'first <ins>new </ins>text')
Ejemplo n.º 11
0
 def test_same(self):
     self.assertEqual(html_diff('first text', 'first text'), 'first text')