Opened 12 months ago

Last modified 9 months ago

#27486 new Cleanup/optimization

intword and filesizeformat filters shouldn't pass non-integers to gettext

Reported by: Tim Graham Owned by:
Component: Template system Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Python plans to deprecate selecting plural form by fractional numbers. The intword and filesizeformat template filters are currently doing this:

$ python -Wall tests/runtests.py humanize_tests.tests.HumanizeTests.test_i18n_intword
Testing against Django installed in '/home/tim/code/django/django' with up to 3 processes
Creating test database for alias 'default'...
Creating test database for alias 'other'...
/home/tim/code/django/django/utils/translation/trans_real.py:373: DeprecationWarning: Plural value must be an integer, got float
  return getattr(t, translation_function)(singular, plural, number)
.
python -Wall tests/runtests.py template_tests.filter_tests.test_filesizeformat.FunctionTests.test_localized_formats 
Testing against Django installed in '/home/tim/code/django/django' with up to 3 processes
Creating test database for alias 'default'...
Creating test database for alias 'other'...
/home/tim/code/django/django/utils/translation/trans_real.py:373: DeprecationWarning: Plural value must be an integer, got float
  return getattr(t, translation_function)(singular, plural, number)
.

Change History (3)

comment:1 Changed 12 months ago by Claude Paroz

The exact rule about pluralizing floats/decimals depends on the language, so whether we floor or ceil the number before passing it to ngettext we'll do the right thing for some languages and wrong for others. This is a known limitation of the gettext spec (in C, ngettext is only accepting integers). We should make a choice and then document the limitation.

It would not be impossible to try providing a language-specific hook in the LANG_INFO structure and call that hook when converting floats to ints.

comment:2 Changed 12 months ago by Tim Graham

My PR does naive int() casting. I closed it since it seems a more involved solution would be better. Perhaps we could look to see how other projects handle the issue.

comment:3 Changed 9 months ago by Tim Graham

Owner: Tim Graham deleted
Status: assignednew
Note: See TracTickets for help on using tickets.
Back to Top