From d35f6abca07b9c53e60b4fbce4fac5eae7e00c99 Mon Sep 17 00:00:00 2001 From: jack1142 <6032823+jack1142@users.noreply.github.com> Date: Sat, 28 Mar 2020 23:11:25 +0100 Subject: [PATCH] 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 --- redbot/core/core_commands.py | 56 +++++++++++++----------------------- 1 file changed, 20 insertions(+), 36 deletions(-) diff --git a/redbot/core/core_commands.py b/redbot/core/core_commands.py index e0934aad0..c9f173335 100644 --- a/redbot/core/core_commands.py +++ b/redbot/core/core_commands.py @@ -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. + `` 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):