API Reference¶
The public API is experimental, and it SHOULD NOT be considered stable. wittrans is primarily a CLI tool. This API reference is for version 0.8.0 of wittrans.
Import¶
Import wittrans into your project:
Functions¶
wittrans.api.search(term, language_code, *, include_flatpak=True, source_only=False, translation_only=False, whole_word=False, on_progress=None)
¶
Search for a term across all installed .mo translation files for a given language.
Searches both source (usually English) text and translated strings by default. Results are organized by source category (system locale, Flatpak, GNOME extensions, KDE Plasmoids, etc.) and collected concurrently.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
term
|
str
|
Text to search for. Search is case-insensitive. |
required |
language_code
|
str
|
Language to search in. Accepts ISO 639-1 (e.g. |
required |
include_flatpak
|
bool
|
Search Flatpak application and runtime translations. Disable for faster searches if Flatpak results are not needed. |
True
|
source_only
|
bool
|
Search only in source strings.
Cannot be used with |
False
|
translation_only
|
bool
|
Search only in translation strings.
Cannot be used with |
False
|
whole_word
|
bool
|
Match whole words only instead of partial matching. |
False
|
on_progress
|
Callable[[str], None] | None
|
Optional callback invoked once per category before it is
searched. Receives a human-readable status string, e.g.
|
None
|
Returns:
| Type | Description |
|---|---|
SearchResult
|
A SearchResult containing all matches. |
Raises:
| Type | Description |
|---|---|
InvalidLanguageCode
|
If the language code is not a valid two- or three-letter ISO 639 code or locale string. |
TranslationDirectoryNotFound
|
If no translation directories are found for the given language. |
Source code in src/wittrans/api.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 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 | |
wittrans.api.get_translations(language_code, *, include_flatpak=True, on_progress=None)
¶
Retrieve all translation strings from installed .mo files for a given language.
Matches every entry in every translation file found for the language. Use this to retrieve all translations. The result can be very large depending on the number of installed .mo files and their content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
language_code
|
str
|
Language to retrieve translations for. Accepts ISO 639-1
(e.g. |
required |
include_flatpak
|
bool
|
Include Flatpak application and runtime translations. Disable for faster retrieval if Flatpak results are not needed. |
True
|
on_progress
|
Callable[[str], None] | None
|
Optional callback invoked once per category before it is
searched. Receives a human-readable status string, e.g.
|
None
|
Returns:
| Type | Description |
|---|---|
SearchResult
|
A SearchResult containing all translations. |
Raises:
| Type | Description |
|---|---|
InvalidLanguageCode
|
If the language code is not a valid two- or three-letter ISO 639 code or locale string. |
TranslationDirectoryNotFound
|
If no translation directories are found for the given language. |
Source code in src/wittrans/api.py
Examples¶
Search and print results¶
from wittrans import search
# Search only the whole word as is, no conjugated words, e.g. maanantaina
result = search("maanantai", "fi", whole_word=True)
for category in result.categories:
for file_result in category.results:
for match in file_result.matches:
print(f"{match.original} → {match.translation}")
Show search statistics¶
from wittrans import search
result = search("maanantai", "fi")
stats = result.statistics
print(f"Matches: {stats.total_matches}")
print(f".mo files: {stats.mo_files_found}")
print(f"Directories: {stats.directories_searched}")
print(f"Duration: {stats.search_duration_seconds:.2f}s")