From 24fec19787dfbaca24e4ae0433e0e648697a00b5 Mon Sep 17 00:00:00 2001 From: ElJefe Date: Fri, 3 Apr 2020 11:07:35 -0400 Subject: [PATCH 1/4] Fix #1858 - locale closest match This fixes the situation where the browser returns a two character locale, such as de, but the supported_locales de_DE. --- tornado/locale.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tornado/locale.py b/tornado/locale.py index 52aa1af9d5..083f2fba2c 100644 --- a/tornado/locale.py +++ b/tornado/locale.py @@ -238,7 +238,7 @@ def get_closest(cls, *locale_codes: str) -> "Locale": for code in locale_codes: if not code: continue - code = code.replace("-", "_") + code = code.replace("-", "_").split(";")[0] parts = code.split("_") if len(parts) > 2: continue @@ -248,6 +248,9 @@ def get_closest(cls, *locale_codes: str) -> "Locale": return cls.get(code) if parts[0].lower() in _supported_locales: return cls.get(parts[0].lower()) + for supported in _supported_locales: + if code == supported.split("_")[0]: + return cls.get(supported) return cls.get(_default_locale) @classmethod From a4d6d4826d06b1e41fd33265f4a390690db125ce Mon Sep 17 00:00:00 2001 From: ElJefe Date: Fri, 3 Apr 2020 13:34:42 -0400 Subject: [PATCH 2/4] Check length for performance - should be 1 --- tornado/locale.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tornado/locale.py b/tornado/locale.py index 083f2fba2c..c3fc1b017d 100644 --- a/tornado/locale.py +++ b/tornado/locale.py @@ -248,9 +248,10 @@ def get_closest(cls, *locale_codes: str) -> "Locale": return cls.get(code) if parts[0].lower() in _supported_locales: return cls.get(parts[0].lower()) - for supported in _supported_locales: - if code == supported.split("_")[0]: - return cls.get(supported) + if len(parts) == 1: + for supported in _supported_locales: + if code == supported.split("_")[0]: + return cls.get(supported) return cls.get(_default_locale) @classmethod From f60ed573e7d5706926b3e5711318ab096720acd5 Mon Sep 17 00:00:00 2001 From: ElJefe Date: Fri, 3 Apr 2020 13:44:33 -0400 Subject: [PATCH 3/4] Forces lowercase on code --- tornado/locale.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tornado/locale.py b/tornado/locale.py index c3fc1b017d..74b92a2414 100644 --- a/tornado/locale.py +++ b/tornado/locale.py @@ -250,7 +250,7 @@ def get_closest(cls, *locale_codes: str) -> "Locale": return cls.get(parts[0].lower()) if len(parts) == 1: for supported in _supported_locales: - if code == supported.split("_")[0]: + if parts[0].lower() == supported.split("_")[0]: return cls.get(supported) return cls.get(_default_locale) From f910d8499f531dcd4a5d511ab0427fac7c7344b1 Mon Sep 17 00:00:00 2001 From: ElJefe Date: Fri, 3 Apr 2020 17:04:53 -0400 Subject: [PATCH 4/4] Already done in get_browser_locale --- tornado/locale.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tornado/locale.py b/tornado/locale.py index 74b92a2414..aea9e1f3bb 100644 --- a/tornado/locale.py +++ b/tornado/locale.py @@ -238,7 +238,7 @@ def get_closest(cls, *locale_codes: str) -> "Locale": for code in locale_codes: if not code: continue - code = code.replace("-", "_").split(";")[0] + code = code.replace("-", "_") parts = code.split("_") if len(parts) > 2: continue