Skip to content

Translation

other_lang_to_odia

other_lang_to_odia(
    text: str, source_language_code: str = "en"
) -> str

Translate from English to Odia language

odia_to_other_lang

odia_to_other_lang(
    text: str, dest_language_code: str = "en"
) -> str

Translate from Odia to other language

universal_translation

universal_translation(
    text: str,
    source_language_code: str = "en",
    dest_language_code: str = "or",
) -> str

Translate from any language to any By default it works for English to Odia. Based on the source and destination language provided, it can work for any languages supported.

odia_to_other_lang_async async

odia_to_other_lang_async(
    text: str, dest_language_code: str = "en"
) -> str

Async form of odia_to_other_lang. Runs in a worker thread.

other_lang_to_odia_async async

other_lang_to_odia_async(
    text: str, source_language_code: str = "en"
) -> str

Async form of other_lang_to_odia. Runs in a worker thread.

universal_translation_async async

universal_translation_async(
    text: str,
    source_language_code: str = "en",
    dest_language_code: str = "or",
) -> str

Async form of universal_translation. Runs in a worker thread.

translate_batch async

translate_batch(
    texts: Iterable[str],
    *,
    source: str = ...,
    to: str = ...,
    concurrency: int = ...,
    retries: int = ...,
    backoff: float = ...,
    return_exceptions: Literal[False] = ...,
) -> list[str]
translate_batch(
    texts: Iterable[str],
    *,
    source: str = ...,
    to: str = ...,
    concurrency: int = ...,
    retries: int = ...,
    backoff: float = ...,
    return_exceptions: Literal[True],
) -> list[str | BaseException]
translate_batch(
    texts: Iterable[str],
    *,
    source: str = "or",
    to: str = "en",
    concurrency: int = 8,
    retries: int = 0,
    backoff: float = 0.5,
    return_exceptions: bool = False,
) -> list[str] | list[str | BaseException]

Translate texts concurrently and return the results in input order.

texts can be any iterable of strings, including a generator; it is read once, up front.

Each distinct text is translated once, through the same path and cache as the sync functions: source="or" and to="or" behave like odia_to_other_lang and other_lang_to_odia, so English to Odia uses the offline dictionary first. Any other pair behaves like universal_translation.

At most concurrency requests run at once, on a thread pool private to this call. A request is retried up to retries times, sleeping backoff * 2**attempt seconds between attempts, only when it fails with a rate limit (HTTP 429), an HTTP 5xx status, or a connection or timeout error. Anything else is raised at once: other statuses, OSError subclasses such as PermissionError, and errors such as an unsupported language code. deep-translator reports a failed HTTP response as a RequestError that carries no status, so that error is not retried; only exceptions that expose status_code (or response.status_code) can count as 5xx.

Texts of 5000 characters or more raise NotValidLength, which aborts the whole batch unless return_exceptions=True. Identical texts are translated once per call, not across calls; the shared cache covers repeats across calls.

By default the first failure is raised, texts not yet started are skipped, and requests already running finish in the background. With return_exceptions=True every text gets an entry, either its translation or the exception it failed with. Cancelling the call stops queued texts the same way. A translation is cached only once it has succeeded, so neither case leaves a partial entry.

Raises:

Type Description
TypeError

If texts is a single str, bytes, bytearray or memoryview, is not iterable, or has an item that is not a str.

ValueError

If concurrency is not an integer >= 1, retries is not an integer >= 0, or backoff is not a finite number >= 0.

cache

Translation cache configuration and observability.

The package keeps an in-memory LRU of translation results. By default it holds 10,000 entries and is hidden from callers.

Use the helpers in this module to:

  • enlarge / shrink the LRU,
  • enable a persistent disk cache (requires the [cache] extra),
  • observe hit/miss counts,
  • clear the cache.

Configuration may also be set via environment variables at import time:

  • OPENODIA_CACHE_MAX_SIZE — integer LRU size (default 10000).
  • OPENODIA_CACHE_DISK — path to a directory used for disk persistence.
Example

from openodia import cache cache.configure(max_size=50_000) cache.stats()

CacheStats

Bases: TypedDict

Snapshot of cache counters.

TranslationCache

LRU cache for translation results, with optional disk persistence.

Keys are arbitrary hashable tuples; the package uses (text, source_lang, dest_lang) triples. Values are translated strings.

Parameters:

Name Type Description Default
max_size int

Maximum number of entries to keep in memory. Must be ≥ 1.

DEFAULT_MAX_SIZE
disk_path Path | str | None

Optional directory for persistent storage. Requires diskcache (install with pip install openodia[cache]).

None

Pickled and copied caches (copy.copy included) get their own snapshot of the memory entries, taken under the lock, and their own locks.

max_size property

max_size: int

Configured LRU capacity.

disk_enabled property

disk_enabled: bool

Whether disk persistence is currently active.

get

get(key: Any) -> str | None

Return a cached value or None if missing.

Hits in memory are moved to the most-recently-used position. Hits on disk are promoted to memory. The disk is read outside the lock. While a set or clear is running, the lookup waits for it instead of reading the disk; a value read while one started is discarded and looked up again. The wait is on the lock those calls hold, so one that is interrupted, by KeyboardInterrupt too, releases the waiting lookups.

set

set(key: Any, value: str) -> None

Insert into memory (and disk, if configured).

If the disk write raises, the error propagates, the memory entry stays, and the cache remains usable, whatever the exception.

stats

stats() -> CacheStats

Return current hit/miss/size counters.

clear

clear() -> None

Drop all entries (memory + disk) and reset counters.

If clearing the disk raises, the error propagates, memory stays empty, and the cache remains usable, whatever the exception.

clear

clear() -> None

Drop all entries and reset counters in the active cache.

configure

configure(
    max_size: int | None = None,
    disk_path: Path | str | None = None,
) -> TranslationCache

Replace the active cache with a new configuration.

Parameters:

Name Type Description Default
max_size int | None

New LRU capacity. None keeps the current value.

None
disk_path Path | str | None

Path for disk persistence; None disables disk persistence. To keep the existing disk path, pass it again.

None

Returns:

Type Description
TranslationCache

The newly-installed cache instance.

get_cache

get_cache() -> TranslationCache

Return the active cache instance, building it lazily on first use.

Safe to call from several threads at once: they all get the same instance.

stats

stats() -> CacheStats

Snapshot of the active cache's counters.