|
| 1 | +from typing import Any |
| 2 | +from typing_extensions import override |
| 3 | +from pydantic import BaseModel, Field |
| 4 | + |
| 5 | +LOCALES = ( |
| 6 | + "en-US", |
| 7 | + "en-GB", |
| 8 | + "bg", |
| 9 | + "zh-CN", |
| 10 | + "zh-TW", |
| 11 | + "hr", |
| 12 | + "cs", |
| 13 | + "da", |
| 14 | + "nl", |
| 15 | + "fi", |
| 16 | + "fr", |
| 17 | + "de", |
| 18 | + "el", |
| 19 | + "hi", |
| 20 | + "hu", |
| 21 | + "it", |
| 22 | + "ja", |
| 23 | + "ko", |
| 24 | + "lt", |
| 25 | + "no", |
| 26 | + "pl", |
| 27 | + "pt-BR", |
| 28 | + "ro", |
| 29 | + "ru", |
| 30 | + "es-ES", |
| 31 | + "es-419", |
| 32 | + "sv-SE", |
| 33 | + "th", |
| 34 | + "tr", |
| 35 | + "uk", |
| 36 | + "vi", |
| 37 | +) |
| 38 | +DEFAULT = "en-US" |
| 39 | + |
| 40 | + |
| 41 | +class RawTranslation(BaseModel): |
| 42 | + en_US: str | None = Field(None, alias="en-US") |
| 43 | + en_GB: str | None = Field(None, alias="en-GB") |
| 44 | + bg: str | None = None |
| 45 | + zh_CN: str | None = Field(None, alias="zh-CN") |
| 46 | + zh_TW: str | None = Field(None, alias="zh-TW") |
| 47 | + hr: str | None = None |
| 48 | + cs: str | None = None |
| 49 | + da: str | None = None |
| 50 | + nl: str | None = None |
| 51 | + fi: str | None = None |
| 52 | + fr: str | None = None |
| 53 | + de: str | None = None |
| 54 | + el: str | None = None |
| 55 | + hi: str | None = None |
| 56 | + hu: str | None = None |
| 57 | + it: str | None = None |
| 58 | + ja: str | None = None |
| 59 | + ko: str | None = None |
| 60 | + lt: str | None = None |
| 61 | + no: str | None = None |
| 62 | + pl: str | None = None |
| 63 | + pt_BR: str | None = Field(None, alias="pt-BR") |
| 64 | + ro: str | None = None |
| 65 | + ru: str | None = None |
| 66 | + es_ES: str | None = Field(None, alias="es-ES") |
| 67 | + es_419: str | None = Field(None, alias="es-419") |
| 68 | + sv_SE: str | None = Field(None, alias="sv-SE") |
| 69 | + th: str | None = None |
| 70 | + tr: str | None = None |
| 71 | + uk: str | None = None |
| 72 | + vi: str | None = None |
| 73 | + |
| 74 | + class Config: |
| 75 | + populate_by_name = True |
| 76 | + |
| 77 | + |
| 78 | +class Translation(BaseModel): |
| 79 | + def get_for_locale(self, locale: str) -> "TranslationWrapper": |
| 80 | + return apply_locale(self, locale) |
| 81 | + |
| 82 | + |
| 83 | +class TranslationWrapper: |
| 84 | + def __init__(self, model: "Translatable", locale: str, default: str = DEFAULT): |
| 85 | + self._model = model |
| 86 | + self._default: str |
| 87 | + self.default = default |
| 88 | + self._locale: str |
| 89 | + self.locale = locale.replace("-", "_") |
| 90 | + |
| 91 | + def __getattr__(self, key: str) -> Any: |
| 92 | + if isinstance(self._model, dict): |
| 93 | + applicable = self._model.get(key) |
| 94 | + if not applicable: |
| 95 | + raise AttributeError |
| 96 | + else: |
| 97 | + applicable = getattr(self._model, key) |
| 98 | + if isinstance(applicable, RawTranslation): |
| 99 | + try: |
| 100 | + print(id(self._model)) |
| 101 | + return getattr(applicable, self._locale) |
| 102 | + except AttributeError: |
| 103 | + return getattr(applicable, self._default) |
| 104 | + return apply_locale(applicable, self._locale) |
| 105 | + |
| 106 | + @property |
| 107 | + def locale(self) -> str: |
| 108 | + return self._locale |
| 109 | + |
| 110 | + @locale.setter |
| 111 | + def locale(self, value: str | None) -> None: # pyright: ignore[reportPropertyTypeMismatch] |
| 112 | + if value is None: |
| 113 | + value = self.default |
| 114 | + if value not in LOCALES: |
| 115 | + raise ValueError(f"Invalid locale {value}") |
| 116 | + self._locale = value |
| 117 | + |
| 118 | + @property |
| 119 | + def default(self) -> str: |
| 120 | + return self._default |
| 121 | + |
| 122 | + @default.setter |
| 123 | + def default(self, value: str) -> None: |
| 124 | + if value not in LOCALES: |
| 125 | + raise ValueError(f"Invalid locale {value}") |
| 126 | + self._default = value |
| 127 | + |
| 128 | + @override |
| 129 | + def __repr__(self) -> str: |
| 130 | + return repr(self._model) |
| 131 | + |
| 132 | + @override |
| 133 | + def __str__(self) -> str: |
| 134 | + return str(self._model) |
| 135 | + |
| 136 | + |
| 137 | +Translatable = Translation | dict[str, Translation] |
| 138 | + |
| 139 | + |
| 140 | +class NameDescriptionTranslation(Translation): |
| 141 | + name: RawTranslation | None = None |
| 142 | + description: RawTranslation | None = None |
| 143 | + |
| 144 | + |
| 145 | +class CommandTranslation(NameDescriptionTranslation): |
| 146 | + strings: dict[str, RawTranslation] | None = None |
| 147 | + options: dict[str, NameDescriptionTranslation] | None = None |
| 148 | + |
| 149 | + |
| 150 | +class Deg3CommandTranslation(CommandTranslation): ... |
| 151 | + |
| 152 | + |
| 153 | +class Deg2CommandTranslation(CommandTranslation): |
| 154 | + commands: dict[str, Deg3CommandTranslation] | None = None |
| 155 | + |
| 156 | + |
| 157 | +class Deg1CommandTranslation(CommandTranslation): |
| 158 | + commands: dict[str, Deg2CommandTranslation] | None = None |
| 159 | + |
| 160 | + |
| 161 | +AnyCommandTranslation = ( |
| 162 | + Deg1CommandTranslation | Deg2CommandTranslation | Deg3CommandTranslation |
| 163 | +) |
| 164 | + |
| 165 | + |
| 166 | +class ExtensionTranslation(Translation): |
| 167 | + commands: dict[str, Deg1CommandTranslation] | None = None |
| 168 | + strings: dict[str, RawTranslation] | None = None |
| 169 | + |
| 170 | + |
| 171 | +def apply_locale( |
| 172 | + model: "Translatable | TranslationWrapper", locale: str, default: str = DEFAULT |
| 173 | +) -> TranslationWrapper: |
| 174 | + if isinstance(model, TranslationWrapper): |
| 175 | + model.locale = locale |
| 176 | + model.default = default |
| 177 | + return model |
| 178 | + return TranslationWrapper(model, locale, default) |
0 commit comments