Skip to content

translation_cache

Persistent SQLite caching for English translations.

CachedTranslator

CachedTranslator(translator: Translator, path: StrPath, *, timeout: float = 60)

Cache translations by full identity, source language, and input text.

Create one instance per worker; instances may share a database file. Use as a context manager to close the cache automatically.

Parameters:

Name Type Description Default
translator Translator

Translation backend with a nonempty engine identifier.

required
path StrPath

SQLite database path, or :memory: for an in-memory cache.

required
timeout float

SQLite lock timeout in seconds.

60

Raises:

Type Description
ValueError

If the engine identifier or timeout is invalid.

Error

If the cache cannot be opened or initialized.

Source code in src/gdelt_regkg/translation_cache.py
def __init__(self, translator: Translator, path: StrPath, *, timeout: float = 60):
    self.translator = translator
    """Wrapped translation backend."""
    self.engine = translator.engine
    """Public citation used by translated metadata."""
    if not isinstance(self.engine, str) or not self.engine.strip():
        raise ValueError("Translator must identify its engine")
    identity = getattr(translator, "cache_identity", None)
    self.cache_identity = self.engine if identity is None else identity
    if not isinstance(self.cache_identity, str) or not self.cache_identity.strip():
        raise ValueError("cache_identity must be a nonempty string")
    self._connection: sqlite3.Connection | None = open_database(path, timeout=timeout)
    try:
        self.connection.execute(
            "CREATE TABLE IF NOT EXISTS translations (key TEXT PRIMARY KEY, body TEXT NOT NULL)"
        )
        self.connection.commit()
    except BaseException:
        self.close()
        raise

translator instance-attribute

translator = translator

Wrapped translation backend.

engine instance-attribute

engine = translator.engine

Public citation used by translated metadata.

translation_profile property

translation_profile: dict[str, Any] | None

Full provenance, when exposed by the wrapped backend.

connection property

connection: Connection

Return the cache's open SQLite connection.

Raises:

Type Description
RuntimeError

If the cache is closed.

close

close() -> None

Roll back pending work and close the cache connection.

Repeated calls are safe. The wrapped translator remains available.

Source code in src/gdelt_regkg/translation_cache.py
def close(self) -> None:
    """Roll back pending work and close the cache connection.

    Repeated calls are safe. The wrapped translator remains available.
    """
    connection, self._connection = self._connection, None
    close_database(connection)

translate

translate(text: str, source_language: str) -> str

Translate one body into English, reusing cached results.

Parameters:

Name Type Description Default
text str

Source body to translate.

required
source_language str

Supported language code, name, or regional tag.

required

Returns:

Type Description
str

The English translation. English input is returned unchanged.

Raises:

Type Description
TypeError

If text is not a string.

ValueError

If the language is invalid, a non-English body is blank, or the backend returns invalid output.

RuntimeError

If the cache is closed.

Error

If reading or saving cached results fails.

Source code in src/gdelt_regkg/translation_cache.py
def translate(self, text: str, source_language: str) -> str:
    """Translate one body into English, reusing cached results.

    Args:
        text: Source body to translate.
        source_language: Supported language code, name, or regional tag.

    Returns:
        The English translation. English input is returned unchanged.

    Raises:
        TypeError: If `text` is not a string.
        ValueError: If the language is invalid, a non-English body is blank,
            or the backend returns invalid output.
        RuntimeError: If the cache is closed.
        sqlite3.Error: If reading or saving cached results fails.
    """
    return self.translate_many([text], source_language)[0]

translate_many

translate_many(texts: Iterable[str], source_language: str) -> list[str]

Translate bodies into English, batching unique cache misses.

Uses the backend's batch method when available. New results are committed after validation.

Parameters:

Name Type Description Default
texts Iterable[str]

Source bodies to translate.

required
source_language str

Supported language code, name, or regional tag.

required

Returns:

Type Description
list[str]

Translations in input order, preserving duplicates. English input

list[str]

is returned unchanged.

Raises:

Type Description
TypeError

If texts is not iterable or contains non-string values.

ValueError

If the language is invalid, a non-English body is blank, or the backend returns invalid output.

RuntimeError

If the cache is closed.

Error

If reading or saving cached results fails.

Source code in src/gdelt_regkg/translation_cache.py
def translate_many(self, texts: Iterable[str], source_language: str) -> list[str]:
    """Translate bodies into English, batching unique cache misses.

    Uses the backend's batch method when available. New results are committed
    after validation.

    Args:
        texts: Source bodies to translate.
        source_language: Supported language code, name, or regional tag.

    Returns:
        Translations in input order, preserving duplicates. English input
        is returned unchanged.

    Raises:
        TypeError: If `texts` is not iterable or contains non-string values.
        ValueError: If the language is invalid, a non-English body is blank,
            or the backend returns invalid output.
        RuntimeError: If the cache is closed.
        sqlite3.Error: If reading or saving cached results fails.
    """
    source = normalize_language(source_language)
    texts = list(texts)
    if any(not isinstance(text, str) for text in texts):
        raise TypeError("texts must contain strings")
    connection = self.connection
    if source == "eng":
        return texts
    if any(not text.strip() for text in texts):
        raise ValueError("Cannot translate a blank body")
    keys = [self._key(text, source) for text in texts]
    outputs = {}
    missing: dict[str, str] = {}
    for key, text in zip(keys, texts, strict=True):
        cached = connection.execute(
            "SELECT body FROM translations WHERE key = ?", (key,)
        ).fetchone()
        if cached is not None:
            outputs[key] = cached[0]
        else:
            missing.setdefault(key, text)
    if missing:
        bodies = list(missing.values())
        method = getattr(self.translator, "translate_many", None)
        translated = (
            method(bodies, source)
            if callable(method)
            else [self.translator.translate(text, source) for text in bodies]
        )
        if isinstance(translated, (str, bytes)):
            raise ValueError("Translator returned an invalid batch")
        try:
            translated = list(translated)
        except TypeError as error:
            raise ValueError("Translator returned an invalid batch") from error
        if len(translated) != len(bodies) or any(
            not isinstance(text, str) or not text.strip() for text in translated
        ):
            raise ValueError("Translator returned an invalid batch")
        entries = list(zip(missing, translated, strict=True))
        try:
            connection.executemany("INSERT OR REPLACE INTO translations VALUES (?, ?)", entries)
            connection.commit()
        except BaseException:
            connection.rollback()
            raise
        outputs.update(entries)
    return [outputs[key] for key in keys]