Text Translation

  • Example request
    //You are advised to construct and use the client in a singleton pattern to avoid frequent object creation.
    MtClient client = new MtClient(AuthMode.AKSK,authInfo);
    
    TextTranslateReq req = new TextTranslateReq();
    
    //Text to be translated
    req.setText("Text to translate");
    
    //Source language. zh indicates Chinese and auto indicates automatic identification.
    req.setFrom("zh");
    
    //Target language. en indicates English.
    req.setTo("en");
    
    //Optional. Application scenario. Currently, only common is supported.
    req.setScene("common");
    
    try {
        TextTranslateResp resp = client.translateText(req);
    } catch (NlpException e) {
        //A failure is thrown as an exception. For details about exceptions, see section "Exceptions". The client automatically processes exceptions.
    }
  • Table 1 describes TextTranslateReq parameters.
    Table 1 TextTranslateReq parameters

    Parameter

    Mandatory

    Type

    Description

    text

    Yes

    String

    Text to be translated

    from

    Yes

    String

    Abbreviation of the source language. auto indicates automatic identification. Table 3 lists the supported languages.

    to

    Yes

    String

    Abbreviation of the target language. For details about supported languages, see Table 3.

    scene

    No

    String

    Only the default value common is currently supported, indicating a common scenario.

  • Table 2 describes TextTranslateResp parameters.
    Table 2 TextTranslateResp parameters

    Parameter

    Type

    Description

    srcText

    String

    Text to be translated

    translatedText

    String

    Translation result

    from

    String

    Source language when the API call succeeds (if the source language is set to auto, the language detection result is displayed). It is encoded using UTF-8.

    This parameter is not included when the API call fails.

    to

    String

    Target language when the API call succeeds. It is encoded using UTF-8.

    This parameter is not included when the API call fails.

    Table 3 Supported languages

    Language (from)

    Language (to)

    Description

    zh

    en

    Translation from Chinese to English

    en

    zh

    Translation from English to Chinese

    auto

    en

    Translation from Chinese to English

    auto

    zh

    Translation from English to Chinese