mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-06 03:08:55 -05:00
Use Babel in [p]set locale to allow any valid locale (#3676)
* Use Babel in `[p]set locale` to allow any valid locale * Remove `[p]listlocales` and link to Crowdin in `[p]set locale` * import babel.Locale as BabelLocale to avoid confousion
This commit is contained in:
parent
cf31c22e5d
commit
d35f6abca0
@ -20,6 +20,7 @@ from typing import TYPE_CHECKING, Union, Tuple, List, Optional, Iterable, Sequen
|
||||
import aiohttp
|
||||
import discord
|
||||
import pkg_resources
|
||||
from babel import Locale as BabelLocale, UnknownLocaleError
|
||||
|
||||
from . import (
|
||||
__version__,
|
||||
@ -1275,28 +1276,32 @@ class Core(commands.Cog, CoreLogic):
|
||||
|
||||
@_set.command()
|
||||
@checks.is_owner()
|
||||
async def locale(self, ctx: commands.Context, locale_name: str):
|
||||
async def locale(self, ctx: commands.Context, language_code: str):
|
||||
"""
|
||||
Changes bot locale.
|
||||
Changes bot's locale.
|
||||
|
||||
Use [p]listlocales to get a list of available locales.
|
||||
`<language_code>` can be any language code with country code included,
|
||||
e.g. `en-US`, `de-DE`, `fr-FR`, `pl-PL`, etc.
|
||||
|
||||
Go to Red's Crowdin page to see locales that are available with translations:
|
||||
https://translate.discord.red
|
||||
|
||||
To reset to English, use "en-US".
|
||||
"""
|
||||
red_dist = pkg_resources.get_distribution("red-discordbot")
|
||||
red_path = Path(red_dist.location) / "redbot"
|
||||
locale_list = [loc.stem.lower() for loc in list(red_path.glob("**/*.po"))]
|
||||
if locale_name.lower() in locale_list or locale_name.lower() == "en-us":
|
||||
i18n.set_locale(locale_name)
|
||||
await ctx.bot._config.locale.set(locale_name)
|
||||
await ctx.send(_("Locale has been set."))
|
||||
else:
|
||||
try:
|
||||
locale = BabelLocale.parse(language_code, sep="-")
|
||||
except (ValueError, UnknownLocaleError):
|
||||
await ctx.send(_("Invalid language code. Use format: `en-US`"))
|
||||
return
|
||||
if locale.territory is None:
|
||||
await ctx.send(
|
||||
_(
|
||||
"Invalid locale. Use `{prefix}listlocales` to get "
|
||||
"a list of available locales."
|
||||
).format(prefix=ctx.clean_prefix)
|
||||
_("Invalid format - language code has to include country code, e.g. `en-US`")
|
||||
)
|
||||
return
|
||||
standardized_locale_name = f"{locale.language}-{locale.territory}"
|
||||
i18n.set_locale(standardized_locale_name)
|
||||
await ctx.bot._config.locale.set(standardized_locale_name)
|
||||
await ctx.send(_("Locale has been set."))
|
||||
|
||||
@_set.command()
|
||||
@checks.is_owner()
|
||||
@ -1503,27 +1508,6 @@ class Core(commands.Cog, CoreLogic):
|
||||
await ctx.bot._config.help.tagline.set(tagline)
|
||||
await ctx.send(_("The tagline has been set."))
|
||||
|
||||
@commands.command()
|
||||
@checks.is_owner()
|
||||
async def listlocales(self, ctx: commands.Context):
|
||||
"""
|
||||
Lists all available locales
|
||||
|
||||
Use `[p]set locale` to set a locale
|
||||
"""
|
||||
async with ctx.channel.typing():
|
||||
red_dist = pkg_resources.get_distribution("red-discordbot")
|
||||
red_path = Path(red_dist.location) / "redbot"
|
||||
locale_list = [loc.stem for loc in list(red_path.glob("**/*.po"))]
|
||||
locale_list.append("en-US")
|
||||
locale_list = sorted(set(locale_list))
|
||||
if not locale_list:
|
||||
await ctx.send(_("No languages found."))
|
||||
return
|
||||
pages = pagify("\n".join(locale_list), shorten_by=26)
|
||||
|
||||
await ctx.send_interactive(pages, box_lang="Available Locales:")
|
||||
|
||||
@commands.command()
|
||||
@commands.cooldown(1, 60, commands.BucketType.user)
|
||||
async def contact(self, ctx: commands.Context, *, message: str):
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user