Added handling of system locale
This commit is contained in:
@@ -156,6 +156,47 @@ languages mapping. Unknown languages raise `ValueError`. A language change
|
||||
loads and validates its translation file before changing state. Selecting the
|
||||
active language is a no-op.
|
||||
|
||||
### Detect the system locale
|
||||
|
||||
```python
|
||||
from ki18n import Ki18n
|
||||
|
||||
locale = Ki18n.get_locale() # no manager needed; also i18n.get_locale()
|
||||
if locale is not None:
|
||||
print(locale.id) # "de_DE"
|
||||
print(locale.language, locale.region, locale.encoding) # de DE UTF-8
|
||||
try:
|
||||
i18n.set_language(locale.id) # "de_DE" selects "de" if only "de" exists
|
||||
except ValueError:
|
||||
pass # language not available: keep the default
|
||||
```
|
||||
|
||||
`get_locale()` describes the locale currently set in the operating system and
|
||||
works on Linux, macOS (Darwin) and Windows. It returns a frozen `SystemLocale`
|
||||
or `None` when no locale can be determined or the system uses the `C`/`POSIX`
|
||||
locale. The result is not cached and does not change the active language.
|
||||
|
||||
| Field | Meaning |
|
||||
| --- | --- |
|
||||
| `raw` | Value as reported by the system, e.g. `de_DE.UTF-8` (POSIX), `de_DE` (macOS), `de-DE` (Windows) |
|
||||
| `id` | Normalized language ID (`de_DE`), accepted by `set_language()` |
|
||||
| `language`, `script`, `region` | Parts of `id` (`de`, `None`, `DE`); `script` and `region` may be `None` |
|
||||
| `encoding` | Character encoding from `raw` (`UTF-8`) or `None` |
|
||||
| `source` | Where the value came from (see below) |
|
||||
|
||||
Sources, in order:
|
||||
|
||||
- **Linux and other POSIX systems**: the first non-empty of `LC_ALL`,
|
||||
`LC_MESSAGES`, `LANG`. If it names a real locale, the `LANGUAGE` priority list
|
||||
(colon separated, as used by `gettext`) takes precedence (`source` is
|
||||
`LANGUAGE`).
|
||||
- **macOS**: the same environment variables first (they are set by Terminal);
|
||||
if they name no usable locale, e.g. in GUI applications, the system settings
|
||||
`AppleLocale` and then `AppleLanguages` (read with `defaults read -g`).
|
||||
- **Windows**: `GetUserDefaultLocaleName` (user locale), then
|
||||
`GetUserDefaultUILanguage` (user interface language). Environment variables
|
||||
are ignored.
|
||||
|
||||
### Translate strings
|
||||
|
||||
```python
|
||||
|
||||
Reference in New Issue
Block a user