rewrite: Setting input strings and localisation.

This commit is contained in:
2023-06-06 12:57:29 +03:00
parent 809cada228
commit e1a23695ee
29 changed files with 823 additions and 236 deletions

View File

@@ -4,6 +4,7 @@ from settings.setting_types import StringSetting, BoolSetting
from settings.groups import SettingGroup
from meta.errors import UserInputError
from meta.context import ctx_bot
from core.data import CoreData
from .translator import ctx_translator
@@ -17,11 +18,30 @@ class LocaleSetting(StringSetting):
"""
Base class describing a LocaleSetting.
"""
_accepts = _p(
'settype:locale|accepts',
"Enter a supported language (e.g. 'en-GB')."
)
def _desc_table(self) -> list[str]:
translator = ctx_translator.get()
t = translator.t
lines = super()._desc_table()
lines.append((
t(_p(
'settype:locale|summary_table|field:supported|key',
"Supported"
)),
', '.join(f"`{locale}`" for locale in translator.supported_locales)
))
return lines
@classmethod
def _format_data(cls, parent_id, data, **kwargs):
t = ctx_translator.get().t
if data is None:
formatted = t(_p('set_type:locale|formatted:unset', "Unset"))
formatted = t(_p('settype:locale|formatted:unset', "Unset"))
else:
name = locale_names.get(data, None)
if name:
@@ -37,7 +57,7 @@ class LocaleSetting(StringSetting):
lang = string[:20]
raise UserInputError(
translator.t(
_p('set_type:locale|error', "Sorry, we do not support the language `{lang}` at this time!")
_p('settype:locale|error', "Sorry, we do not support the language `{lang}` at this time!")
).format(lang=lang)
)
return string
@@ -54,6 +74,11 @@ class LocaleSettings(SettingGroup):
_display_name = _p('userset:locale', 'language')
_desc = _p('userset:locale|desc', "Your preferred language for interacting with me.")
_long_desc = _p(
'userset:locale|long_desc',
"The language you would prefer me to respond to commands and interactions in. "
"Servers may be configured to override this with their own language."
)
_model = CoreData.User
_column = CoreData.User.locale.name
@@ -68,6 +93,12 @@ class LocaleSettings(SettingGroup):
lang=self.formatted
)
@property
def set_str(self):
bot = ctx_bot.get()
if bot:
return bot.core.mention_cmd('my language')
class ForceLocale(ModelData, BoolSetting):
"""
Guild configuration for whether to force usage of the guild locale.
@@ -108,10 +139,11 @@ class LocaleSettings(SettingGroup):
"I will now allow the members to set their own language here."
))
@classmethod
def _format_data(cls, parent_id, data, **kwargs):
t = ctx_translator.get().t
return t(cls._outputs[data])
@property
def set_str(self):
bot = ctx_bot.get()
if bot:
return bot.core.mention_cmd('configure language')
class GuildLocale(ModelData, LocaleSetting):
"""
@@ -142,3 +174,9 @@ class LocaleSettings(SettingGroup):
return t(_p('guildset:locale|response', "You have set the guild language to {lang}.")).format(
lang=self.formatted
)
@property
def set_str(self):
bot = ctx_bot.get()
if bot:
return bot.core.mention_cmd('configure language')