v161 · concept 5 · practical + composition
Offline chat composer
The use case behind the Android launch: a customer writes to support in their own language on a phone. LanguageDetector (on Android since 157) finds the language, Translator converts it to the agent's language, and the text leaves the device already translated, even on a flaky connection. Everything degrades honestly. If a model isn't installed, the message goes out marked not translated, and you can install the pack with one explicit tap.
Checking which models this device can already use…
Hi! I'm the (sample) support agent. Write in any language and your phone will translate it before sending.
the code
// On send (a user gesture), use only models that are already "available".
const det = await LanguageDetector.availability() === "available" ? await LanguageDetector.create() : null;
const lang = det ? (await det.detect(text))[0].detectedLanguage : chosenLanguage;
const state = await Translator.availability({ sourceLanguage: lang, targetLanguage: agentLang });
const body = state === "available"
? await (await Translator.create({ sourceLanguage: lang, targetLanguage: agentLang })).translate(text)
: text; // sent untranslated, and labelled as such
send({ body, original: text, translated: state === "available" });