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
+7 -40
View File
@@ -61,7 +61,7 @@ my-app/
from ki18n import Ki18n, i18n
# Optional: configure the manager before the first translation is requested.
Ki18n("en") # default language; project_root is derived from sys.argv[0]
Ki18n("en") # default language; project_root is bundle_root()
print(i18n.t("greeting", name="Ada")) # Hello, Ada!
print(i18n.p("files.found", 3)) # 3 files found
@@ -227,8 +227,8 @@ Creates (or returns) the singleton manager and loads the default language.
| Parameter | Description |
| --- | --- |
| `default_language` | Language that is active after construction. Also the language that is listed first and that must be shipped with the application. Normalized (`"de-at"` → `"de_AT"`). |
| `project_root` | Application directory that contains the installed `i18n/` folder. `~` is expanded. If omitted, the directory of `sys.argv[0]` is used (relative paths are resolved against the current working directory). |
| `local_root` | The application's per-user directory; user resources are read from its `i18n/` subfolder. `~` is expanded. If omitted: `<user data dir>/<name of project_root>` (see below). |
| `project_root` | Application directory that contains the installed `i18n/` folder. `~` is expanded. If omitted, [`ki18n.paths.bundle_root()`](#7-ki18npaths-module-reference) is used: the application root, independent of where ki18n is located. |
| `local_root` | The application's per-user directory; user resources are read from its `i18n/` subfolder. `~` is expanded. If omitted: `<user data dir>/<application name>` (see below). The application name is the name of `project_root`; in a frozen one-file build it is the name of the executable (without extension), because the extraction directory changes on every start. |
Default user data directory (used when `local_root` is omitted):
@@ -558,43 +558,8 @@ Notes:
started from `root/app/` gets `root/app`.
- For installed console scripts (`sys.argv[0]` points into a virtual
environment's `bin/` directory) pass `project_root` explicitly instead.
- `Ki18n` itself uses the directory of `sys.argv[0]` as default `project_root`
without walking up. To use the application root also for deeper entry points,
pass it explicitly:
```python
from ki18n import Ki18n
from ki18n.paths import bundle_root
Ki18n("en", project_root=bundle_root())
```
#### `_base_data_dir() -> Path` (internal)
Conventional per-user base directory for application data. The directory is
neither created nor checked for existence.
| Platform | Result |
| --- | --- |
| Windows | `%LOCALAPPDATA%`, else `~/AppData/Local` |
| macOS (Darwin) | `$XDG_DATA_HOME` if set, else `~/Library/Application Support` |
| Linux and other POSIX | `$XDG_DATA_HOME` if set, else `~/.local/share` |
An empty environment variable counts as unset; a leading `~` in `XDG_DATA_HOME`
is expanded.
#### `data_dir(name: str) -> Path`
Data directory of the application called `name`: `base_data_dir() / name`, for
example `data_dir("ki18n")` is `~/.local/share/ki18n` on Linux. The directory is
not created.
Raises `ValueError` if `name` is empty, `.` or `..`, or contains `/` or `\`.
```python
from ki18n.paths import data_dir
Ki18n("en", local_root=data_dir("my-app"))
```
- `Ki18n` uses `bundle_root()` as the default `project_root`, so the two always
agree. Pass `project_root` explicitly to override it.
---
@@ -639,3 +604,5 @@ These points are based on reading the current source code.
selecting the already active language does nothing.
- **Plain Markdown help.** `help.md` is returned verbatim; there is no merge with
the installed file and no translation of individual sections.
- **Documentation drift in the source.** The module and constructor docstrings
mention a `key_aliases` mapping, which the code does not implement.