[Chargeable] Firebase ML-Vision Translate

DonManfred

Expert
Licensed User
Longtime User
This is a wrap for the Firebase ML-Vision Translate components.

With ML Kit's on-device translation API, you can dynamically translate text between 59 languages.

The Languagecodes understood are:
AF, AR, BE, BG, BN, CA, CS, CY, DA, DE, EL, EN, EO, ES, ET, FA, FI. FR, GA, GL, GU, HE, HI, HR, HT, HU, ID, IS, IT, JA, KA, KN, KO, LT, LV, MK, MR, MS, MT, NL, NO, PL, PT, RO, RU, SK, SL, SQ, SV, SW, TA, TE, TH, TL, TR, UK, UR, VI, ZH

On-device translation is intended for casual and simple translations, and the quality of translations depends on the specific languages being translated from and to. As such, you should evaluate the quality of the translations for your specific use case. If you require higher fidelity, try the Cloud Translation API.

Also, ML Kit's translation models are trained to translate to and from English. When you translate between non-English languages, English is used as an intermediate translation, which can affect quality.

Refer to Usage Guidelines for ML Kit On-device Translation for important guidelines and restrictions on usage of this API. This document covers requirements around doing attribution in your app when translating text.

FirebaseMLTranslate
<link>...|https://www.b4x.com</link>
Author: DonManfred
Version: 1.05
  • FirebaseModelDownloadConditions
    • Functions:
      • Initialize (model As com.google.firebase.ml.common.modeldownload.FirebaseModelDownloadConditions) As FirebaseModelDownloadConditions
      • IsInitialized As Boolean
    • Properties:
      • ChargingRequired As Boolean [read only]
      • DeviceIdleRequired As Boolean [read only]
      • WifiRequired As Boolean [read only]
  • FirebaseModelDownloadConditionsBuilder
    • Functions:
      • build As com.google.firebase.ml.common.modeldownload.FirebaseModelDownloadConditions
      • Initialize (EventName As String) As FirebaseModelDownloadConditionsBuilder
      • IsInitialized As Boolean
      • requireCharging As FirebaseModelDownloadConditionsBuilder
      • requireDeviceIdle As FirebaseModelDownloadConditionsBuilder
      • requireWifi As FirebaseModelDownloadConditionsBuilder
  • FirebaseModelManager
    • Events:
      • AvailableModels (success As Boolean, info As String, models As List)
      • ModelDeleted (success As Boolean, info As String)
      • ModelDownload (success As Boolean, info As String)
    • Functions:
      • deleteDownloadedModel (model As com.google.firebase.ml.naturallanguage.translate.FirebaseTranslateRemoteModel)
      • downloadRemoteModelIfNeeded (model As com.google.firebase.ml.common.modeldownload.FirebaseRemoteModel)
      • GetAvailableModels
      • getLocalModel (modelname As String) As com.google.firebase.ml.common.modeldownload.FirebaseLocalModel
      • getNonBaseRemoteModel (modelname As String) As com.google.firebase.ml.common.modeldownload.FirebaseRemoteModel
      • Initialize (EventName As String) As FirebaseModelManager
      • registerLocalModel (model As com.google.firebase.ml.common.modeldownload.FirebaseLocalModel) As Boolean
      • registerRemoteModel (model As com.google.firebase.ml.common.modeldownload.FirebaseRemoteModel) As Boolean
  • FirebaseTranslateRemoteModel
    • Functions:
      • baseModelHashMatches (hash As String) As Boolean
      • Initialize (model As com.google.firebase.ml.naturallanguage.translate.FirebaseTranslateRemoteModel) As FirebaseTranslateRemoteModel
      • IsInitialized As Boolean
    • Properties:
      • BaseModel As Boolean [read only]
      • InitialDownloadConditions As com.google.firebase.ml.common.modeldownload.FirebaseModelDownloadConditions [read only]
      • Language As Int [read only]
      • LanguageCode As String [read only]
      • ModelHash As String [read only]
      • ModelName As String [read only]
      • ModelUpdatesEnabled As Boolean [read only]
      • UpdatesDownloadConditions As com.google.firebase.ml.common.modeldownload.FirebaseModelDownloadConditions [read only]
  • FirebaseTranslateRemoteModelBuilder
    • Functions:
      • build As com.google.firebase.ml.naturallanguage.translate.FirebaseTranslateRemoteModel
      • DownloadConditions (conditions As com.google.firebase.ml.common.modeldownload.FirebaseModelDownloadConditions) As FirebaseTranslateRemoteModelBuilder
      • Initialize (languageID As Int) As FirebaseTranslateRemoteModelBuilder
      • IsInitialized As Boolean
  • FirebaseTranslatorOptionsBuilder
    • Fields:
      • AF As Int
      • AR As Int
      • BE As Int
      • BG As Int
      • BN As Int
      • CA As Int
      • CS As Int
      • CY As Int
      • DA As Int
      • DE As Int
      • EL As Int
      • EN As Int
      • EO As Int
      • ES As Int
      • ET As Int
      • FA As Int
      • FI As Int
      • FR As Int
      • GA As Int
      • GL As Int
      • GU As Int
      • HE As Int
      • HI As Int
      • HR As Int
      • HT As Int
      • HU As Int
      • ID As Int
      • IS As Int
      • IT As Int
      • JA As Int
      • KA As Int
      • KN As Int
      • KO As Int
      • LT As Int
      • LV As Int
      • MK As Int
      • MR As Int
      • MS As Int
      • MT As Int
      • NL As Int
      • NO As Int
      • PL As Int
      • PT As Int
      • RO As Int
      • RU As Int
      • SK As Int
      • SL As Int
      • SQ As Int
      • SV As Int
      • SW As Int
      • TA As Int
      • TE As Int
      • TH As Int
      • TL As Int
      • TR As Int
      • UK As Int
      • UR As Int
      • VI As Int
      • ZH As Int
    • Functions:
      • build As com.google.firebase.ml.naturallanguage.translate.FirebaseTranslatorOptions
      • Initialize (EventName As String) As FirebaseTranslatorOptionsBuilder
      • IsInitialized As Boolean
      • SrcLanguage (languageID As Int) As FirebaseTranslatorOptionsBuilder
      • SrcLanguage2 (language As String) As FirebaseTranslatorOptionsBuilder
      • TargetLanguage (languageID As Int) As FirebaseTranslatorOptionsBuilder
      • TargetLanguage2 (language As String) As FirebaseTranslatorOptionsBuilder
  • FirebaseVisionTranslate
    • Events:
      • ModelDownload (success As Boolean, info As String)
      • TranslateFinished (success As Boolean, translatedText As String)
    • Functions:
      • close
      • downloadModelIfNeeded (conditions As com.google.firebase.ml.common.modeldownload.FirebaseModelDownloadConditions)
      • GetAvailableLanguages As java.util.List
      • Initialize (EventName As String, options As com.google.firebase.ml.naturallanguage.translate.FirebaseTranslatorOptions)
      • translate (text As String)

