Handle negative and float values in pluralize method

This commit is contained in:
2026-11-21 00:04:52 +01:00
parent 86f3b8a769
commit 1575bec4ac
4 changed files with 126 additions and 37 deletions
+17 -7
View File
@@ -181,14 +181,24 @@ no_count_text = i18n.p("files.found", None)
| Count | Category |
| --- | --- |
| `0` | `zero` |
| `1` | `one` |
| Greater than `1` | `many` |
| `0` (also `0.0` and `-0.0`) | `zero` |
| `1` (also `1.0`) and `-1` | `one` |
| Any other number (`2`, `-2`, `0.5`, `-1.5`, `2.5`, ...) | `many` |
| `None` | `none` |
Negative counts raise `ValueError`. The selected text follows the same current
language, English, `default`, and key fallback order as `t()`. The count is
available to formatting as `{count}`; `#count` is also replaced directly.
The count may be an integer or a float, positive or negative. The category is
chosen from its absolute value, so `-1` selects `one` like `1`. The selected
text follows the same current language, English, `default`, and key fallback
order as `t()`. The count is available to formatting as `{count}`; `#count` is
also replaced directly and inserts the count as Python prints it (for example
`2.5` or `-3`). Use `{count}` with a format specification to control the
number format:
```python
# "many": "{count:.1f} files"
i18n.p("files.found", 2.5) # "2.5 files"
i18n.p("files.found", -2) # "-2.0 files"
```
### Listen for language changes
@@ -239,7 +249,7 @@ it is intended for compatibility and inspection rather than normal lookup.
- `ValueError`: unknown language, invalid JSON, invalid translation structure,
missing or empty `language` metadata, plural entries missing `one` or
`many`, or a negative plural count.
or `many`.
- `FileNotFoundError`: the internal default language folder or its
`strings.json` is unavailable at construction, a selected language has no
usable translation file, or no current/English help file can be found.