Skip to content

@capacitor-mlkit/translation

Unofficial Capacitor plugin for ML Kit Translation.1

Installation

npm install @capacitor-mlkit/translation
npx cap sync

Android

Variables

This plugin will use the following project variables (defined in your app’s variables.gradle file):

  • $mlkitTranslateVersion version of com.google.mlkit:translate (default: 17.0.1)

Configuration

No configuration required for this plugin.

Demo

A working example can be found here: robingenz/capacitor-mlkit-plugin-demo

Usage

import { Translation, Language } from '@capacitor-mlkit/translation';

const deleteDownloadedModel = async () => {
  await Translation.deleteDownloadedModel({
    language: Language.English,
  });
};

const downloadModel = async () => {
  await Translation.downloadModel({
    language: Language.English,
  });
};

const getDownloadedModels = async () => {
  const { languages } = await Translation.getDownloadedModels();
  return languages;
};

const translate = async () => {
  const { text } = await Translation.translate({
    text: 'Good morning!',
    sourceLanguage: Language.English,
    targetLanguage: Language.German,
  });
  return text;
};

API

deleteDownloadedModel(...)

deleteDownloadedModel(options: DeleteDownloadedModelOptions) => Promise<void>

Delete the language model for the given language.

Only available on Android and iOS.

Param Type
options DeleteDownloadedModelOptions

Since: 0.0.1


downloadModel(...)

downloadModel(options: DownloadModel) => Promise<void>

Download a language model for offline translation.

Language models are around 30MB in size, so be sure to only download the models you need and only download them using a WiFi connection unless the user has specified otherwise.

Only available on Android and iOS.

Param Type
options DownloadModel

Since: 0.0.1


getDownloadedModels()

getDownloadedModels() => Promise<GetDownloadedModelsResult>

Get the languages for which a model has been downloaded.

Only available on Android and iOS.

Returns: Promise<GetDownloadedModelsResult>

Since: 0.0.1


translate(...)

translate(options: TranslateOptions) => Promise<TranslateResult>

Translate the given text.

If the language model for the given source and target languages is not downloaded, it will be downloaded automatically which may take some time. If you want to avoid this, use the downloadModel(...) method to download the model first.

Only available on Android and iOS.

Param Type
options TranslateOptions

Returns: Promise<TranslateResult>

Since: 0.0.1


Interfaces

DeleteDownloadedModelOptions

Prop Type Description Since
language Language The language for which to delete the model. 0.0.1

DownloadModel

Prop Type Description Since
language Language The language to download the model for. 0.0.1

GetDownloadedModelsResult

Prop Type Description
languages Language[] The languages for which a model has been downloaded.

TranslateResult

Prop Type Description Since
text string The translated text. 0.0.1

TranslateOptions

Prop Type Description Since
text string The text to translate. 0.0.1
sourceLanguage Language The source language of the text. If you don't know the source language, you can use the Language</a> Identification plugin to detect it. 0.0.1
targetLanguage Language The target language to translate the text to. 0.0.1

Enums

Language

Members Value Since
Afrikaans 'af' 0.0.1
Arabic 'ar' 0.0.1
Belarusian 'be' 0.0.1
Bulgarian 'bg' 0.0.1
Bengali 'bn' 0.0.1
Catalan 'ca' 0.0.1
Czech 'cs' 0.0.1
Welsh 'cy' 0.0.1
Danish 'da' 0.0.1
German 'de' 0.0.1
Greek 'el' 0.0.1
English 'en' 0.0.1
Esperanto 'eo' 0.0.1
Spanish 'es' 0.0.1
Estonian 'et' 0.0.1
Persian 'fa' 0.0.1
Finnish 'fi' 0.0.1
French 'fr' 0.0.1
Irish 'ga' 0.0.1
Galician 'gl' 0.0.1
Gujarati 'gu' 0.0.1
Hebrew 'he' 0.0.1
Hindi 'hi' 0.0.1
Croatian 'hr' 0.0.1
Haitian 'ht' 0.0.1
Hungarian 'hu' 0.0.1
Indonesian 'id' 0.0.1
Icelandic 'is' 0.0.1
Italian 'it' 0.0.1
Japanese 'ja' 0.0.1
Georgian 'ka' 0.0.1
Kannada 'kn' 0.0.1
Korean 'ko' 0.0.1
Lithuanian 'lt' 0.0.1
Latvian 'lv' 0.0.1
Macedonian 'mk' 0.0.1
Marathi 'mr' 0.0.1
Malay 'ms' 0.0.1
Maltese 'mt' 0.0.1
Dutch 'nl' 0.0.1
Norwegian 'no' 0.0.1
Polish 'pl' 0.0.1
Portuguese 'pt' 0.0.1
Romanian 'ro' 0.0.1
Russian 'ru' 0.0.1
Slovak 'sk' 0.0.1
Slovenian 'sl' 0.0.1
Albanian 'sq' 0.0.1
Swedish 'sv' 0.0.1
Swahili 'sw' 0.0.1
Tamil 'ta' 0.0.1
Telugu 'te' 0.0.1
Thai 'th' 0.0.1
Tagalog 'tl' 0.0.1
Turkish 'tr' 0.0.1
Ukrainian 'uk' 0.0.1
Urdu 'ur' 0.0.1
Vietnamese 'vi' 0.0.1
Chinese 'zh' 0.0.1

Terms & Privacy

This plugin uses the Google ML Kit:

Changelog

See CHANGELOG.md.

License

See LICENSE.


  1. This project is not affiliated with, endorsed by, sponsored by, or approved by Google LLC or any of their affiliates or subsidiaries.