mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-21 02:16:09 -05:00
[Core] Guild scoped I18n (#3896)
* Guild I18n Never again! * Finish off guild scoped i18n * Black formatting * Added guild only flags. * Fix missing import. * Added listing of guild i18n settings * Added API support * Added API support... properly! * Added API support... for realsies! * Auto-translate create_cases instances You're welcome cog creators! Jack talked me into this! * Fix get_regional_format to actually return properly * Cleanup `set showsettings` * Style pass * Update redbot/core/core_commands.py Co-authored-by: jack1142 <6032823+jack1142@users.noreply.github.com> * Update redbot/core/events.py Co-authored-by: jack1142 <6032823+jack1142@users.noreply.github.com> * Update redbot/core/core_commands.py Co-authored-by: jack1142 <6032823+jack1142@users.noreply.github.com> * Fix missing import * Improve caching * Removal of unneeded function * Fix some naming * IDFK anymore... * Reformat * Update redbot/core/settings_caches.py Co-authored-by: jack1142 <6032823+jack1142@users.noreply.github.com> * Update redbot/core/settings_caches.py Co-authored-by: jack1142 <6032823+jack1142@users.noreply.github.com> * Update redbot/core/settings_caches.py Co-authored-by: jack1142 <6032823+jack1142@users.noreply.github.com> * Remove line number * Fix global sets * Set contextual locale manually where needed * Reports cog is wonderful... * Update redbot/core/core_commands.py Co-authored-by: Draper <27962761+Drapersniper@users.noreply.github.com> * Set contextual locale manually where needed in Mutes cog * s Co-authored-by: jack1142 <6032823+jack1142@users.noreply.github.com> Co-authored-by: Draper <27962761+Drapersniper@users.noreply.github.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Dict, List, Optional, Union, Set, Iterable, Tuple
|
||||
from typing import Dict, List, Optional, Union, Set, Iterable, Tuple, overload
|
||||
import asyncio
|
||||
from argparse import Namespace
|
||||
from collections import defaultdict
|
||||
@@ -56,6 +56,93 @@ class PrefixManager:
|
||||
await self._config.guild_from_id(gid).prefix.set(prefixes)
|
||||
|
||||
|
||||
class I18nManager:
|
||||
def __init__(self, config: Config):
|
||||
self._config: Config = config
|
||||
self._guild_locale: Dict[Union[int, None], Union[str, None]] = {}
|
||||
self._guild_regional_format: Dict[Union[int, None], Union[str, None]] = {}
|
||||
|
||||
async def get_locale(self, guild: Union[discord.Guild, None]) -> str:
|
||||
"""Get the guild locale from the cache"""
|
||||
# Ensure global locale is in the cache
|
||||
if None not in self._guild_locale:
|
||||
global_locale = await self._config.locale()
|
||||
self._guild_locale[None] = global_locale
|
||||
|
||||
if 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.
|
||||
return self._guild_locale[None]
|
||||
elif guild.id in self._guild_locale: # Cached guild
|
||||
if self._guild_locale[guild.id] is None:
|
||||
return self._guild_locale[None]
|
||||
else:
|
||||
return self._guild_locale[guild.id]
|
||||
else: # Uncached guild
|
||||
out = await self._config.guild(guild).locale() # No locale set
|
||||
if out is None:
|
||||
self._guild_locale[guild.id] = None
|
||||
return self._guild_locale[None]
|
||||
else:
|
||||
self._guild_locale[guild.id] = out
|
||||
return out
|
||||
|
||||
@overload
|
||||
async def set_locale(self, guild: None, locale: str):
|
||||
...
|
||||
|
||||
@overload
|
||||
async def set_locale(self, guild: discord.Guild, locale: Union[str, None]):
|
||||
...
|
||||
|
||||
async def set_locale(
|
||||
self, guild: Union[discord.Guild, None], locale: Union[str, None]
|
||||
) -> None:
|
||||
"""Set the locale in the config and cache"""
|
||||
if guild is None:
|
||||
if locale is None:
|
||||
# this method should never be called like this
|
||||
raise ValueError("Global locale can't be None!")
|
||||
self._guild_locale[None] = locale
|
||||
await self._config.locale.set(locale)
|
||||
return
|
||||
self._guild_locale[guild.id] = locale
|
||||
await self._config.guild(guild).locale.set(locale)
|
||||
|
||||
async def get_regional_format(self, guild: Union[discord.Guild, None]) -> Optional[str]:
|
||||
"""Get the regional format from the cache"""
|
||||
# Ensure global locale is in the cache
|
||||
if None not in self._guild_regional_format:
|
||||
global_regional_format = await self._config.regional_format()
|
||||
self._guild_regional_format[None] = global_regional_format
|
||||
|
||||
if guild is None: # Not a guild so cannot support guild locale
|
||||
return self._guild_regional_format[None]
|
||||
elif guild.id in self._guild_regional_format: # Cached guild
|
||||
if self._guild_regional_format[guild.id] is None:
|
||||
return self._guild_regional_format[None]
|
||||
else:
|
||||
return self._guild_regional_format[guild.id]
|
||||
else: # Uncached guild
|
||||
out = await self._config.guild(guild).regional_format() # No locale set
|
||||
if out is None:
|
||||
self._guild_regional_format[guild.id] = None
|
||||
return self._guild_regional_format[None]
|
||||
else: # Not cached, got a custom regional format.
|
||||
self._guild_regional_format[guild.id] = out
|
||||
return out
|
||||
|
||||
async def set_regional_format(
|
||||
self, guild: Union[discord.Guild, None], regional_format: Union[str, None]
|
||||
) -> None:
|
||||
"""Set the regional format in the config and cache"""
|
||||
if guild is None:
|
||||
self._guild_regional_format[None] = regional_format
|
||||
await self._config.regional_format.set(regional_format)
|
||||
return
|
||||
self._guild_regional_format[guild.id] = regional_format
|
||||
await self._config.guild(guild).regional_format.set(regional_format)
|
||||
|
||||
|
||||
class IgnoreManager:
|
||||
def __init__(self, config: Config):
|
||||
self._config: Config = config
|
||||
|
||||
Reference in New Issue
Block a user