Before API development, you need to make necessary development preparations, ensure that the Maven repository address of the HMS Core SDK has been configured in your project, and the SDK of this service has been integrated.
1. Create a local offline translator. You can create the translator using the MLLocalTranslateSetting class.
- // Create an offline translator.
- MLLocalTranslateSetting setting = new MLLocalTranslateSetting.Factory()
- // Set the source language code. The ISO 639-1 standard is used. This parameter is mandatory. If this parameter is not set, an error may occur.
- .setSourceLangCode("zh")
- // Set the target language code. The ISO 639-1 standard is used. This parameter is mandatory. If this parameter is not set, an error may occur.
- .setTargetLangCode("en")
- .create();
- final MLLocalTranslator mlLocalTranslator = MLTranslatorFactory.getInstance().getLocalTranslator(setting);
2. Download the offline model required for local offline translation in either of the following ways:
Use MLLocalModelManager to manually download the required model package.
Use preparedModel to automatically download the required model package.
You can select a mode as required.
- // Method 1:
- // After the download is successful, translate text in the onSuccess callback.
- // Obtain the model manager.
- MLLocalModelManager manager = MLLocalModelManager.getInstance();
- MLLocalTranslatorModel model = new MLLocalTranslatorModel.Factory(sourceLangCode).create();
- MLModelDownloadStrategy downloadStrategy = new MLModelDownloadStrategy.Factory()
- .needWifi() // It is recommended that you download the package in a Wi-Fi environment.
- .create();
- manager.downloadModel(model, downloadStrategy).addOnSuccessListener(new OnSuccessListener<Void>() {
- @Override
- public void onSuccess(Void aVoid) {
- // Called when the model package is successfully downloaded.
- }
- }).addOnFailureListener(new OnFailureListener() {
- @Override
- public void onFailure(Exception e) {
- // Called when the model package fails to be downloaded.
- }
- });
- // Method 2:
- MLModelDownloadStrategy downloadStrategy = new MLModelDownloadStrategy.Factory()·
- .needWifi() // It is recommended that you download the package in a Wi-Fi environment.
- .create();
- mlLocalTranslator.preparedModel(downloadStrategy).
- addOnSuccessListener(new OnSuccessListener<Void>() {
- @Override
- // Called when the model package is successfully downloaded.
- public void onSuccess (Void aVoid){
- Log.i(TAG, "translate download model success");
- }
- }).addOnFailureListener(new OnFailureListener() {
- @Override
- public void onFailure (Exception e){
- // Called when the model package fails to be downloaded.
- }
- });
3. Implement text translation.
- final Task<String> task = mlLocalTranslator.asyncTranslate(input); // input is a string of less than 5000 characters.
- // Before translation, ensure that the model is successfully downloaded.
- task.addOnSuccessListener(new OnSuccessListener<String>() {
- @Override
- public void onSuccess(String s) {
- // Processing logic for recognition success.
- }
- }).addOnFailureListener(new OnFailureListener() {
- @Override
- public void onFailure(Exception e) {
- // Processing logic for detection failure.
- }
- });
4. Delete an unnecessary model package
- // Obtain the model manager.
- MLLocalModelManager manager = MLLocalModelManager.getInstance();
- MLLocalTranslatorModel model = new MLLocalTranslatorModel
- .Factory("zh") // Set the target language code (ISO 639-1 standard) for the model.
- .create();
- // Delete a model package.
- manager.deleteModel(model).addOnSuccessListener(new OnSuccessListener<Void>() {
- public void onSuccess(Void aVoid) {
- Log.i(TAG, "deleteModel: " + model.getModelName() + " success");
- }
- }).addOnFailureListener(new OnFailureListener() {
- @Override
- public void onFailure(Exception e) {
- // Called when the model package fails to be deleted.
- }
- });
5. Release resources after the translation is complete
- if (mlRemoteTranslator!= null) {
- mlRemoteTranslator.stop();
- }
1. Create a text translator. You can create the translator using the MLRemoteTranslateSetting class.
- // Create a text translator using custom parameter settings.
- MLRemoteTranslateSetting setting = new MLRemoteTranslateSetting
- .Factory()
- // Set the source language code. The ISO 639-1 standard is used. This parameter is optional. If this parameter is not set, the system automatically detects the language.
- .setSourceLangCode("zh")
- // Set the target language code. The ISO 639-1 standard is used.
- .setTargetLangCode("en")
- .create();
- MLRemoteTranslator mlRemoteTranslator = MLTranslatorFactory.getInstance().getRemoteTranslator(setting);
2. Implement text translation
- // sourceText: text to be translated, with up to 5000 characters.
- final Task<String> task = mlRemoteTranslator .asyncTranslate(sourceText);
- task.addOnSuccessListener(new OnSuccessListener<String>() {
- @Override
- public void onSuccess(String text) {
- // Processing logic for recognition success.
- }
- }).addOnFailureListener(new OnFailureListener() {
- @Override
- public void onFailure(Exception e) {
- // Processing logic for recognition failure.
- try {
- MLException mlException = (MLException)e;
- // Obtain the result codes. You can process the result codes and customize respective messages displayed to users. For details about the result codes, please refer to Result Codes of ML Kit.
- int errorCode = mlException.getErrCode();
- // Obtain the error information. You can quickly locate the fault based on the result code.
- String errorMessage = mlException.getMessage();
- } catch (Exception error) {
- // Handle the conversion error.
- }
- }
- });
3. Release resources after the translation is complete.
- if (mlRemoteTranslator!= null) {
- mlRemoteTranslator.stop();
- }