This document describes only the definition of APIs. For details about how and when to call these APIs, please refer to Text Translation.
Add the following configuration to the features attribute in the manifest.json file:
{"name": "service.ml.translate"} Add the following configuration to the script element on the page where the API will be called:
import translate from '@service.ml.translate'
Or
var translate = require("@service.ml.translate") Item | Description |
|---|---|
Applicable devices | Mobile phones and tablets |
Applicable regions | Global |
Description
Translates texts and obtains the translation result from the callback.
Parameters
Parameter | Type | Mandatory | Description |
|---|---|---|---|
appId | string | Yes | Value of app_id in Obtaining the agconnect-services.json File. |
apiKey | string | Yes | Value of api_key in Obtaining the agconnect-services.json File. You need to set and keep your API key properly. For details, please refer to Notes on Using Cloud Authentication Information. |
sourceLanguageCode | string | No | Source language code. The value is an ISO 639-1 language code, for example, en. The BCP-47 language code zh-HK is used for traditional Chinese. If this parameter is not set, the system automatically detects the language and starts translation. |
targetLanguageCode | string | Yes | Target language code. The value is an ISO 639-1 language code, for example, zh. The BCP-47 language code zh-HK is used for traditional Chinese. |
sourceText | string | Yes | Text to be translated. A maximum of 5000 characters can be translated at a time. The input data format is UTF-8. |
success | function | Yes | Callback function. A parameter of the string type is returned, indicating the text after translation. |
fail | function | No | Callback method, which is called after an error occurs. |
Parameters returned by success
Parameter | Type | Description |
|---|---|---|
text | string | Text generated after translation. |
Result codes returned by fail
Result Code | Description | Suggestion |
|---|---|---|
-1 | Unknown error. | - |
0 | Service running properly. | - |
1 | Operation canceled. | - |
2 | Internal error. | - |
3 | Service unavailable. | - |
4 | Operation not supported. | - |
5 | Incorrect parameter. This exception is irrelevant to services. | Modify the parameter. |
6 | Operation timed out. | - |
7 | Requested resource not found or not exist. | - |
8 | Resource created repeatedly. | - |
9 | No operation permission. | - |
10 | Resources exhausted. The system resources or resources of the current user may be insufficient. | - |
11 | Operation failed because the system was not ready. | - |
12 | Operation interrupted, possibly due to concurrent operations. | - |
13 | Out-of-bounds access. | - |
Sample code
translate.translate({// Start speech recognition.
appId: 'App ID that you have applied for.', // String, mandatory.
apiKey: 'API key that you have applied for.', // String, mandatory. This is parameter is set for authentication.
sourceLanguageCode: 'en', // Source language code. The ISO 639-1 language code is used, for example, en. The BCP-47 language code is used for traditional Chinese, such as zh-HK. If this parameter is not set, the system automatically detects the language and starts translation. This parameter of the string type.
targetLanguageCode: 'zh', // Target language code. The ISO 639-1 language code is used, for example, zh. The BCP-47 language code is used for traditional Chinese, such as zh-HK. This parameter of the string type.
sourceText:'I am Chinese.', // Text to be translated. A maximum of 5000 characters can be translated at a time. The value is of the string type. The input data format is UTF-8.
success: (text) => {// Callback method, indicating the text generated after translation.
console.log(text);
},
fail: (errMsg, errorCode) => {// Callback upon failure.
console.log("error:", errMsg);
},
});