Setup:
- Integrate Firebase in your App. You need at least Play Services Base and Firebase Snippets.
https://www.b4x.com/android/forum/threads/integrating-firebase-services.67692/
- Add the following to your Manifesteditor

B4X:
AddApplicationText(
<service android:name="com.google.firebase.components.ComponentDiscoveryService" android:exported="false">
<meta-data android:name="com.google.firebase.components:com.google.firebase.ml.naturallanguage.NaturalLanguageRegistrar" android:value="com.google.firebase.components.ComponentRegistrar"/>
<meta-data android:name="com.google.firebase.components:com.google.firebase.ml.naturallanguage.translate.NaturalLanguageTranslateRegistrar" android:value="com.google.firebase.components.ComponentRegistrar"/>
<meta-data android:name="com.google.firebase.components:com.google.firebase.remoteconfig.RemoteConfigRegistrar" android:value="com.google.firebase.components.ComponentRegistrar"/>
<meta-data android:name="com.google.firebase.components:com.google.firebase.iid.Registrar" android:value="com.google.firebase.components.ComponentRegistrar"/>
<meta-data android:name="com.google.firebase.components:com.google.firebase.abt.component.AbtRegistrar" android:value="com.google.firebase.components.ComponentRegistrar"/>
</service>
<provider
  android:authorities="${applicationId}.firebaseinitprovider"
  android:name="com.google.firebase.provider.FirebaseInitProvider"
  android:exported="false"
  android:initOrder="100" />
)
- Add this code to your Main
B4X:
#AdditionalJar: com.google.firebase:firebase-core
#AdditionalJar: com.google.firebase:firebase-ml-natural-language
#AdditionalJar: com.google.firebase:firebase-ml-natural-language-translate
#AdditionalJar: com.google.firebase:firebase-ml-natural-language-translate-model
#AdditionalJar: com.google.firebase:firebase-ml-natural-language-smart-reply
#AdditionalJar: com.google.firebase:firebase-ml-natural-language-language-id-model

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Public tr As FirebaseVisionTranslate
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")

    Dim tloptb As FirebaseTranslatorOptionsBuilder
    tloptb.Initialize("").SrcLanguage(tloptb.DE).TargetLanguage(tloptb.EN)
    tr.Initialize("Translate",tloptb.build)
    Dim condbld As FirebaseModelDownloadConditionsBuilder
    condbld.Initialize("").requireCharging.requireWifi
    tr.downloadModelIfNeeded(condbld.build)
    wait for Translate_ModelDownload(success As Boolean, info As String)
    If success Then
        tr.translate("Hello B4X Community. Ich freue mich euch die Library MLTranslate vorstellen zu können. In diesem Beispiel werde ich einen deutschen Text ins Englische übersetzen lassen.")
    End If
    wait for Translate_TranslateFinished(success As Boolean, translatedText As String)
    Log($"Translate_TranslateFinished(${success}, ${translatedText})"$)
End Sub
Sub Translate_ModelDownload(success As Boolean, info As String)
    Log($"Translate_ModelDownload(${success}, ${info})"$)
End Sub
Sub Translate_TranslateFinished(success As Boolean, translatedText As String)
    Log($"Translate_TranslateFinished(${success}, ${translatedText})"$)
End Sub

this is the logoutput from the code used here.

The example is attached here too but it only works if you have the Library. You need to buy, download the library and extract the libraryfiles into your additional library folder.

But you can use the Example to have a look at the needed Code.
 
Last edited:
Top