Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

pyxpo-translate

CI License: MIT

Offline text translation for pyxpo apps, written in Python. It runs Google ML Kit's on-device translation models on Android: the app downloads a language model once (about 30 MB) when the user agrees, and from then on every translation runs on the phone, with no network, account or API key. About 60 languages, including Hindi, Spanish, Chinese, Arabic and French.

A pyxpo demo on Android downloading the Hindi model, then translating a Hindi sentence to English offline

import pyxpo_translate as Translate

if not await Translate.is_ready_async("hi", "en"):
    await Translate.download_async("hi")                  # once, after the user agrees
english = await Translate.translate_async("मैं कल आऊँगा", "hi", "en")   # "I will come tomorrow"

Features

  • Offline after one download:
    • download_async(language) fetches one language's model from Google, once.
    • translate_async then runs on the phone. It never fetches a model by itself: a missing language raises an error at once instead of waiting on the network.
    • English is built in and never downloaded.
  • Private: the text you translate is never sent anywhere and never logged. Only model files come from the network, and only when you call download_async.
  • The app decides when to download: is_ready_async, downloaded_async and status_async tell you what is on the phone, so you can ask the user first, and wifi_only=True waits for Wi-Fi instead of using mobile data.
  • Fast after the first call: the first translation of a language pair loads its model (about a second); the module keeps it open for the next call. close_async() frees the memory.
  • Works from a keyboard: the module needs no screen, so an input method built with pyxpo-keyboard can call it (the recipe shows one).
  • Checked arguments: language codes and text are checked in Python before any native work starts.

Contents

If you are... Start here
New to the package Quick start → Languages
Getting models onto the phone Downloading models
Translating text Translating
Seeing it in a real app Recipe: a voice keyboard that types English
Shipping to the stores Production checklist
Looking for exact signatures API reference · Errors
Stuck Testing · Troubleshooting · FAQ

Platform support

Target Translation
Android, API 24+ Yes. Google ML Kit on-device translation (com.google.mlkit:translate). Models download on request.
iOS Not yet. status_async() reports supported: False and the other calls raise NotSupportedError.
Web page and desktop window (pyxpo run web / pyxpo run desktop), Mac Catalyst, Windows None, same as iOS.

Requires Python 3.9+ and pyxpo>=0.1.0a6.

Install

pip install pyxpo-translate

Add the module to your app's pyproject.toml:

[tool.pyxpo]
modules = ["translate"]

The package has no config plugin and no options. Its Android manifest adds the INTERNET and ACCESS_NETWORK_STATE permissions, which download_async needs to fetch models. If your app said it uses no network, update that text: the app now downloads model files when asked (the text it translates still stays on the phone).

The module has native code, so rebuild the app after adding it (pyxpo run android). The first build downloads ML Kit through Gradle.

Quick start

A screen that downloads the Hindi model when the user taps, then translates what they type:

import pyxpo_translate as Translate
from pyxpo import Pressable, Text, TextInput, View, component, use_async, use_state


@component
def HindiToEnglish():
    async def check():
        return await Translate.is_ready_async("hi", "en")

    ready = use_async(check, [])
    downloaded, set_downloaded = use_state(False)
    text, set_text = use_state("")
    english, set_english = use_state("")
    busy, set_busy = use_state("")

    async def download():
        set_busy("Downloading the Hindi model (about 30 MB)...")
        try:
            await Translate.download_async("hi")
            set_downloaded(True)
            set_busy("")
        except RuntimeError as e:
            set_busy("Download failed: %s" % e)

    async def translate():
        set_english(await Translate.translate_async(text, "hi", "en"))

    if ready.loading:
        return Text("Getting ready...")
    if ready.error is not None:
        return Text("Translation needs the Android app.")
    if not (ready.value or downloaded):
        return View(
            Text(busy or "Translate Hindi offline: download the Hindi model once."),
            Pressable(Text("Download Hindi"), on_press=download),
            style={"flex": 1, "alignItems": "center", "justifyContent": "center", "gap": 16},
        )
    return View(
        TextInput(value=text, on_change_text=set_text, placeholder="हिंदी में लिखें"),
        Pressable(Text("Translate"), on_press=translate),
        Text(english),
        style={"flex": 1, "padding": 24, "gap": 16},
    )

Languages

Languages are lowercase codes, the ones ML Kit uses: "hi" (Hindi), "en" (English), "es", "fr", "de", "zh", "ar", "bn", "mr", "ta", "te", "gu", "kn", "ur" and about 45 more.

codes = await Translate.languages_async()     # ["af", "ar", "be", "bg", "bn", ..., "zh"]

Any pair works, for example "es" to "hi"; ML Kit translates through English inside the phone. A code ML Kit does not know raises RuntimeError("ML Kit cannot translate 'xx'"), and a value that is not a 2 or 3 letter lowercase code raises ValueError before anything native runs.

Downloading models

status = await Translate.status_async()            # {"supported": True, "downloaded": ["en", "hi"]}
have = await Translate.downloaded_async()          # ["en", "hi"]
ok = await Translate.is_ready_async("hi", "en")    # True when both are on the phone

