From 32016bf8b2628a929ca6781618d5ed17cb5c5116 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Wed, 16 Nov 2022 17:13:59 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=A5=85=20Canonicalize=20requested=20local?= =?UTF-8?q?es?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This way ?l10n=zh_hk will work too, not just ?l10n=zh_hant_hk Additionally, a comma separated list of l10n options can now also be provided via query or header, not just a single value: ?l10n=zh_hk,zh See #4445 --- src/octoprint/server/__init__.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/octoprint/server/__init__.py b/src/octoprint/server/__init__.py index a5411ccd2..c8a77dbbc 100644 --- a/src/octoprint/server/__init__.py +++ b/src/octoprint/server/__init__.py @@ -1342,11 +1342,11 @@ class Server: if "l10n" in request.values: # request: query param - l10n = request.values["l10n"] + l10n = request.values["l10n"].split(",") elif "X-Locale" in request.headers: # request: header - l10n = request.headers["X-Locale"] + l10n = request.headers["X-Locale"].split(",") elif hasattr(g, "identity") and g.identity: # user setting @@ -1356,7 +1356,7 @@ class Server: userid, ("interface", "language") ) if user_language is not None and not user_language == "_default": - l10n = user_language + l10n = [user_language] except octoprint.access.users.UnknownUser: pass @@ -1366,10 +1366,18 @@ class Server: and default_language in LANGUAGES ): # instance setting - l10n = default_language + l10n = [default_language] if l10n: - return Locale.negotiate([l10n], LANGUAGES) + # canonicalize and get rid of invalid language codes + l10n_canoicalized = [] + for x in l10n: + try: + l10n_canoicalized.append(str(Locale.parse(x))) + except Exception: + # invalid language code, ignore + continue + return Locale.negotiate(l10n_canoicalized, LANGUAGES) # request: preference return Locale.parse(request.accept_languages.best_match(LANGUAGES))