Handle negative and float values in pluralize method
This commit is contained in:
+18
-10
@@ -326,14 +326,17 @@ i18n.t("wert", n=42) # formats e.g. "{n:05d}" -> "0004
|
||||
|
||||
Translates a pluralized key.
|
||||
|
||||
The category is chosen from `count`:
|
||||
`count` may be an `int` or a `float`, positive or negative. The category is
|
||||
chosen from its **absolute value**:
|
||||
|
||||
| `count` | Category |
|
||||
| --- | --- |
|
||||
| `None` | `none` |
|
||||
| `0` | `zero` |
|
||||
| `1` | `one` |
|
||||
| greater than 1 | `many` |
|
||||
| `0`, `0.0`, `-0.0` | `zero` |
|
||||
| `1`, `1.0`, `-1` | `one` |
|
||||
| any other number, e.g. `2`, `-2`, `0.5`, `-1.5`, `2.5` | `many` |
|
||||
|
||||
Values such as `NaN` and infinity also select `many`.
|
||||
|
||||
Lookup order: active language → English (same category) → `default` → `key`.
|
||||
A plain string value is also accepted and used for every category, which helps
|
||||
@@ -342,14 +345,18 @@ with partially converted resources.
|
||||
In the selected text, `#count` is replaced by the count, and the text is then
|
||||
formatted with `count` plus any keyword arguments, so `{count}` and extra
|
||||
placeholders work. A failed formatting returns the text with `#count` already
|
||||
replaced.
|
||||
replaced. `#count` inserts the count as Python prints it (`2.5`, `-3`); use a
|
||||
format specification such as `{count:.1f}` to control the number format.
|
||||
|
||||
**Raises** `ValueError` for negative counts (checked before the lookup).
|
||||
**Raises** `TypeError` if `count` is neither a number nor `None`.
|
||||
|
||||
```python
|
||||
i18n.p("files.found", 0) # "No files found"
|
||||
i18n.p("files.found", 1) # "One file found"
|
||||
i18n.p("files.found", -1) # "One file found" (absolute value)
|
||||
i18n.p("files.found", 5) # "5 files found"
|
||||
i18n.p("files.found", 2.5) # "2.5 files found"
|
||||
i18n.p("files.found", -2) # "-2 files found"
|
||||
i18n.p("files.found", None) # text of the "none" category
|
||||
i18n.p("files.found", 3, default="{count} things") # used only if the key is unknown
|
||||
```
|
||||
@@ -581,7 +588,7 @@ Ki18n("en", local_root=data_dir("my-app"))
|
||||
| `FileNotFoundError` | `set_language` | The language's files disappeared after start-up. |
|
||||
| `ValueError` | `set_language` | Unknown language or invalid ID. |
|
||||
| `ValueError` | `t` | Key refers to a plural object. |
|
||||
| `ValueError` | `p` | Negative count. |
|
||||
| `TypeError` | `p` | `count` is neither a number nor `None`. |
|
||||
| `FileNotFoundError` | `get_help` | No help file for the active language or English. |
|
||||
|
||||
After any exception from `set_language`, the previously active language remains
|
||||
@@ -609,9 +616,10 @@ These points are based on reading the current source code.
|
||||
`IndexError` and `ValueError` from `str.format`. Placeholders that raise
|
||||
`AttributeError` or `TypeError` (for example `{name.attr}` or `{name[x]}` with
|
||||
a string value) propagate to the caller.
|
||||
- **Fractional counts below 1.** `p()` accepts floats, but values between 0 and 1
|
||||
(for example `0.5`) match no category and raise `ValueError` with the message
|
||||
`count must be nonnegative or None`.
|
||||
- **Simple plural rules.** `p()` knows only the categories `zero`, `one`, `many`
|
||||
and `none`, chosen from the absolute value of the count. Language-specific
|
||||
plural rules (for example separate forms for 2–4 in Slavic languages) are not
|
||||
supported, and fractions such as `1.5` always use `many`.
|
||||
- **No automatic reload.** Resource files are read when a language is activated;
|
||||
selecting the already active language does nothing.
|
||||
- **Plain Markdown help.** `help.md` is returned verbatim; there is no merge with
|
||||
|
||||
Reference in New Issue
Block a user