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) )
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)
def test_replace(self): self.assertEqual( html_diff("first old text", "first new text"), "first <del>old</del><ins>new</ins> text", )
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}
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)
def test_remove(self): self.assertEqual( html_diff('first old text', 'first text'), 'first <del>old </del>text' )
def test_add(self): self.assertEqual( html_diff('first text', 'first new text'), 'first <ins>new </ins>text' )
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) )
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))
def test_remove(self): self.assertEqual(html_diff('first old text', 'first text'), 'first <del>old </del>text')
def test_unicode(self): self.assertEqual(html_diff('zkouška text', 'zkouška nový text'), 'zkouška <ins>nový </ins>text')
def test_add(self): self.assertEqual(html_diff('first text', 'first new text'), 'first <ins>new </ins>text')
def test_replace(self): self.assertEqual( html_diff('first old text', 'first new text'), 'first <del>old</del><ins>new</ins> text', )
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, }
def test_same(self): self.assertEqual(html_diff('first text', 'first text'), 'first text')
def test_same(self): self.assertEqual(html_diff("first text", "first text"), "first text")
def test_same(self): self.assertEqual( html_diff('first text', 'first text'), 'first text' )
def test_add(self): self.assertEqual(html_diff("first text", "first new text"), "first <ins>new </ins>text")
def test_unicode(self): self.assertEqual( html_diff('zkouška text', 'zkouška nový text'), 'zkouška <ins>nový </ins>text' )
def test_unicode(self): self.assertEqual( html_diff("zkouška text", "zkouška nový text"), "zkouška <ins>nový </ins>text", )
def test_replace(self): self.assertEqual( html_diff('first old text', 'first new text'), 'first <del>old</del><ins>new</ins> text' )
def test_remove(self): self.assertEqual(html_diff("first old text", "first text"), "first <del>old </del>text")
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, }
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, }