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

i18n: Handle missing babel locale again #5965

Merged
merged 2 commits into from Feb 13, 2017
Merged

Conversation

unho
Copy link
Member

@unho unho commented Feb 10, 2017

Fixes: 'UnknownLocaleError: unknown locale 'son'

@unho unho force-pushed the songhay-fix-again branch 4 times, most recently from b4904af to 33e0251 Compare February 13, 2017 09:34
try:
return format_timedelta(timedelta, locale=locale)
except ValueError:
return format_timedelta(timedelta, locale='en')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a test.

This seems like it's following the same pattern from the bits I coded. Can you not refactor that out into a function that devolves to a workable locale.

try:
return format_timedelta(timedelta, locale=locale)
except UnknownLocaleError:
locale = settings.LANGUAGE_CODE.split("-")[0]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely a no no. I'm pretty sure I used something that can give you a valid code.

@unho unho force-pushed the songhay-fix-again branch 5 times, most recently from ddb2c6c to 3158dc5 Compare February 13, 2017 13:11
@unho unho force-pushed the songhay-fix-again branch 2 times, most recently from cac82ae to 82e1a90 Compare February 13, 2017 14:14
('en-za', 'en'),
('en-us', 'en'),
# Missing in babel
('son', 'en'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bad one to test, this test will be bust as soon as son comes to babel. Rather us a really broken one that won't ever appear in CLDR

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are testing here that it catches the error and falls back to default locale.

@pytest.mark.parametrize('language,fallback', [
# Normal
('af', 'af'),
('en-za', 'en'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing that this passes by accident rather than by design. For numbers it will fail. So really not sure what we're testing here.

for language in [get_language(), settings.LANGUAGE_CODE, 'en-us']:
def get_locale_formats(locale=None):
languages = [locale] if locale else []
languages += [get_language(), settings.LANGUAGE_CODE, 'en-us', 'en']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was there a specific reason to fallback to en? Since our app is in en-us it doesn't make much sense to me. And I suspect in CLDR en-us == en so kinda pointless step IIUC and just confuses the matter with no justification in the code or the commit

try:
locale = babel_core.Locale.parse(to_locale(language))
break
except UnknownLocaleError:
except (UnknownLocaleError, ValueError):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to catch ValueError? This sounds remarkably like Except catchall but without info I'm not sure.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems babel_support.Format(babel_core.Locale.parse(to_locale('en-us'))).timedelta(timedelta, format='long') is not the same as format_timedelta(timedelta, locale='en-us'). The latter actually yields a ValueError and thus the default to en. Changed PR to remove all of that.

locale = babel_core.Locale.parse(to_locale(language))
break
except UnknownLocaleError:
continue
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this test anything? Can we actually have a test that flags the UnkownLocaleError so we know that we're succeeding in capturing that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rearranged the test to drop this code.

@unho unho force-pushed the songhay-fix-again branch 2 times, most recently from 55fc0f0 to f52f980 Compare February 13, 2017 16:36
@dwaynebailey
Copy link
Member

lgtm

LocalDate is dropped since the right babel locale formatter can
be already retrieved by using the more reliable get_locale_formats
function which provides sane defaults to fall back to in case of
locale being missing in babel.

The leftover code after this refactoring is simple enough to be folded
into the timesince function. Also did minor refactor to get_locale_formats
in order to allow telling to retrieve a specific locale, which eases
testing specific locales formatting.

Fixes: 'UnknownLocaleError: unknown locale 'son'
@unho unho merged commit 898316d into translate:master Feb 13, 2017
@unho unho deleted the songhay-fix-again branch February 13, 2017 17:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants