Refactor TranslationContext to be more useful

The previous enum was good at accomplishing dynamic key generation for
a few cases, but it fell short in others:

1. It could not discern group names and command names
2. It could not give you more contextual data such as the full object
   currently being translated.

On top of that, the context being a required parameter for
Translator.translate meant that it wouldn't be possible to re-use the
translator for other use cases outside of the rigid ones defined in the
library.

To alleviate these concerns, new enum attributes were added along with
a richer type for obtaining even more context.
This commit is contained in:
Rapptz
2022-08-09 10:36:30 -04:00
parent d826f4f3a8
commit c32567ea81
5 changed files with 136 additions and 34 deletions

View File

@ -26,7 +26,7 @@ from __future__ import annotations
from datetime import datetime
from .errors import MissingApplicationID
from .translator import Translator, TranslationContext, locale_str
from .translator import TranslationContextLocation, Translator, TranslationContext, locale_str
from ..permissions import Permissions
from ..enums import AppCommandOptionType, AppCommandType, AppCommandPermissionType, ChannelType, Locale, try_enum
from ..mixins import Hashable
@ -463,9 +463,10 @@ class Choice(Generic[ChoiceT]):
async def get_translated_payload(self, translator: Translator) -> Dict[str, Any]:
base = self.to_dict()
name_localizations: Dict[str, str] = {}
context = TranslationContext(location=TranslationContextLocation.choice_name, data=self)
if self._locale_name:
for locale in Locale:
translation = await translator._checked_translate(self._locale_name, locale, TranslationContext.choice_name)
translation = await translator._checked_translate(self._locale_name, locale, context)
if translation is not None:
name_localizations[locale.value] = translation