Ki18n.__init__ now uses bundle_root() if project_root parameter is missing

This commit is contained in:
2026-11-21 10:20:00 +01:00
parent 94f220e00d
commit bdb1e9dfe9
5 changed files with 188 additions and 70 deletions
+17 -21
View File
@@ -48,8 +48,10 @@ following entries in all supported languages:
The library searches the user's application data tree first, then the
application's installation tree. The application name is the name of
`project_root` (or the directory containing the executed program when
`project_root` is omitted).
`project_root` (or of the application root returned by
`ki18n.paths.bundle_root()` when `project_root` is omitted). In a frozen
(PyInstaller) build the name of the executable is used instead of the name of
the temporary extraction directory.
- Linux and other Unix-like systems: `$XDG_DATA_HOME/<app-name>/i18n/`, or
`~/.local/share/<app-name>/i18n/` when `XDG_DATA_HOME` is unset.
@@ -126,8 +128,12 @@ the discovered mapping or override the display name for an installed language.
initializes it; later calls return that same instance and ignore their
configuration arguments. Configure it once near application startup.
If omitted, `project_root` is derived from the directory containing
`sys.argv[0]`. The application installation resources are then read from
If omitted, `project_root` is the application root returned by
`ki18n.paths.bundle_root()`: in a frozen (PyInstaller) build `sys._MEIPASS`,
otherwise the nearest directory that contains an `i18n/` folder, searching from
the directory of `sys.argv[0]` upwards (or that directory itself if none
contains one). The result does not depend on where `ki18n` itself is located.
The application installation resources are then read from
`<project_root>/i18n/`.
### Read and change the language
@@ -181,24 +187,14 @@ no_count_text = i18n.p("files.found", None)
| Count | Category |
| --- | --- |
| `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` |
| `0` | `zero` |
| `1` | `one` |
| Greater than `1` | `many` |
| `None` | `none` |
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"
```
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.
### Listen for language changes
@@ -249,7 +245,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
or `many`.
`many`, or a negative plural count.
- `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.