当前位置: 首页>>代码示例>>Python>>正文


Python util.split_plural函数代码示例

本文整理汇总了Python中weblate.trans.util.split_plural函数的典型用法代码示例。如果您正苦于以下问题:Python split_plural函数的具体用法?Python split_plural怎么用?Python split_plural使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了split_plural函数的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: format_translation

def format_translation(value, language, diff=None, search_match=None,
                       simple=False, num_plurals=2, unit=None):
    """Nicely formats translation text possibly handling plurals or diff."""
    # Split plurals to separate strings
    plurals = split_plural(value)

    # Show plurals?
    if int(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, raw_value in enumerate(plurals):
        # HTML escape
        value = escape(force_text(raw_value))

        # Format diff if there is any
        value = fmt_diff(value, diff, idx)

        # Create span for checks highlights
        value = fmt_highlights(raw_value, value, unit)

        # Format search term
        value = fmt_search(value, search_match)

        # 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_name(idx)

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

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

    return {
        'simple': simple,
        'items': parts,
        'language': language,
    }
开发者ID:saily,项目名称:weblate,代码行数:60,代码来源:translations.py

示例2: fmttranslation

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)
    )
开发者ID:Freso,项目名称:weblate,代码行数:58,代码来源:translations.py

示例3: get_target_plurals

    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
开发者ID:chervol,项目名称:weblate,代码行数:25,代码来源:unit.py

示例4: merge_translations

    def merge_translations(self, request, store2, overwrite, add_fuzzy, fuzzy):
        """
        Merges translation unit wise, needed for template based translations to
        add new strings.
        """
        ret = False

        for set_fuzzy, unit2 in store2.iterate_merge(fuzzy):
            try:
                unit = self.unit_set.get(
                    source=unit2.get_source(),
                    context=unit2.get_context(),
                )
            except Unit.DoesNotExist:
                continue

            if unit.translated and not overwrite:
                continue

            ret = True

            unit.translate(
                request,
                split_plural(unit2.get_target()),
                add_fuzzy or set_fuzzy
            )

        return ret
开发者ID:franco999,项目名称:weblate,代码行数:28,代码来源:translation.py

示例5: merge_translations

    def merge_translations(self, request, author, store2, overwrite,
                           add_fuzzy):
        """
        Merges translation unit wise, needed for template based translations to
        add new strings.
        """

        for unit2 in store2.all_units():
            # No translated -> skip
            if not unit2.is_translated() or unit2.unit.isheader():
                continue

            try:
                unit = self.unit_set.get(
                    source=unit2.get_source(),
                    context=unit2.get_context(),
                )
            except Unit.DoesNotExist:
                continue

            unit.translate(
                request,
                split_plural(unit2.get_target()),
                add_fuzzy or unit2.is_fuzzy()
            )
开发者ID:dschlager,项目名称:weblate,代码行数:25,代码来源:translation.py

示例6: merge_translations

    def merge_translations(self, request, store2, overwrite, add_fuzzy,
                           fuzzy, merge_header):
        """Merge translation unit wise

        Needed for template based translations to add new strings.
        """
        not_found = 0
        skipped = 0
        accepted = 0

        # Commit possible prior changes
        self.commit_pending(request)

        for set_fuzzy, unit2 in store2.iterate_merge(fuzzy):
            try:
                unit = self.unit_set.get_unit(unit2)
            except Unit.DoesNotExist:
                not_found += 1
                continue

            if ((unit.translated and not overwrite)
                    or (not request.user.has_perm('unit.edit', unit))):
                skipped += 1
                continue

            accepted += 1

            # We intentionally avoid propagating:
            # - in most cases it's not desired
            # - it slows down import considerably
            # - it brings locking issues as import is
            #   executed with lock held and linked repos
            #   can't obtain the lock
            state = STATE_TRANSLATED
            if add_fuzzy or set_fuzzy:
                state = STATE_FUZZY
            unit.translate(
                request,
                split_plural(unit2.get_target()),
                state,
                change_action=Change.ACTION_UPLOAD,
                propagate=False
            )

        if accepted > 0:
            self.invalidate_cache()
            request.user.profile.refresh_from_db()
            request.user.profile.translated += accepted
            request.user.profile.save(update_fields=['translated'])

            if merge_header:
                self.store.merge_header(store2)
                self.store.save()

            self.commit_pending(request)

        return (not_found, skipped, accepted, store2.count_units())
开发者ID:dsnoeck,项目名称:weblate,代码行数:57,代码来源:translation.py

示例7: fmttranslation

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)
开发者ID:seungjin,项目名称:weblate,代码行数:23,代码来源:translations.py

示例8: get_source_plurals

 def get_source_plurals(self):
     """
     Returns source plurals in array.
     """
     return split_plural(self.source)
开发者ID:chervol,项目名称:weblate,代码行数:5,代码来源:unit.py

示例9: format_translation

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,
    }
开发者ID:blockgiven,项目名称:weblate,代码行数:74,代码来源:translations.py

示例10: fmttranslation

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)
    )
开发者ID:paour,项目名称:weblate,代码行数:75,代码来源:translations.py

示例11: get_source_plurals

 def get_source_plurals(self):
     '''
     Returns source plurals in array.
     '''
     return split_plural(self.source)
开发者ID:sebo77,项目名称:weblate,代码行数:5,代码来源:unit.py

示例12: merge_translations

    def merge_translations(self, request, store2, overwrite, add_fuzzy,
                           fuzzy, merge_header):
        """Merge translation unit wise

        Needed for template based translations to add new strings.
        """
        not_found = 0
        skipped = 0
        accepted = 0

        # Are there any translations to propagate?
        # This is just an optimalization to avoid doing that for every unit.
        propagate = Translation.objects.filter(
            language=self.language,
            subproject__project=self.subproject.project
        ).filter(
            subproject__allow_translation_propagation=True
        ).exclude(
            pk=self.pk
        ).exists()

        author = get_author_name(request.user)

        # Commit possible prior changes
        self.commit_pending(request, author)
        # Avoid committing while we're importing
        self._skip_commit = True

        for set_fuzzy, unit2 in store2.iterate_merge(fuzzy):
            try:
                unit = self.unit_set.get_unit(unit2)
            except Unit.DoesNotExist:
                not_found += 1
                continue

            if unit.translated and not overwrite:
                skipped += 1
                continue

            accepted += 1

            unit.translate(
                request,
                split_plural(unit2.get_target()),
                add_fuzzy or set_fuzzy,
                change_action=Change.ACTION_UPLOAD,
                propagate=propagate
            )

        self._skip_commit = False

        if accepted > 0:
            self.update_stats()

            if merge_header:
                self.store.merge_header(store2)
                self.store.save()

            self.git_commit(
                request, author, timezone.now(),
                force_commit=True, sync=True
            )

        return (not_found, skipped, accepted, store2.count_units())
开发者ID:saily,项目名称:weblate,代码行数:64,代码来源:translation.py

示例13: format_translation

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}
开发者ID:lln-ijinus,项目名称:weblate,代码行数:97,代码来源:translations.py


注:本文中的weblate.trans.util.split_plural函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。