await Translate.download_async("hi")                   # returns when the model is on the phone
await Translate.download_async("hi", wifi_only=True)   # waits for Wi-Fi first
await Translate.delete_async("hi")                     # frees the space
Argument Default Description
language (required) The language code to download, for example "hi". "en" returns at once.
wifi_only False True waits until the phone is on Wi-Fi instead of using mobile data. The call does not return until then.
  • A model is about 30 MB. Ask the user before downloading, and say the size.
  • download_async returns only when the model is on the phone, or raises RuntimeError when the download failed (no network, no space). There is no progress callback: show a spinner.
  • A model stays on the phone until delete_async or until the app is uninstalled. It is shared by every translator in the app.
  • delete_async("en") raises RuntimeError: English is built in.

Translating

english = await Translate.translate_async("आज मौसम बहुत अच्छा है", "hi")        # target defaults to "en"
hindi = await Translate.translate_async("The weather is lovely today", "en", "hi")
Argument Default Description
text (required) The text to translate, up to MAX_TEXT (10,000) characters. Several sentences are fine.
source (required) The language of text.
target "en" The language to translate into.
  • When source == target, or the text is blank, the text comes back unchanged and nothing native runs.
  • When a language is not on the phone, translate_async raises RuntimeError("not on the phone: hi (download_async first)") at once. Check is_ready_async first.
  • The first call for a pair takes about a second while the model loads; later calls take tens of milliseconds for a sentence. Call close_async() when you are done to free the memory (the models stay downloaded).
  • ML Kit translates the text it is given and does not know which language it is. For mixed text such as Hinglish written in Latin letters, pass the language most of it is in, or clean it up first.

Errors

Error When What to do
ValueError A language is not a lowercase code, text is not a string, or it is longer than MAX_TEXT. Fix the argument. Split long text into chunks.
RuntimeError: not on the phone: hi (download_async first) translate_async with a language that is not downloaded. Check is_ready_async, ask the user, call download_async.
RuntimeError: the hi model did not download: ... No network, no space, or Google's server could not be reached. Show the message and offer to try again later, on Wi-Fi.
RuntimeError: ML Kit cannot translate 'xx' ML Kit does not know the code. Use a code from languages_async().
RuntimeError: ML Kit did not answer in time A call took longer than its limit (60 seconds for a translation). Try a shorter text; restart the app if it repeats.
NotSupportedError iOS, the web or the desktop. Check status_async()["supported"] and offer something else there.
ModuleNotLinkedError The build does not include the module. Add "translate" to [tool.pyxpo] modules and rebuild.

Web and desktop

There is no translator on iOS, the web or the desktop yet. status_async() returns {"supported": False, "downloaded": []}, close_async() does nothing, and every other call raises NotSupportedError, so the same app code runs everywhere and can hide the feature where it is missing.

Recipe: a voice keyboard that types English

The AI Keyboard example app listens to Hindi with pyxpo-speech, translates it on the phone and types the English into whatever app is open. This is the translation step, called with each finished transcript:

import pyxpo_translate as Translate


async def to_english(text: str, language: str) -> str:
    """English for what the user said. Asks for the model download instead of reaching the network silently."""
    if language == "en":
        return text
    if not await Translate.is_ready_async(language, "en"):
        raise LookupError(language)        # the keyboard shows "Download Hindi (30 MB)" and waits for a tap
    return await Translate.translate_async(text, language, "en")


async def on_download_tap(language: str) -> None:
    await Translate.download_async(language, wifi_only=True)

The keyboard has no screen of its own, so it shows the download offer in its panel and calls download_async only after the tap.

Production checklist

  • Ask before downloading, say the size (about 30 MB per language), and prefer wifi_only=True.
  • Check is_ready_async before translating, and handle RuntimeError from both download and translate.
  • Call close_async() when the user leaves the translation feature, so the models do not stay in memory.
  • Say in your privacy policy (and the Play data safety form) that text is translated on the device, and that the app downloads language models from Google. This package sends no text anywhere.
  • Keep long text under MAX_TEXT; split by paragraph.
  • Test on a real phone: the emulator works but is slower, and its first download can take a while.

Testing

On a phone or emulator, run the quick start screen, tap Download Hindi (the emulator needs network), then translate a sentence. Turn on airplane mode and translate again: it still works.

Unit tests run without a device. The module is a set of plain async functions, so swap in fakes with monkeypatch:

import asyncio

import pyxpo_translate as Translate

ON_PHONE = {"en"}


async def fake_ready(source, target="en"):
    return source in ON_PHONE and target in ON_PHONE


async def fake_download(language, wifi_only=False):
    ON_PHONE.add(language)


async def fake_translate(text, source, target="en"):
    if source not in ON_PHONE:
        raise RuntimeError("not on the phone: %s (download_async first)" % source)
    return "[%s>%s] %s" % (source, target, text)


