lists.py 4.3 KB

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