new method data_dir(name: str) in paths.py, updated related documents

This commit is contained in:
2026-11-20 23:35:48 +01:00
parent 71a162acc9
commit 86f3b8a769
4 changed files with 205 additions and 31 deletions
+30 -2
View File
@@ -514,8 +514,9 @@ register a language-change listener.
## 7. `ki18n.paths` module reference
Small helpers for applications that are bundled with PyInstaller. They are
independent of `Ki18n`.
Small helpers for applications that are bundled with PyInstaller and for locating
per-user data directories. They are independent of `Ki18n` (which uses
`base_data_dir()` for its default user directory).
#### `is_frozen() -> bool`
@@ -539,6 +540,33 @@ from ki18n.paths import bundle_root
Ki18n("en", project_root=bundle_root())
```
#### `base_data_dir() -> Path`
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"))
```
---
## 8. Errors and logging