mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-06 11:18:54 -05:00
Make humanize_list() use babel. (#2982)
* Removes `MAX_BALANCE` from bank, user `bank.get_max_balance()` now `[p]bankset maxbal` can be used to set the maximum bank balance Signed-off-by: Guy <guyreis96@gmail.com> * Migrates ``humanize_list`` over to babel, and uses the existing `get_babel_locale` function to get a valid locale based on bot locale. Signed-off-by: guyre <27962761+drapersniper@users.noreply.github.com> * Migrates ``humanize_list`` over to babel, and uses the existing `get_babel_locale` function to get a valid locale based on bot locale. Signed-off-by: guyre <27962761+drapersniper@users.noreply.github.com> * Fixes docs Signed-off-by: guyre <27962761+drapersniper@users.noreply.github.com> * Add reference to Babel's `format_list` * Add Babel to intersphinx * remove towncrier entry * Migrates ``humanize_list`` over to babel, and uses the existing `get_babel_locale` function to get a valid locale based on bot locale. Signed-off-by: guyre <27962761+drapersniper@users.noreply.github.com> * Fixes docs Signed-off-by: guyre <27962761+drapersniper@users.noreply.github.com> * Add reference to Babel's `format_list` * Fix order, make the parameters keyword-only Co-authored-by: jack1142 <6032823+jack1142@users.noreply.github.com>
This commit is contained in:
parent
c5a553a600
commit
1a3e264b2a
@ -1,13 +1,13 @@
|
|||||||
import itertools
|
|
||||||
import datetime
|
import datetime
|
||||||
from typing import Sequence, Iterator, List, Optional, Union, SupportsInt
|
import itertools
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
|
from typing import Iterator, List, Optional, Sequence, SupportsInt, Union
|
||||||
|
|
||||||
import discord
|
import discord
|
||||||
|
from babel.lists import format_list as babel_list
|
||||||
from babel.numbers import format_decimal
|
from babel.numbers import format_decimal
|
||||||
|
|
||||||
from redbot.core.i18n import Translator, get_babel_regional_format
|
from redbot.core.i18n import Translator, get_babel_locale, get_babel_regional_format
|
||||||
|
|
||||||
_ = Translator("UtilsChatFormatting", __file__)
|
_ = Translator("UtilsChatFormatting", __file__)
|
||||||
|
|
||||||
@ -352,21 +352,50 @@ def escape(text: str, *, mass_mentions: bool = False, formatting: bool = False)
|
|||||||
return text
|
return text
|
||||||
|
|
||||||
|
|
||||||
def humanize_list(items: Sequence[str]) -> str:
|
def humanize_list(
|
||||||
"""Get comma-separted list, with the last element joined with *and*.
|
items: Sequence[str], *, locale: Optional[str] = None, style: str = "standard"
|
||||||
|
) -> str:
|
||||||
This uses an Oxford comma, because without one, items containing
|
"""Get comma-separated list, with the last element joined with *and*.
|
||||||
the word *and* would make the output difficult to interpret.
|
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
items : Sequence[str]
|
items : Sequence[str]
|
||||||
The items of the list to join together.
|
The items of the list to join together.
|
||||||
|
locale : Optional[str]
|
||||||
|
The locale to convert, if not specified it defaults to the bot's locale.
|
||||||
|
style : str
|
||||||
|
The style to format the list with.
|
||||||
|
|
||||||
|
Note: Not all styles are necessarily available in all locales,
|
||||||
|
see documentation of `babel.lists.format_list` for more details.
|
||||||
|
|
||||||
|
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″"
|
||||||
|
|
||||||
Raises
|
Raises
|
||||||
------
|
------
|
||||||
IndexError
|
ValueError
|
||||||
An empty sequence was passed
|
The locale does not support the specified style.
|
||||||
|
|
||||||
Examples
|
Examples
|
||||||
--------
|
--------
|
||||||
@ -380,14 +409,12 @@ def humanize_list(items: Sequence[str]) -> str:
|
|||||||
'One, Two, and Three'
|
'One, Two, and Three'
|
||||||
>>> humanize_list(['One'])
|
>>> humanize_list(['One'])
|
||||||
'One'
|
'One'
|
||||||
|
>>> humanize_list(['omena', 'peruna', 'aplari'], style='or', locale='fi')
|
||||||
|
'omena, peruna tai aplari'
|
||||||
|
|
||||||
"""
|
"""
|
||||||
if len(items) == 1:
|
|
||||||
return items[0]
|
return babel_list(items, style=style, locale=get_babel_locale(locale))
|
||||||
try:
|
|
||||||
return ", ".join(items[:-1]) + _(", and ") + items[-1]
|
|
||||||
except IndexError:
|
|
||||||
raise IndexError("Cannot humanize empty sequence") from None
|
|
||||||
|
|
||||||
|
|
||||||
def format_perms_list(perms: discord.Permissions) -> str:
|
def format_perms_list(perms: discord.Permissions) -> str:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user