Example #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(code='en')

    # 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)
    )
Example #2
0
def fmt_diff(value, diff, idx):
    """Format diff if there is any."""
    if diff is None:
        return escape(value)
    return html_diff(force_str(diff[idx]), value)
Example #3
0
 def test_replace(self):
     self.assertEqual(
         html_diff("first old text", "first new text"),
         "first <del>old</del><ins>new</ins> text",
     )
Example #4
0
def format_translation(value, language, diff=None, search_match=None, simple=False, num_plurals=2, checks=None):
    """
    Nicely formats translation text possibly handling plurals or diff.
    """
    # Split plurals to separate strings
    plurals = split_plural(value)

    # Show plurals?
    if num_plurals <= 1:
        plurals = plurals[:1]

    # Newline concatenator
    newline = SPACE_NL.format(_("New line"))

    # 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):
        highlights = None
        # Find all checks highlight
        if checks:
            highlights = []
            for c in checks:
                highlights += c.check_highlight(value, None)
            # Sort by order in string
            if highlights:
                highlights.sort(key=lambda tup: tup[0])
            # remove probelmatics ones
            for n in xrange(0, len(highlights)):
                if n >= len(highlights):
                    break
                elref = highlights[n]
                for n2 in xrange(n, len(highlights)):
                    if n2 >= len(highlights):
                        break
                    eltest = highlights[n2]
                    if eltest[0] >= elref[0] and eltest[0] <= (elref[0] + len(elref[1])):
                        highlights.pop(n2)
                    elif eltest[0] > (elref[0] + len(elref[1])):
                        break
            # then transform highlights to escaped html
            highlights = [(h[0], escape(force_unicode(h[1]))) for h in highlights]

        # 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)

        # Create span for checks highlights
        if highlights:
            n = 0
            for (hidx, htext) in highlights:
                p = value.find(htext, n)
                if p >= 0:
                    newpart = u'<span class="hlcheck">{0}</span>'.format(htext)
                    value = value[:p] + newpart + value[(p + len(htext)) :]
                    n = p + len(newpart)

        # Format search term
        if search_match:
            # 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, u'<span class="hlmatch">{0}</span>'.format(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)
        title = ""
        if len(plurals) > 1:
            title = language.get_plural_label(idx)

        # Join paragraphs
        content = mark_safe(newline.join(paras))

        parts.append({"title": title, "content": content})

    return {"simple": simple, "items": parts, "language": language}
Example #5
0
def fmt_diff(value, diff, idx):
    """Format diff if there is any"""
    if diff is None:
        return value
    diffvalue = escape(force_text(diff[idx]))
    return html_diff(diffvalue, value)
Example #6
0
 def test_remove(self):
     self.assertEqual(
         html_diff('first old text', 'first text'),
         'first <del>old </del>text'
     )
Example #7
0
 def test_add(self):
     self.assertEqual(
         html_diff('first text', 'first new text'),
         'first <ins>new </ins>text'
     )
Example #8
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)
    )
Example #9
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))
Example #10
0
 def test_remove(self):
     self.assertEqual(html_diff('first old text', 'first text'),
                      'first <del>old </del>text')
Example #11
0
 def test_unicode(self):
     self.assertEqual(html_diff('zkouška text', 'zkouška nový text'),
                      'zkouška <ins>nový </ins>text')
Example #12
0
 def test_add(self):
     self.assertEqual(html_diff('first text', 'first new text'),
                      'first <ins>new </ins>text')
Example #13
0
 def test_replace(self):
     self.assertEqual(
         html_diff('first old text', 'first new text'),
         'first <del>old</del><ins>new</ins> text',
     )
Example #14
0
def fmt_diff(value, diff, idx):
    """Format diff if there is any"""
    if diff is None:
        return value
    diffvalue = escape(force_text(diff[idx]))
    return html_diff(diffvalue, value)
Example #15
0
def format_translation(value,
                       language,
                       diff=None,
                       search_match=None,
                       simple=False,
                       num_plurals=2):
    """
    Nicely formats translation text possibly handling plurals or diff.
    """
    # Split plurals to separate strings
    plurals = split_plural(value)

    # Show plurals?
    if num_plurals <= 1:
        plurals = plurals[:1]

    # Newline concatenator
    newline = SPACE_NL.format(_('New line'))

    # 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:
            # 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,
                    u'<span class="hlmatch">{0}</span>'.format(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)
        title = ''
        if len(plurals) > 1:
            title = language.get_plural_label(idx)

        # Join paragraphs
        content = mark_safe(newline.join(paras))

        parts.append({'title': title, 'content': content})

    return {
        'simple': simple,
        'items': parts,
        'language': language,
    }