def test_downloads_once_then_translates(monkeypatch):
    monkeypatch.setattr(Translate, "is_ready_async", fake_ready)
    monkeypatch.setattr(Translate, "download_async", fake_download)
    monkeypatch.setattr(Translate, "translate_async", fake_translate)

    async def run():
        if not await Translate.is_ready_async("hi"):
            await Translate.download_async("hi")
        return await Translate.translate_async("नमस्ते", "hi")

    assert asyncio.run(run()) == "[hi>en] नमस्ते"

Troubleshooting

Problem Cause and fix
download_async never returns wifi_only=True and the phone is on mobile data, or the network is down. Show a cancel option; try again on Wi-Fi.
the hi model did not download No network, low storage, or Google's servers were not reachable (some networks block them). Try another network.
The first translation is slow The model loads on the first call for a pair (about a second). Translate a short text when the feature opens to warm it up.
A Hinglish sentence comes back half translated ML Kit expects one language. Latin-letter Hindi words are often left as they are. Pass Devanagari text, or clean it up first (for example with a language model).
Names or brand words are translated ML Kit translates every word it knows. Put such words back yourself, or keep them out of the text.
NotSupportedError on iOS or in the browser There is no translator there yet. Check status_async()["supported"].
ModuleNotLinkedError The build does not include the module. Add it to [tool.pyxpo] modules and rebuild.
The Android build hangs downloading ML Kit Gradle cannot reach dl.google.com. Check the network; if IPv4 to Google is blocked, JAVA_TOOL_OPTIONS=-Djava.net.preferIPv6Addresses=true can help.
RuntimeWarning: coroutine ... was never awaited The check was started with use_effect(lambda: coro()). Pass use_async an async def that awaits Translate.is_ready_async("hi").

FAQ

Does my text leave the phone? No. Translation runs on the device and the package never sends text anywhere or logs it. The network is used only by download_async, to fetch model files.

Why does the app need the INTERNET permission? For download_async. If your app never downloads models, it still has the permission (it comes with the module), but nothing is sent.

How good is the translation? ML Kit's models are small and run on the phone, so they are good for everyday sentences and weaker than large cloud models for idioms and long, formal text. To polish the result (tone, grammar), run the English through an on-device language model afterwards.

Can I ship the model inside the app? Not with ML Kit: its translation models are always downloaded at run time.

Will iOS be supported? Not in this release. Apple's on-device Translation framework is the likely route.

API reference

Functions

Name Description
status_async() -> dict {"supported": bool, "downloaded": [codes]}. Never raises on other platforms.
languages_async() -> list Every language code ML Kit can translate.
downloaded_async() -> list The codes whose models are on the phone; always includes "en".
is_ready_async(source, target="en") -> bool True when both languages are on the phone.
download_async(language, wifi_only=False) -> None Downloads one language's model; returns when it is on the phone.
delete_async(language) -> None Removes a downloaded model. English cannot be removed.
translate_async(text, source, target="en") -> str Translates on the phone; raises when a language is not downloaded.
close_async() -> None Frees the open translators. Does nothing on other platforms.
statusAsync, languagesAsync, downloadedAsync, isReadyAsync, downloadAsync, deleteAsync, translateAsync, closeAsync Expo-style names for the functions above.

Constants and types

Name Description
MAX_TEXT The longest text one translate_async call takes: 10,000 characters.
NotSupportedError Raised where there is no translator (re-exported from pyxpo.module_api).

License

MIT. ML Kit is a Google product; this package is not made or endorsed by Google, and using ML Kit means agreeing to Google's ML Kit terms.

Metadata

Release files for pyxpo-translate 0.1.0a1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyxpo-translate 0.1.0a1
File Size Uploaded Provenance Actions
pyxpo_translate-0.1.0a1.tar.gz 20.7 kB With provenance Details for pyxpo_translate-0.1.0a1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pyxpo-translate 0.1.0a1
File Interpreter ABI Platform Actions
pyxpo_translate-0.1.0a1-py3-none-any.whl Python 3 none any Details for pyxpo_translate-0.1.0a1-py3-none-any.whl

Total release size: 34.5 kB

Release files / pyxpo_translate-0.1.0a1.tar.gz

Download URL pyxpo_translate-0.1.0a1.tar.gz
Size 20.7 kB
Tags Source
SHA-256 checksum
How to use checksums
dbf848f1a18ec3e57a5e4c702785ae9870843106a795c930de3a4194330c3bac
BLAKE2b-256 checksum
How to use checksums
c44bbbcb2e4a28ad2fb48f7967e9bea52868639372029d8e2dd98643a13e7816
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 29, 2026.

Transparency log

Release files / pyxpo_translate-0.1.0a1-py3-none-any.whl

Download URL pyxpo_translate-0.1.0a1-py3-none-any.whl
Size 13.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
27a641597536ef842ce40744ab0d2396de2f50f0327f3c0e216f25cab9034d89
BLAKE2b-256 checksum
How to use checksums
648d4b46077bec8c9841d1ec587b2f82cb3e419d4a120be974598999af1705d2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 29, 2026.

Transparency log

Security for pyxpo-translate 0.1.0a1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pyxpo_translate-0.1.0a1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 29, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page