目录

class info
com.huawei.hms.mlsdk.translate.cloud.MLRemoteTranslator
Represents a text translator that translates the source language into the target language based on the supplied text. After the cloud API is called, the detection model runs on the cloud.

Sample Code

  1. // Create a text translator.
  2. // setSourceLangCode: Set the code of the source language. ISO 639-1 standard code.
  3. // setTargetLangCode: Set the code of the target language. ISO 639-1 standard code.
  4. MLRemoteTranslateSetting settings = new MLRemoteTranslateSetting.Factory()
  5.         .setSourceLangCode(sourceLangCode)
  6.         .setTargetLangCode(targetLangCode)
  7.         .create();
  8. MLRemoteTranslator mlRemoteTranslator = MLTranslatorFactory.getInstance().getRemoteTranslator(settings);
  9. // Translate text.
  10. // sourceText: text to be translated.
  11. final Task<String> task = mlRemoteTranslator.asyncTranslate(sourceText);
  12. task.addOnSuccessListener(new OnSuccessListener<String>() {
  13.    public void onSuccess(String text) {
  14.        // Processing logic for success.      
  15.    }
  16. }).addOnFailureListener(new OnFailureListener() {
  17.    public void onFailure(Exception e) {
  18.        // Processing logic for failure.
  19.    }
  20. });
  21. // Release resources.
  22. mlRemoteTranslator.stop();

Public Method Summary

Return typeMethod name
com.huawei.hmf.tasks.TaskasyncTranslate(String text)
MLRemoteTranslatorcreate(MLApplication app, MLRemoteTranslateSetting setting)
voidstop()

Public Methods

Method
public com.huawei.hmf.tasks.TaskasyncTranslate(String text)
Translates text in asynchronous mode.

Parameters

Parameter nameParameter desc
textText to be translated.

Return

Typedesc
com.huawei.hmf.tasks.TaskTranslation result.
Method
public static synchronized MLRemoteTranslator create(MLApplication app, MLRemoteTranslateSetting setting)
Creates a text translator instance.

Parameters

Parameter nameParameter desc
appApp instance.
settingReturns a text translation configurator instance.

Return

Typedesc
MLRemoteTranslatorReturns the text translator instance.
Method
public void stop()
Releases resources, including input and output streams.