| 1 |
""" |
| 2 |
babel.lists |
| 3 |
~~~~~~~~~~~ |
| 4 |
|
| 5 |
Locale dependent formatting of lists. |
| 6 |
|
| 7 |
The default locale for the functions in this module is determined by the |
| 8 |
following environment variables, in that order: |
| 9 |
|
| 10 |
* ``LC_ALL``, and |
| 11 |
* ``LANG`` |
| 12 |
|
| 13 |
:copyright: (c) 2015-2026 by the Babel Team. |
| 14 |
:license: BSD, see LICENSE for more details. |
| 15 |
""" |
| 16 |
|
| 17 |
from __future__ import annotations |
| 18 |
|
| 19 |
import warnings |
| 20 |
from collections.abc import Sequence |
| 21 |
from typing import Literal |
| 22 |
|
| 23 |
from babel.core import Locale, default_locale |
| 24 |
|
| 25 |
_DEFAULT_LOCALE = default_locale() # TODO(3.0): Remove this. |
| 26 |
|
| 27 |
|
| 28 |
def __getattr__(name): |
| 29 |
if name == "DEFAULT_LOCALE": |
| 30 |
warnings.warn( |
| 31 |
"The babel.lists.DEFAULT_LOCALE constant is deprecated and will be removed.", |
| 32 |
DeprecationWarning, |
| 33 |
stacklevel=2, |
| 34 |
) |
| 35 |
return _DEFAULT_LOCALE |
| 36 |
raise AttributeError(f"module {__name__!r} has no attribute {name!r}") |
| 37 |
|
| 38 |
|
| 39 |
def format_list( |
| 40 |
lst: Sequence[str], |
| 41 |
style: Literal[ |
| 42 |
'standard', |
| 43 |
'standard-short', |
| 44 |
'or', |
| 45 |
'or-short', |
| 46 |
'unit', |
| 47 |
'unit-short', |
| 48 |
'unit-narrow', |
| 49 |
] = 'standard', |
| 50 |
locale: Locale | str | None = None, |
| 51 |
) -> str: |
| 52 |
""" |
| 53 |
Format the items in `lst` as a list. |
| 54 |
|
| 55 |
>>> format_list(['apples', 'oranges', 'pears'], locale='en') |
| 56 |
'apples, oranges, and pears' |
| 57 |
>>> format_list(['apples', 'oranges', 'pears'], locale='zh') |
| 58 |
'apples、oranges和pears' |
| 59 |
>>> format_list(['omena', 'peruna', 'aplari'], style='or', locale='fi') |
| 60 |
'omena, peruna tai aplari' |
| 61 |
|
| 62 |
Not all styles are necessarily available in all locales. |
| 63 |
The function will attempt to fall back to replacement styles according to the rules |
| 64 |
set forth in the CLDR root XML file, and raise a ValueError if no suitable replacement |
| 65 |
can be found. |
| 66 |
|
| 67 |
The following text is verbatim from the Unicode TR35-49 spec [1]. |
| 68 |
|
| 69 |
* standard: |
| 70 |
A typical 'and' list for arbitrary placeholders. |
| 71 |
eg. "January, February, and March" |
| 72 |
* standard-short: |
| 73 |
A short version of an 'and' list, suitable for use with short or abbreviated placeholder values. |
| 74 |
eg. "Jan., Feb., and Mar." |
| 75 |
* or: |
| 76 |
A typical 'or' list for arbitrary placeholders. |
| 77 |
eg. "January, February, or March" |
| 78 |
* or-short: |
| 79 |
A short version of an 'or' list. |
| 80 |
eg. "Jan., Feb., or Mar." |
| 81 |
* unit: |
| 82 |
A list suitable for wide units. |
| 83 |
eg. "3 feet, 7 inches" |
| 84 |
* unit-short: |
| 85 |
A list suitable for short units |
| 86 |
eg. "3 ft, 7 in" |
| 87 |
* unit-narrow: |
| 88 |
A list suitable for narrow units, where space on the screen is very limited. |
| 89 |
eg. "3′ 7″" |
| 90 |
|
| 91 |
[1]: https://www.unicode.org/reports/tr35/tr35-49/tr35-general.html#ListPatterns |
| 92 |
|
| 93 |
:param lst: a sequence of items to format in to a list |
| 94 |
:param style: the style to format the list with. See above for description. |
| 95 |
:param locale: the locale. Defaults to the system locale. |
| 96 |
""" |
| 97 |
locale = Locale.parse(locale or _DEFAULT_LOCALE) |
| 98 |
if not lst: |
| 99 |
return '' |
| 100 |
if len(lst) == 1: |
| 101 |
return lst[0] |
| 102 |
|
| 103 |
patterns = _resolve_list_style(locale, style) |
| 104 |
|
| 105 |
if len(lst) == 2 and '2' in patterns: |
| 106 |
return patterns['2'].format(*lst) |
| 107 |
|
| 108 |
result = patterns['start'].format(lst[0], lst[1]) |
| 109 |
for elem in lst[2:-1]: |
| 110 |
result = patterns['middle'].format(result, elem) |
| 111 |
result = patterns['end'].format(result, lst[-1]) |
| 112 |
|
| 113 |
return result |
| 114 |
|
| 115 |
|
| 116 |
# Based on CLDR 45's root.xml file's `<alias>`es. |
| 117 |
# The root file defines both `standard` and `or`, |
| 118 |
# so they're always available. |
| 119 |
# TODO: It would likely be better to use the |
| 120 |
# babel.localedata.Alias mechanism for this, |
| 121 |
# but I'm not quite sure how it's supposed to |
| 122 |
# work with inheritance and data in the root. |
| 123 |
_style_fallbacks = { |
| 124 |
"or-narrow": ["or-short", "or"], |
| 125 |
"or-short": ["or"], |
| 126 |
"standard-narrow": ["standard-short", "standard"], |
| 127 |
"standard-short": ["standard"], |
| 128 |
"unit": ["unit-short", "standard"], |
| 129 |
"unit-narrow": ["unit-short", "unit", "standard"], |
| 130 |
"unit-short": ["standard"], |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
def _resolve_list_style(locale: Locale, style: str): |
| 135 |
for style in (style, *(_style_fallbacks.get(style, []))): # noqa: B020 |
| 136 |
if style in locale.list_patterns: |
| 137 |
return locale.list_patterns[style] |
| 138 |
raise ValueError( |
| 139 |
f"Locale {locale} does not support list formatting style {style!r} " |
| 140 |
f"(supported are {sorted(locale.list_patterns)})", |
| 141 |
) |