-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix #1858 - locale closest match #2833
base: master
Are you sure you want to change the base?
Conversation
This fixes the situation where the browser returns a two character locale, such as de, but the supported_locales de_DE.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, but this change should really have a test.
I'm curious whether there's any particular reason you're using de_DE
as your supported language instead of just de
? The assumption behind this code (and the reason this issue went without notice for so long) was that you'd just use the two-letter codes until you needed multiple variants of the same language.
@@ -248,6 +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()) | |||
if len(parts) == 1: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be part of the elif len(parts)
blocks above?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using the elif len(parts)
would place it higher in the order of operations. There are two returns after the conditional statement, so I wanted it to be the last thing it tries after the others fail.
To be honest, it didn't occur to me to use two letters. lol I had just followed the docs and the examples had 4 letters, so that's what I used. The better solution for me would have been to just rename my locale files. |
This fixes the situation where the browser returns a two character locale, such as de, but the supported_locales includes de_DE. Prior to this fix, it would return en_US (default) as it would not match the two character Acceptable Language value against the prefix of supported_locales. It only did the reverse.
Currently Tornado does this:
This change will fix that last one to: