API#
- class quart_libretranslate.LibreTranslate(app: Quart | None = None, url: str | None = None, api_key: str | None = None)#
This is an extension to use LibreTranslate with Quart.
It is basically a wrapper around the LibreTranslate API.
- Parameters:
app – The Quart application.
url – The url to LibreTranslate with the port.
api_key – The api key for LibreTranslate.
- init_app(app: Quart, url: str | None = None, api_key: str | None = None) None#
Register the extension with the Quart application.
- Parameters:
app – The Quart application.
- async detect(q: str) List[Dict[str, Any]]#
Detects the language of a single string.
Must be called within app context.
- Argument:
q: The string to detect the language on.
- Returns:
[{“confidence”: 0.6, “language”: “en”}]
- Return type:
The detected languages ex
- Raises:
ApiError –
- property languages: List[Dict[str, str]]#
Retrieve a list of supported languages.
Must be called within app context.
- Returns:
[{“code”:”en”, “name”:”English”}]
- Return type:
A list of available languages ex
- Raises:
ApiError –
- async translate(q: str, source: str = 'en', target: str = 'es') Dict[str, Any]#
Translate a string.
Must be called within app context.
- Parameters:
q – The text to translate.
source – The source language code (ISO 639).
target – The target language code (ISO 639).
- Returns:
- A dict with alternatives and translated text. The
translated text will be at the key “translatedText”.
- Raises:
ApiError –
- async quart_libretranslate.detect(q: str) List[Dict[str, Any]]#
Detects the language of a single string.
Must be called within app context.
- Argument:
q: The string to detect the language on.
- Returns:
[{“confidence”: 0.6, “language”: “en”}]
- Return type:
The detected languages ex
- Raises:
ApiError –
- async quart_libretranslate.languages() List[Dict[str, str]]#
Retrieve a list of supported languages.
Must be called within app context.
- Returns:
[{“code”:”en”, “name”:”English”}]
- Return type:
A list of available languages ex
- Raises:
ApiError –
- async quart_libretranslate.translate(q: str, source: str = 'en', target: str = 'es') Dict[str, Any]#
Translate a string.
Must be called within app context.
- Parameters:
q – The text to translate.
source – The source language code (ISO 639).
target – The target language code (ISO 639).
- Returns:
The translated text
- Return type:
str
- Raises:
ApiError –
- class quart_libretranslate.ApiError(message: str, code: int)#
Custom exception for when receiving errors from LibreTranslate API.
- Parameters:
message – The error message.
code – The error code.