* ``LC_ALL``, and
* ``LANG``
- :copyright: (c) 2015 by the Babel Team.
+ :copyright: (c) 2015, 2018 by the Babel Team.
:license: BSD, see LICENSE for more details.
"""
DEFAULT_LOCALE = default_locale()
-def format_list(lst, locale=DEFAULT_LOCALE):
+def format_list(lst, style='standard', locale=DEFAULT_LOCALE):
"""
Format the items in `lst` as a list.
- >>> format_list(['apples', 'oranges', 'pears'], 'en')
+ >>> format_list(['apples', 'oranges', 'pears'], locale='en')
u'apples, oranges, and pears'
- >>> format_list(['apples', 'oranges', 'pears'], 'zh')
+ >>> format_list(['apples', 'oranges', 'pears'], locale='zh')
u'apples\u3001oranges\u548cpears'
+ >>> format_list(['omena', 'peruna', 'aplari'], style='or', locale='fi')
+ u'omena, peruna tai aplari'
+
+ These styles are defined, but not all are necessarily available in all locales.
+ The following text is verbatim from the Unicode TR35-49 spec [1].
+
+ * standard:
+ A typical 'and' list for arbitrary placeholders.
+ eg. "January, February, and March"
+ * standard-short:
+ A short version of a 'and' list, suitable for use with short or abbreviated placeholder values.
+ eg. "Jan., Feb., and Mar."
+ * or:
+ A typical 'or' list for arbitrary placeholders.
+ eg. "January, February, or March"
+ * or-short:
+ A short version of an 'or' list.
+ eg. "Jan., Feb., or Mar."
+ * unit:
+ A list suitable for wide units.
+ eg. "3 feet, 7 inches"
+ * unit-short:
+ A list suitable for short units
+ eg. "3 ft, 7 in"
+ * unit-narrow:
+ A list suitable for narrow units, where space on the screen is very limited.
+ eg. "3′ 7″"
+
+ [1]: https://www.unicode.org/reports/tr35/tr35-49/tr35-general.html#ListPatterns
:param lst: a sequence of items to format in to a list
+ :param style: the style to format the list with. See above for description.
:param locale: the locale
"""
locale = Locale.parse(locale)
return ''
if len(lst) == 1:
return lst[0]
+
+ if style not in locale.list_patterns:
+ raise ValueError('Locale %s does not support list formatting style %r (supported are %s)' % (
+ locale,
+ style,
+ list(sorted(locale.list_patterns)),
+ ))
+ patterns = locale.list_patterns[style]
+
if len(lst) == 2:
- return locale.list_patterns['2'].format(*lst)
+ return patterns['2'].format(*lst)
- result = locale.list_patterns['start'].format(lst[0], lst[1])
+ result = patterns['start'].format(lst[0], lst[1])
for elem in lst[2:-1]:
- result = locale.list_patterns['middle'].format(result, elem)
- result = locale.list_patterns['end'].format(result, lst[-1])
+ result = patterns['middle'].format(result, elem)
+ result = patterns['end'].format(result, lst[-1])
return result
data["day_period_rules"] = day_period_rules[locale_id]
parse_locale_display_names(data, tree)
-
+ parse_list_patterns(data, tree)
parse_dates(data, tree, sup, regions, territory)
for calendar in tree.findall('.//calendars/calendar'):
scripts = data.setdefault('scripts', {})
for elem in tree.findall('.//scripts/script'):
_import_type_text(scripts, elem)
+
+
+def parse_list_patterns(data, tree):
list_patterns = data.setdefault('list_patterns', {})
for listType in tree.findall('.//listPatterns/listPattern'):
- if 'type' in listType.attrib:
- continue
+ by_type = list_patterns.setdefault(listType.attrib.get('type', 'standard'), {})
for listPattern in listType.findall('listPatternPart'):
- list_patterns[listPattern.attrib['type']] = _text(listPattern)
+ by_type[listPattern.attrib['type']] = _text(listPattern)
def parse_dates(data, tree, sup, regions, territory):