HMS ML Kit: Real-Time Translation (iOS — Swift)

Ali Mert Özhayta
Huawei Developers
Published in
3 min readMay 27, 2021

In this article, we are going to cover how to integrate and use HMS ML Kit’s Real-time Translation feature on iOS.

HUAWEI ML Kit allows your apps to easily leverage Huawei’s long-term proven expertise in machine learning to support diverse artificial intelligence (AI) applications throughout a wide range of industries. Thanks to Huawei’s technology accumulation, ML Kit provides diversified leading machine learning capabilities that are easy to use, helping you develop various AI apps.

Real-time Translation: The real-time translation service can translate text from the source language into the target language through the server on the cloud. Currently, real-time translation supports 40 languages. For details, please refer to Languages Supported by Translation.

Enabling the Services

Data storage location

First of all “data storage location” must be configured for ML services. Go to Project Setting > General information, click Set next to Data storage location under Project Information.

Enable ML Kit in Manage APIs

Then you need to enable ML kit from Project Setting > Manage APIs > ML kit. After that go to Develop > Build > ML Kit so that you can view information about the service usage.

Download ‘agconnect-services.plist’ file

P.S.: Don’t forget to download and add “agconnect-services.plist” file to app folder of your project.

Integration Using Pods

CocoaPods can be used to integrate HMS services.

  1. Open terminal and change directory to project folder to create a Podfile (If you haven’t create it already).
$ cd your-project-directory
$ pod init

2. Edit the Podfile to add pod dependencies:

# Pods for HMS ML Kit - Translationpod 'AGConnectCore'pod 'MLTranslate', '~>2.0.5.300'

3. Finally, run the code below:

pod install

4. When you run pod install code, another file with .xcworkspace extension will be created. Open your project with using .xcworkspace file.

Development

ML Kit requires an API Key for translation. The API Key is located in the “agconnect-services.plist” file or you can get on AGC Project Settings. To retrieve it from the plist file, import AGConnectCore. Also import MLTranslate for setting the API Key and translation.

import AGConnectCore
import MLTranslate

You can retrieve and set API Key as follows:

To get all supported languages, use MLTranslateLanguage.getCloudAllLanguages() method.

Set source and target languages to translate text from one to another. To set it you need to define custom parameter settings. Languages must be defined with language codes. You can check supported languages and language codes from here.

Finally, use MLRemoteTranslator.sharedInstance().asyncTranslate() method to translate text asynchronously.

Demo

Here is a demo project that I developed by using the methods above.

You can access the source codes of the ‘HMS ML Kit-Translation’ demo project from the link down below:

References

If you need more details about HMS ML Kit, you can examine below links.

--

--