|
31 | 31 | from .openai_service import OpenAIService |
32 | 32 |
|
33 | 33 |
|
34 | | -# Map machinery `service` query param to the locale attribute that indicates whether |
35 | | -# the locale supports the MT service, used by the composed-translation view. |
36 | | -# `translation-memory` is special-cased to mean "TM-only, no MT fallback". |
37 | | -COMPOSED_MT_SERVICES = { |
38 | | - "google-translate": "google_translate_code", |
39 | | - "microsoft-translator": "ms_translator_code", |
40 | | -} |
41 | | - |
42 | | - |
43 | 34 | log = logging.getLogger(__name__) |
44 | 35 |
|
45 | 36 |
|
@@ -120,26 +111,33 @@ def machinery_composed(request): |
120 | 111 | {"status": False, "message": f"Bad Request: {e}"}, status=400 |
121 | 112 | ) |
122 | 113 |
|
123 | | - if service == "translation-memory": |
124 | | - # TM-only: no MT service is called (mt_supported=False). |
125 | | - mt_service = None |
126 | | - mt_service_name = "tm" |
127 | | - mt_supported = False |
128 | | - elif service in COMPOSED_MT_SERVICES: |
129 | | - if not request.user.is_authenticated: |
130 | | - return JsonResponse( |
131 | | - {"status": False, "message": "Authentication required"}, status=403 |
132 | | - ) |
133 | | - if service == "google-translate": |
| 114 | + match service: |
| 115 | + case "translation-memory": |
| 116 | + # TM-only: no MT service is called (mt_supported=False). |
| 117 | + mt_service = None |
| 118 | + mt_service_name = "tm" |
| 119 | + mt_supported = False |
| 120 | + case "google-translate": |
134 | 121 | mt_service = get_google_translate_data |
135 | | - else: |
| 122 | + mt_service_name = service |
| 123 | + mt_supported = bool(locale.google_translate_code) |
| 124 | + case "microsoft-translator": |
136 | 125 | mt_service = get_microsoft_translator_data |
137 | | - mt_service_name = service |
138 | | - mt_supported = bool(getattr(locale, COMPOSED_MT_SERVICES[service], None)) |
139 | | - else: |
| 126 | + mt_service_name = service |
| 127 | + mt_supported = bool(locale.ms_translator_code) |
| 128 | + case _: |
| 129 | + return JsonResponse( |
| 130 | + { |
| 131 | + "status": False, |
| 132 | + "message": f"Bad Request: unknown service `{service}`", |
| 133 | + }, |
| 134 | + status=400, |
| 135 | + ) |
| 136 | + |
| 137 | + # MT services call an external provider; translation-memory is anonymous-friendly. |
| 138 | + if mt_service is not None and not request.user.is_authenticated: |
140 | 139 | return JsonResponse( |
141 | | - {"status": False, "message": f"Bad Request: unknown service `{service}`"}, |
142 | | - status=400, |
| 140 | + {"status": False, "message": "Authentication required"}, status=403 |
143 | 141 | ) |
144 | 142 |
|
145 | 143 | # Only multi-pattern messages — those with multiple properties and/or |
|
0 commit comments