[Core] Guild scoped I18n #3896
No reviewers
Labels
No labels
Automated PR
Blocked
Blocked By: Damage Control
Blocked By: Dependency
Blocked By: Other PR
Breaking Change
Category: CI
Category: Cogs - Admin
Category: Cogs - Alias
Category: Cogs - Audio
Category: Cogs - Bank
Category: Cogs - Cleanup
Category: Cogs - CustomCommands
Category: Cogs - Dev
Category: Cogs - Downloader
Category: Cogs - Economy
Category: Cogs - Filter
Category: Cogs - General
Category: Cogs - Image
Category: Cogs - Mod
Category: Cogs - Modlog
Category: Cogs - Mutes
Category: Cogs - Permissions
Category: Cogs - Reports
Category: Cogs - Streams
Category: Cogs - Trivia
Category: Cogs - Trivia - Lists
Category: Cogs - Warnings
Category: Core - API - App Commands Package
Category: Core - API - Audio
Category: Core - API - Bank
Category: Core - API - Commands Package
Category: Core - API - Config
Category: Core - API - Other
Category: Core - API - Utils Package
Category: Core - Bot Class
Category: Core - Bot Commands
Category: Core - Command-line Interfaces
Category: Core - Downloader
Category: Core - Help
Category: Core - i18n
Category: Core - Modlog
Category: Core - Other Internals
Category: Core - RPC/ZMQ
Category: Docker
Category: Docs - Changelogs
Category: Docs - For Developers
Category: Docs - Install Guides
Category: Docs - Other
Category: Docs - User Guides
Category: Meta
Category: RPC/ZMQ methods
Category: Vendored Packages
Changelog Entry: Added
Changelog Entry: Pending
Changelog Entry: Skipped
Closed: Duplicate
Closed: External
Closed: Invalid
Closed: User Error
Closed: Won't Fix
Complex Issue
Docs-only
Feature Branch
Good First Issue
Help Wanted
High Priority
Needs Backport To stable-docs
No Activity
No Repro
QA: Bypassed
QA: Changes Requested
QA: Passed
Release Blocker
Status: Accepted
Status: Frozen
Status: In Progress
Status: Needs Discussion
Status: Needs Info
Status: Needs Triage
Status: PRs Welcome
Type: Bug
Type: Dependency Update
Type: Deprecation
Type: Enhancement
Type: Feature
Type: Informational
Type: Merge
Type: Optimisation
Type: Question
Type: Removal
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
Mrcoolgamer11/Red-DiscordBot!3896
Loading…
Reference in a new issue
No description provided.
Delete branch "pr/guild-i18n"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Never again!
Type
Description of the changes
Add Guild i18n support.
TODO:Clean up code
Add Guild scoped Babel Numeric formatter support.
Fixes #1970
Solved in https://github.com/Cog-Creators/Red-DiscordBot/pull/3896/commits/2210270f0bacd2556a566c0cd9b871eb24aef3ec
Guild scoped i18n currently has an issue whereby modlog cases cannot be translated, this is not the fault of i18n, Modlog doesn't account for i18n existing at all, this PR should be good to merge and modlog handling can be done at a latter date. If it proofs that people are interested in,
This appears to be working as intended, but I'm going to take a closer look a little later at the memory use and how it scales based on concerns the last time this came up (toby's old PR)
We may want a command for the bot owner to designate which locales are available, defaulting to all and release notes about this. Resource use grows as needed but will be higher.
Its a fair suggestion, but based on the calculations I did, I came around a total memory usage of 1.4 MB from French.
accounting for the fact that its ~460KB at 70% completion, adding another 30% on-top of that estimate leads it to being ~600KB of memory, doubling that to account for the overhead from Python.
Meaning that its very roughly ~1.5MB per language loaded.
I was seeing about 15mb increase from french alone when accounting for everything from contextvars as well, i wonder if there may be more to that growth than the i18n itself though, give me a few more to look into this.
Prior concern inflated by a tooling issue and a tool incorrectly assesing the lifecycle of contextvar held references.
There needs to be an exposed method for cog creators to set the contextvar to the appropriate locale from an event. Right now,
set_contextual_localeworks, but there's no supported way for cog creators to get the appropriate locale to set. (such as by asking by guild or guild id) This isn't necessary for command invokes, but would be needed for any messages sent by events to be handlable by cog creators.While people are directed to Red’s Crowdin page to see what translations are available in [p]set globallocale or [p]set locale, we don’t seem to have a list of the language-country code pairs listed anywhere. For ease of use I would suggest having them listed somewhere, whether that is a command (might be a bit overboard imo), more help text in those commands (might be too crowded/long), or a link to somewhere where we can list those. If something like that is already available in the bot elsewhere or listed on Crowdin I am not aware of it.
Edit: [p]listlocales was a thing at one point... I don't know where it went, checking the bot now.
2nd Edit: Please add listlocales back to the bot. Was removed in #3676
It might also be helpful to have a display of the current guild and global locales in the informational display under [p]set, as the regional format override is displayed there already. Ideally I would like to see both global and local information for locale and regional format exposed in that info display, but of course this is just a suggestion for ux and I’d like to see what other people think, or if there is another way to display that for users.
This is a little bit problematic - locale can be set to any valid language code, not just those that we offer translations for. For one, 3rd-party cog creators might provide translations for languages we don't provide translations for, but also the regional formatting functionality allows you to set the formatting of any region. That's why we started to link to Crowdin to see available translations for Red. But you are right that it could not be immediately obvious what to set it to.
It would be great if Crowdin could just show it right under the language in the grid but sadly it doesn't... I think the link would probably still be the best, but if there is some website that lists all language code in
en-US-like format, we could possibly link to that.The locales which we support are technically limited to validity under CLDR and recognized by babel
babel.localedata.locale_identifiers()[currently] generates a list of 758 of these, which would obviously be unruly for any realistic use by end-users.We could create an interactive command which is hidden but pointed to which helps pick a locale based on an interactive prompt and use of
babel.negotiate_locale(which compares requested locales with a set of "available" ones and tries to find the best fit, sacrificing region if no exact match) though this might create misleading circumstances when it results in recommending any of the languages we technically have Crowdin support for but under 10% translation.I don't really have a good solution for this other than perhaps a webpage which we control or trust which checks the browser info to tell the user what their locale code actually is and how much if any of that locale is translated.
Delaying this until I find time to implement the requested features.
I tested this for close to a week with a few thousand users/few servers using it and no one was able to find a way to cause a traceback or unintended behaviour. Made sure to try different combos of the global regional format override and various languages as well, but only tested languages with a good amount of coverage (mostly FR, RU, and DE).
I'm going to give this a try on a live bot and report back if I get any errors
You forgot to restrict the command to guilds only
This needs
@commands.guild_only()This needs
@commands.guild_only()Not really necessary
Will account for that
Edit: That said .... If the check is added before
@checks.guildowner_or_permissions(manage_guild=True)it could be benefitial here, for both clarity and an early exit.AAAA As Jack points out ..... Although this is the case forr everyone thats not a bot owner ... this would still fail for bot owners 🤦
So yes
@commands.guild_only()would be needed here to avoid bot owners running this in DMsThat would be why it was in my DMs
Added in a pending commit.
Might I suggest putting the bot's locale in brackets so guild admins know what the setting is?
pst and maybe add a lil dot on the end of the sentence as well.Yeah let's read the context next time.
a dot doesn't make sense here.
At this stage this is probably ready for an re-review.
Resolved in https://github.com/Cog-Creators/Red-DiscordBot/pull/3896/commits/9f07ae0b7e17f5e2dfaacb9d0ad8c58454d2d7bf
I am going to put this live on my bot for a week to see if there's any issues
It's been 8 days since I've had this running on a live instance now and I've had no issues related to this PR. 👍 Would recommend.
Review comments contain everything that needs to be changed.
Random thoughts that don't need addressing, but I thought might be worth saying:
@ -1574,0 +1582,4 @@"Regional format: {guild_regional_format}\n").format(admin=admin_roles_str,mod=mod_roles_str,This was intentional. It makes it obvious that the regional format is currently set to use bot's locale.
Same suggestion applies to guild regional format (which can be "Same as server's locale") as well as to guild's locale (which can be "Same as bot's locale").
@ -2046,0 +2082,4 @@i18n.set_contextual_locale(global_locale)await self.bot._i18n_cache.set_locale(ctx.guild, None)await ctx.send(_("Locale has been set to the default."))returnThis might be a bit clearer.
This might be a bit clearer.
Let's use our own APIs here:
@ -159,8 +245,9 @@ class Translator(Callable[[str], str]):This will look for the string in the translator's :code:`.pot` file,This method can be removed as it isn't used anywhere.
This line number reference doesn't make sense anymore and even if you update it, it will probably not make sense after some time again. Can you just refer to the relevant method name?
@ -59,0 +70,4 @@self._guild_locale[None] = global_localeif guild is None: # Not a guild so cannot support guild locale# Return the bot's globally set locale if its None on a guild scope.We should really be caching everything. Currently, in most cases we still end up calling config:
None(which will most likely be the case for most guilds we check it for)@ -59,0 +116,4 @@self._guild_regional_format[None] = global_regional_formatif guild is None: # Not a guild so cannot support guild localereturn self._guild_regional_format[None]Same comment as for
get_locale()method.But I also have one more thought. I think it would be more logical to use this resolution order for guild regional format:
It does seem to make some sense to me, but I am not really sure if it's actually better or worse than current resolution order which is:
// Edit: Hmm, it might require more rewriting, so I'm not sure whether it's really worth pursuing.
I know you haven't done all the changes yet, but I just wanted to let you know about the issues I've seen.
@ -56,6 +56,93 @@ class PrefixManager:await self._config.guild_from_id(gid).prefix.set(prefixes)class I18nManager:Looks like you didn't update this method and only updated
get_regional_formatting()method:@ -59,0 +89,4 @@@overloadasync def set_locale(self, guild: None, locale: str):...@ -159,8 +245,9 @@ class Translator(Callable[[str], str]):This will look for the string in the translator's :code:`.pot` file,Removed
@ -59,0 +70,4 @@self._guild_locale[None] = global_localeif guild is None: # Not a guild so cannot support guild locale# Return the bot's globally set locale if its None on a guild scope.Should be done.
@ -59,0 +116,4 @@self._guild_regional_format[None] = global_regional_formatif guild is None: # Not a guild so cannot support guild localereturn self._guild_regional_format[None]Theoretically should be done?
[p]set localeand[p]set regionalformatdon't update the i18n cache.Update: I meant
[p]set globallocaleand[p]set globalregionalformat, sorry about that 😬To add, I've tested everything else and the aforementioned cache is the only issue BUT there are some core cogs that need to set contextual locale manually in events/tasks:
lavalink_event_handler()(I am about 76% certain that this is the only place in Audio that needs this, we can ask Draper to get this closer to 99% territory)maybe_filter_name()andon_message()on_message()on_message()andon_raw_reaction_add()check_streams()I can help out with this if you don't feel like doing it all, just hit me up ;)
@ -1569,8 +1569,22 @@ class Core(commands.commands._RuleDropper, commands.Cog, CoreLogic):mod_role_ids = guild_data["mod_role"]The changes I just pushed around Mutes cog need testing which I'll be doing later today.
LGTM
😍