All Products
Search
Document Center

Machine Translation:C++ SDK

Last Updated:Jul 18, 2023

To compile on the Linux platform, you must install the dependent external library files libcurl, libopenssl, libuuid, and libjsoncpp.

  • You must use C++ 11 or later.

    • Windows: Visual Studio 2015 or above

    • Linux: GCC 4.9 or later

  • You must use CMake 3.0 or later.

  • A memory of at least 4 GB is recommended.

(1) Download or Git clone aliyun-openapi-cpp-sdk from GitHub

  • Download https://github.com/aliyun/aliyun-openapi-cpp-sdk/archive/master.zip directly

  • Use the Git command to obtain the

git clone https://github.com/aliyun/aliyun-openapi-cpp-sdk.git

(2) Create and generate the necessary build files.

cd aliyun-openapi-cpp-sdk
mkdir sdk_build

The main file structure used by the Machine Translation C++ SDK is as follows:

- /aliyun-openapi-cpp-sdk
--- /alimt
--- /core
--- /sdk_build
--- easyinstall.sh
--- CMakeLists.txt

(3) CMakeLists.txt is replaced with the following file

cmake_minimum_required(VERSION 3.1)

cmake_policy(SET CMP0048 NEW)

file(STRINGS "VERSION" version)

project(alibabacloud-sdk VERSION ${version})

message(STATUS "Project version: ${PROJECT_VERSION}")

set(TARGET_OUTPUT_NAME_PREFIX "alibabacloud-sdk-" CACHE STRING "The target's output name prefix")
option(BUILD_SHARED_LIBS  "Enable shared library" ON)
option(BUILD_UNIT_TESTS "Enable unit tests" OFF)
option(BUILD_FUNCTION_TESTS "Enable function test" OFF)

set(LIB_TYPE STATIC)
if(BUILD_SHARED_LIBS)
        set(LIB_TYPE SHARED)
        add_definitions(-DALIBABACLOUD_SHARED)
endif()

set_property(GLOBAL
        PROPERTY
        USE_FOLDERS ON)

set(CMAKE_CXX_STANDARD 11)

include(ExternalProject)
include(GNUInstallDirs)

add_subdirectory(3rdparty)
add_subdirectory(core)

if(BUILD_UNIT_TESTS)
        enable_testing()
        add_subdirectory(test/core)
endif()

if(BUILD_FUNCTION_TESTS)
  enable_testing()
  add_subdirectory(test/function_test/cdn)
  add_subdirectory(test/function_test/core)
  add_subdirectory(test/function_test/cs)
  add_subdirectory(test/function_test/ecs)
  add_subdirectory(test/function_test/nlp)
  add_subdirectory(test/function_test/rds)
  add_subdirectory(test/function_test/slb)
  add_subdirectory(test/function_test/vpc)
endif()

add_subdirectory(alimt)

(4)One-click installation via easyinstall.sh

cd aliyun-openapi-cpp-sdk
sudo sh easyinstall.sh

Alibaba Cloud SDK for C ++ will be installed in/usr.

Procedure

To get started with the Machine Translation C++ SDK, perform the following steps:

Step 1 Create an Alibaba Cloud account

For more information, see Alibaba Cloud account registration process.

We recommend that you complete real-name verification at your earliest opportunity. Otherwise, you cannot use some Alibaba Cloud services.

Step 2: Obtain an Alibaba Cloud AccessKey pair

To use the Machine Translation C++ SDK, you must apply for the AccessKey pair.

Log on to the KMS page. Select an AccessKey pair for AIRec SDK for Java. If no AccessKey pair is available, create an AccessKey pair and make sure that the pair is in the enabled state. Keys refer to Access Key ID and Access Key Secret

The AccessKey pair will be used in the following steps and must be kept confidential.

Step 3 Starting a C ++ program

Now you can start using the C++ SDK. Run the following sample code to interact with the machine translation server and obtain the corresponding output.

#include <iostream>
#include <alibabacloud/core/AlibabaCloud.h>
#include <alibabacloud/alimt/AlimtClient.h>

using namespace AlibabaCloud;
using namespace AlibabaCloud::Alimt;

int main(const std::string & src, std::string & res, const char * destLanguage, const char * srcLanguage)
{
  // Initialize the SDK
  AlibabaCloud::InitializeSdk();

  // Configure the ECS instance
  ClientConfiguration configuration("cn-hangzhou");
  AlimtClient client("<your-access-key-id>", "<your-access-key-secret>", configuration);

  // Create an API request and set parameters
  std::string strDest = gbk2utf8(src.c_str());

  Alimt::Model::TranslateGeneralRequest request;
  request.setSourceLanguage(srcLanguage);
  //request.setScene("title");
  request.setSourceText(strDest);
  request.setFormatType("text");
  request.setTargetLanguage(destLanguage);

  AlimtClient::TranslateGeneralOutcome outCom = client.translateGeneral(request);
  if (outCom.isSuccess())
  {
    int nResCode = outCom.result().getCode();
    res = outCom.result().getData().translated.c_str();
    return true;
  }
  return false;
}