| 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. |
- // Create a text translator.
- // setSourceLangCode: Set the code of the source language. ISO 639-1 standard code.
- // setTargetLangCode: Set the code of the target language. ISO 639-1 standard code.
- MLRemoteTranslateSetting settings = new MLRemoteTranslateSetting.Factory()
- .setSourceLangCode(sourceLangCode)
- .setTargetLangCode(targetLangCode)
- .create();
- MLRemoteTranslator mlRemoteTranslator = MLTranslatorFactory.getInstance().getRemoteTranslator(settings);
- // Translate text.
- // sourceText: text to be translated.
- final Task<String> task = mlRemoteTranslator.asyncTranslate(sourceText);
- task.addOnSuccessListener(new OnSuccessListener<String>() {
- public void onSuccess(String text) {
- // Processing logic for success.
- }
- }).addOnFailureListener(new OnFailureListener() {
- public void onFailure(Exception e) {
- // Processing logic for failure.
- }
- });
- // Release resources.
- mlRemoteTranslator.stop();
| Return type | Method name |
| com.huawei.hmf.tasks.Task | asyncTranslate(String text) |
| MLRemoteTranslator | create(MLApplication app, MLRemoteTranslateSetting setting) |
| void | stop() |
| Method |
| public com.huawei.hmf.tasks.TaskasyncTranslate(String text) |
| Translates text in asynchronous mode. |
Parameters
| Parameter name | Parameter desc |
| text | Text to be translated. |
Return
| Type | desc |
| com.huawei.hmf.tasks.Task | Translation result. |
| Method |
| public static synchronized MLRemoteTranslator create(MLApplication app, MLRemoteTranslateSetting setting) |
| Creates a text translator instance. |
Parameters
| Parameter name | Parameter desc |
| app | App instance. |
| setting | Returns a text translation configurator instance. |
Return
| Type | desc |
| MLRemoteTranslator | Returns the text translator instance. |
| Method |
| public void stop() |
| Releases resources, including input and output streams. |