Skip to content
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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

eljeffeg
Copy link

@eljeffeg eljeffeg commented Apr 3, 2020

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:

header supported match
de_DE de_DE True
de_DE de True
de de True
de de_DE False

This change will fix that last one to:

header supported match
de de_DE True

This fixes the situation where the browser returns a two character locale, such as de, but the supported_locales de_DE.
Copy link
Member

@bdarnell bdarnell left a 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:
Copy link
Member

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?

Copy link
Author

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.

@eljeffeg
Copy link
Author

eljeffeg commented Jun 19, 2020

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.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants