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:
jack1142 2020-03-28 23:11:25 +01:00 committed by GitHub
parent cf31c22e5d
commit d35f6abca0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -20,6 +20,7 @@ from typing import TYPE_CHECKING, Union, Tuple, List, Optional, Iterable, Sequen
import aiohttp import aiohttp
import discord import discord
import pkg_resources import pkg_resources
from babel import Locale as BabelLocale, UnknownLocaleError
from . import ( from . import (
__version__, __version__,
@ -1275,28 +1276,32 @@ class Core(commands.Cog, CoreLogic):
@_set.command() @_set.command()
@checks.is_owner() @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". To reset to English, use "en-US".
""" """
red_dist = pkg_resources.get_distribution("red-discordbot") try:
red_path = Path(red_dist.location) / "redbot" locale = BabelLocale.parse(language_code, sep="-")
locale_list = [loc.stem.lower() for loc in list(red_path.glob("**/*.po"))] except (ValueError, UnknownLocaleError):
if locale_name.lower() in locale_list or locale_name.lower() == "en-us": await ctx.send(_("Invalid language code. Use format: `en-US`"))
i18n.set_locale(locale_name) return
await ctx.bot._config.locale.set(locale_name) if locale.territory is None:
await ctx.send(_("Locale has been set."))
else:
await ctx.send( await ctx.send(
_( _("Invalid format - language code has to include country code, e.g. `en-US`")
"Invalid locale. Use `{prefix}listlocales` to get "
"a list of available locales."
).format(prefix=ctx.clean_prefix)
) )
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() @_set.command()
@checks.is_owner() @checks.is_owner()
@ -1503,27 +1508,6 @@ class Core(commands.Cog, CoreLogic):
await ctx.bot._config.help.tagline.set(tagline) await ctx.bot._config.help.tagline.set(tagline)
await ctx.send(_("The tagline has been set.")) 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.command()
@commands.cooldown(1, 60, commands.BucketType.user) @commands.cooldown(1, 60, commands.BucketType.user)
async def contact(self, ctx: commands.Context, *, message: str): async def contact(self, ctx: commands.Context, *, message: str):