Example #16
0
 def test_same(self):
     self.assertEqual(html_diff('first text', 'first text'), 'first text')
Example #17
0
 def test_same(self):
     self.assertEqual(html_diff("first text", "first text"), "first text")
Example #18
0
 def test_same(self):
     self.assertEqual(
         html_diff('first text', 'first text'),
         'first text'
     )
Example #19
0
 def test_add(self):
     self.assertEqual(html_diff("first text", "first new text"),
                      "first <ins>new </ins>text")
Example #20
0
 def test_unicode(self):
     self.assertEqual(
         html_diff('zkouška text', 'zkouška nový text'),
         'zkouška <ins>nový </ins>text'
     )
Example #21
0
 def test_unicode(self):
     self.assertEqual(
         html_diff("zkouška text", "zkouška nový text"),
         "zkouška <ins>nový </ins>text",
     )
Example #22
0
 def test_replace(self):
     self.assertEqual(
         html_diff('first old text', 'first new text'),
         'first <del>old</del><ins>new</ins> text'
     )
Example #23
0
 def test_remove(self):
     self.assertEqual(html_diff("first old text", "first text"),
                      "first <del>old </del>text")
Example #24
0
def format_translation(value, language=None, diff=None, search_match=None,
                       simple=False):
    """
    Nicely formats translation text possibly handling plurals or diff.
    """
    # Get language
    if language is None:
        language = Language.objects.get_default()

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

    # Newline concatenator
    newline = u'<span class="hlspace" title="{0}">↵</span><br />'.format(
        _('New line')
    )

    # 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:
            # 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,
                    u'<span class="hlmatch">{0}</span>'.format(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)
        title = ''
        if len(plurals) > 1:
            title = language.get_plural_label(idx)

        # Join paragraphs
        content = mark_safe(newline.join(paras))

        parts.append({'title': title, 'content': content})

    return {
        'simple': simple,
        'items': parts,
        'language': language,
    }
Example #25
0
def format_translation(value,
                       language,
                       diff=None,
                       search_match=None,
                       simple=False,
                       num_plurals=2,
                       checks=None):
    """
    Nicely formats translation text possibly handling plurals or diff.
    """
    # Split plurals to separate strings
    plurals = split_plural(value)

    # Show plurals?
    if num_plurals <= 1:
        plurals = plurals[:1]

    # Newline concatenator
    newline = SPACE_NL.format(_('New line'))

    # 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):
        highlights = None
        # Find all checks highlight
        if checks:
            highlights = []
            for c in checks:
                highlights += c.check_highlight(value, None)
            #Sort by order in string
            if highlights: highlights.sort(key=lambda tup: tup[0])
            #remove probelmatics ones
            for n in xrange(0, len(highlights)):
                if n >= len(highlights): break
                elref = highlights[n]
                for n2 in xrange(n, len(highlights)):
                    if n2 >= len(highlights): break
                    eltest = highlights[n2]
                    if eltest[0] >= elref[0] and eltest[0] <= (elref[0] +
                                                               len(elref[1])):
                        highlights.pop(n2)
                    elif eltest[0] > (elref[0] + len(elref[1])):
                        break
            #then transform highlights to escaped html
            highlights = [(h[0], escape(force_unicode(h[1])))
                          for h in highlights]

        # 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)

        # Create span for checks highlights
        if highlights:
            n = 0
            for (hidx, htext) in highlights:
                p = value.find(htext, n)
                if p >= 0:
                    newpart = u'<span class="hlcheck">{0}</span>'.format(htext)
                    value = value[:p] + newpart + value[(p + len(htext)):]
                    n = p + len(newpart)

        # Format search term
        if search_match:
            # 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,
                    u'<span class="hlmatch">{0}</span>'.format(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)
        title = ''
        if len(plurals) > 1:
            title = language.get_plural_label(idx)

        # Join paragraphs
        content = mark_safe(newline.join(paras))

        parts.append({'title': title, 'content': content})

    return {
        'simple': simple,
        'items': parts,
        'language': language,
    }