|
4 | 4 |
|
5 | 5 | from couchers import experimentation |
6 | 6 | from couchers.i18n import LocalizationContext |
| 7 | +from couchers.i18n.locales import get_translation_component |
7 | 8 |
|
8 | 9 | if TYPE_CHECKING: |
9 | 10 | from growthbook import GrowthBook |
@@ -122,17 +123,30 @@ def abort(self, status_code: grpc.StatusCode, error_message: str) -> NoReturn: |
122 | 123 | context.abort(status_code, error_message) |
123 | 124 |
|
124 | 125 | def abort_with_error_code( |
125 | | - self, status_code: grpc.StatusCode, error_message_id: str, *, substitutions: dict[str, str | int] | None = None |
| 126 | + self, |
| 127 | + status_code: grpc.StatusCode, |
| 128 | + error_message_id: str, |
| 129 | + *, |
| 130 | + substitutions: dict[str, str | int] | None = None, |
126 | 131 | ) -> NoReturn: |
127 | 132 | """ |
128 | 133 | Raises an error that's returned to the user, but error_message_id should be an entry from translateable errors |
| 134 | +
|
| 135 | + error_message_id may be namespaced with a translation component, like i18next, e.g. "admin:object_not_found" |
| 136 | + looks up "object_not_found" in the "admin" component (where admin/editor errors live). Without a prefix the |
| 137 | + "main" component is used. |
129 | 138 | """ |
130 | 139 | if not self.__is_interactive: |
131 | 140 | raise NonInteractiveAbortException(status_code, error_message_id) |
132 | 141 | else: |
133 | 142 | context = cast(grpc.ServicerContext, self._grpc_context) |
| 143 | + component, _, error_name = error_message_id.rpartition(":") |
134 | 144 | # Get the translated error message using the user's language preference |
135 | | - error_message = self.localization.localize_string(f"errors.{error_message_id}", substitutions=substitutions) |
| 145 | + error_message = self.localization.localize_string( |
| 146 | + f"errors.{error_name}", |
| 147 | + i18next=get_translation_component(component or "main"), |
| 148 | + substitutions=substitutions, |
| 149 | + ) |
136 | 150 | context.abort(status_code, error_message) |
137 | 151 |
|
138 | 152 | def set_cookies(self, cookies: list[str]) -> None: |
|
0 commit comments