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


Python Locale.plural_form方法代码示例

本文整理汇总了Python中babel.Locale.plural_form方法的典型用法代码示例。如果您正苦于以下问题:Python Locale.plural_form方法的具体用法?Python Locale.plural_form怎么用?Python Locale.plural_form使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在babel.Locale的用法示例。


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

示例1: pluralize

# 需要导入模块: from babel import Locale [as 别名]
# 或者: from babel.Locale import plural_form [as 别名]
def pluralize(dic, count, locale=utils.DEFAULT_LOCALE):
    """Takes a dictionary and a number and return the value whose key in
    the dictionary is either

        a. that number, or
        b. the textual representation of that number according to the `CLDR
           rules <cldr_rules>`_ for that locale, Depending of the language, this can be:
           "zero", "one", "two", "few", "many" or "other".

    ..  rules: http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html

    As a deviation of the standard:

    - If ``count`` is 0, a `'zero'` is tried
    - If the textual representation is `'other'` but that key doesn't exists, a
      `'many'` key is tried instead.

    Finally, if none of these exits, an empty string is returned.

    Examples:

    >>> dic = {
            0: u'No apples',
            1: u'One apple',
            3: u'Few apples',
            'many': u'{count} apples',
        }
    >>> pluralize(dic, 0)
    'No apples'
    >>> pluralize(dic, 1)
    'One apple'
    >>> pluralize(dic, 3)
    'Few apples'
    >>> pluralize(dic, 10)
    '{count} apples'

    >>> dic = {
            'zero': u'No apples whatsoever',
            'one': u'One apple',
            'other': u'{count} apples',
        }
    >>> pluralize(dic, 0)
    u'No apples whatsoever'
    >>> pluralize(dic, 1)
    'One apple'
    >>> pluralize(dic, 2)
    '{count} apples'
    >>> pluralize(dic, 10)
    '{count} apples'

    >>> pluralize({0: 'off', 'many': 'on'}, 3)
    'on'
    >>> pluralize({0: 'off', 'other': 'on'}, 0)
    'off'
    >>> pluralize({0: 'off', 'other': 'on'}, 456)
    'on'
    >>> pluralize({}, 3)


    Note that this function **does not** interpolate the string, just returns
    the right one for the value of ``count``.
    """
    count = int(count or 0)
    scount = str(count).strip()
    plural = dic.get(count, dic.get(scount))
    if plural is not None:
        return plural

    if count == 0:
        plural = dic.get('zero')
        if plural is not None:
            return plural

    if isinstance(locale, string_types):
        locale = Locale(locale)
    literal = locale.plural_form(count)
    return dic.get(literal, dic.get('many', u''))
开发者ID:jpscaletti,项目名称:allspeak,代码行数:79,代码来源:i18n.py


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