Handle invalid locale configuration graciously during startup (#6517)

This commit is contained in:
Jakub Kuczys 2025-02-06 01:42:42 +01:00 committed by GitHub
parent 769c319ffd
commit dfc1e742f8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 4 deletions

View File

@ -22,8 +22,9 @@ __all__ = (
)
FRESH_INSTALL_LOCALE = "en-US"
current_locale = ContextVar("current_locale")
current_locale_default = "en-US"
current_locale_default = FRESH_INSTALL_LOCALE
current_regional_format = ContextVar("current_regional_format")
current_regional_format_default = None

View File

@ -115,7 +115,7 @@ class Red(
owner=None,
whitelist=[],
blacklist=[],
locale="en-US",
locale=_i18n.FRESH_INSTALL_LOCALE,
regional_format=None,
embeds=True,
color=15158332,
@ -1145,9 +1145,28 @@ class Red(
self.owner_ids.add(self._owner_id_overwrite)
i18n_locale = await self._config.locale()
try:
_i18n.set_global_locale(i18n_locale)
except ValueError:
log.warning(
"The bot's global locale was set to an invalid value (%r)"
" and will be reset to default (%s).",
i18n_locale,
_i18n.FRESH_INSTALL_LOCALE,
)
i18n_locale = _i18n.FRESH_INSTALL_LOCALE
await self._config.locale.clear()
i18n_regional_format = await self._config.regional_format()
try:
_i18n.set_global_regional_format(i18n_regional_format)
except ValueError:
log.warning(
"The bot's global regional format was set to an invalid value (%r)"
" and will be reset to default (which is to inherit global locale, i.e. %s).",
i18n_regional_format,
i18n_locale,
)
await self._config.regional_format.clear()
async def _pre_connect(self) -> None:
"""