Internationalization Module¶
i18n
¶
Internationalization (i18n) support for Weeb CLI.
This module provides multi-language support through JSON-based translation files. Supports Turkish (tr), English (en), German (de), and Polish (pl).
The translation system uses dot-notation for nested keys and supports string interpolation with keyword arguments.
Example
Basic usage::
from weeb_cli.i18n import i18n
# Get translated string
message = i18n.t("menu.search", "Search Anime")
# With interpolation
greeting = i18n.t("welcome.user", name="John")
# Change language
i18n.set_language("tr")
Attributes:
| Name | Type | Description |
|---|---|---|
LOCALES_DIR |
Path
|
Directory containing translation JSON files. |
i18n |
I18n
|
Global i18n instance for application-wide use. |
I18n
¶
Internationalization manager for multi-language support.
Loads and manages translation strings from JSON files, providing a simple interface for retrieving localized text with support for nested keys and string interpolation.
Attributes:
| Name | Type | Description |
|---|---|---|
language |
str
|
Current language code (e.g., 'en', 'tr', 'de', 'pl'). |
translations |
Dict[str, Any]
|
Loaded translation dictionary. |
Source code in weeb_cli/i18n.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | |
__init__
¶
Initialize i18n with language from config or default to English.
Source code in weeb_cli/i18n.py
set_language
¶
Set the active language and reload translations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
language_code
|
str
|
Language code (e.g., 'en', 'tr', 'de', 'pl'). |
required |
Source code in weeb_cli/i18n.py
load_translations
¶
Load translation file for the current language.
Falls back to English if the requested language file doesn't exist. Silently handles file read errors by using an empty dictionary.
Source code in weeb_cli/i18n.py
get
¶
Get translated string by dot-notation key path.
Supports nested keys using dot notation (e.g., 'menu.search.title') and string interpolation using keyword arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key_path
|
str
|
Dot-separated path to translation key (e.g., 'menu.search'). |
required |
default
|
Optional[str]
|
Default value if key not found. If None, returns key_path. |
None
|
**kwargs
|
Any
|
Variables for string interpolation using .format(). |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
Translated and interpolated string, or default/key_path if not found. |
Example
i18n.get("welcome.message", name="John") "Welcome, John!" i18n.get("missing.key", "Default Text") "Default Text"
Source code in weeb_cli/i18n.py
get_locales_dir
¶
Get the locales directory path.
Handles both development and frozen (PyInstaller) environments.
Returns:
| Type | Description |
|---|---|
Path
|
Path to the locales directory containing translation files. |
Source code in weeb_cli/i18n.py
Overview¶
The i18n module provides multi-language support through JSON-based translation files. Supports Turkish (tr), English (en), German (de), and Polish (pl).
Usage Examples¶
Basic Translation¶
from weeb_cli.i18n import i18n
# Get translated string
message = i18n.t("menu.search", "Search Anime")
error = i18n.t("errors.network", "Network error")
String Interpolation¶
from weeb_cli.i18n import i18n
# With named parameters
greeting = i18n.t("welcome.user", name="John")
# Result: "Welcome, John!"
progress = i18n.t("download.progress",
current=5,
total=12,
percent=42)
# Result: "Downloading 5/12 (42%)"
Changing Language¶
from weeb_cli.i18n import i18n
# Change to Turkish
i18n.set_language("tr")
# Change to German
i18n.set_language("de")
Nested Keys¶
Translation files use nested JSON structure:
{
"menu": {
"search": "Search Anime",
"downloads": "Downloads",
"settings": "Settings"
},
"errors": {
"network": "Network connection failed",
"not_found": "Anime not found"
}
}
Access with dot notation:
Supported Languages¶
| Code | Language | Status |
|---|---|---|
en |
English | ✅ Complete |
tr |
Turkish | ✅ Complete |
de |
German | ✅ Complete |
pl |
Polish | ✅ Complete |
Translation Files¶
Translation files are located in weeb_cli/locales/:
Adding Translations¶
To add a new language:
- Create
weeb_cli/locales/<lang_code>.json - Copy structure from
en.json - Translate all strings
- Test with
i18n.set_language("<lang_code>")
Fallback Behavior¶
- If a key is not found, returns the default value or key path
- If a translation file doesn't exist, falls back to English
- If English file is missing, returns empty translations
API Reference¶
Internationalization manager for multi-language support.
Loads and manages translation strings from JSON files, providing a simple interface for retrieving localized text with support for nested keys and string interpolation.
Attributes:
| Name | Type | Description |
|---|---|---|
language |
str
|
Current language code (e.g., 'en', 'tr', 'de', 'pl'). |
translations |
Dict[str, Any]
|
Loaded translation dictionary. |
Source code in weeb_cli/i18n.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | |
get
¶
Get translated string by dot-notation key path.
Supports nested keys using dot notation (e.g., 'menu.search.title') and string interpolation using keyword arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key_path
|
str
|
Dot-separated path to translation key (e.g., 'menu.search'). |
required |
default
|
Optional[str]
|
Default value if key not found. If None, returns key_path. |
None
|
**kwargs
|
Any
|
Variables for string interpolation using .format(). |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
Translated and interpolated string, or default/key_path if not found. |
Example
i18n.get("welcome.message", name="John") "Welcome, John!" i18n.get("missing.key", "Default Text") "Default Text"
Source code in weeb_cli/i18n.py
set_language
¶
Set the active language and reload translations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
language_code
|
str
|
Language code (e.g., 'en', 'tr', 'de', 'pl'). |
required |
Source code in weeb_cli/i18n.py
load_translations
¶
Load translation file for the current language.
Falls back to English if the requested language file doesn't exist. Silently handles file read errors by using an empty dictionary.