コード例 #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)

    # 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 = htmlDiff(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(value)
コード例 #2
0
ファイル: translations.py プロジェクト: seungjin/weblate
def fmttranslation(value, language=None, diff=None):
    if language is None:
        language = Language.objects.get(code="en")
    plurals = split_plural(value)
    if diff is not None:
        diff = split_plural(diff)
    parts = []
    for idx, value in enumerate(plurals):
        value = escape(force_unicode(value))
        if diff is not None:
            diffvalue = escape(force_unicode(diff[idx]))
            value = htmlDiff(diffvalue, value)
        value = re.sub(r"\r\n|\r|\n", "\n", value)  # normalize newlines
        paras = re.split("\n", value)
        paras = [fmt_whitespace(p) for p in paras]
        if len(plurals) > 1:
            value = '<span class="pluraltxt">%s</span><br />' % language.get_plural_label(idx)
        else:
            value = ""
        value += u'<span class="hlspace">↵</span><br />'.join(paras)
        parts.append(value)
    value = "<hr />".join(parts)
    return mark_safe(value)