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))
def get_target_plurals(self): ''' Returns target plurals in array. ''' # Is this plural? if not self.is_plural(): return [self.target] # Split plurals ret = split_plural(self.target) # Check if we have expected number of them plurals = self.translation.language.nplurals if len(ret) == plurals: return ret # Pad with empty translations while len(ret) < plurals: ret.append('') # Delete extra plurals while len(ret) > plurals: del(ret[-1]) return ret
def get_target_plurals(self): ''' Returns target plurals in array. ''' # Is this plural? if not self.is_plural(): return [self.target] # Split plurals ret = split_plural(self.target) # Check if we have expected number of them plurals = self.translation.language.nplurals if len(ret) == plurals: return ret # Pad with empty translations while len(ret) < plurals: ret.append('') # Delete extra plurals while len(ret) > plurals: del(ret[-1]) return ret
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 get_source_plurals(self): ''' Returns source plurals in array. ''' return split_plural(self.source)
def get_source_plurals(self): ''' Retuns source plurals in array. ''' return split_plural(self.source)
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) )