Using Translation API with ABAP SDK for Google Cloud

1. Introduction

This codelab provides steps to call the methods of Translation API V3 (Advanced) using ABAP SDK for Google Cloud. In this codelab we will see, how to translate a text, detect the language of the text and list the supported languages using the Translation API V3

The following Google Cloud services are used in this codelab:

  • Compute Engine
  • Network Services
  • Cloud Shell
  • Cloud Translation API V3

Prerequisites

What you'll build

You'll create the following programs in your SAP system using ABAP SDK for Google Cloud:

  • Translate text into multiple languages using Google's Cloud Translation API V3
  • Detect the language of source text using Google Cloud Translation API V3
  • List the supported languages of Google Cloud Translation API V3

2. Requirements

  • A browser, such as Chrome or Firefox.
  • A Google Cloud project with billing enabled or Create a 90-Day Free Trial account for Google Cloud Platform.
  • SAP GUI (Windows or Java) installed in your system. If SAP GUI is already installed on your machine connect to SAP using the VM external IP address as the Application Server IP. If you are on Mac, then you can also install the SAP GUI for Java available in this link.

3. Before you begin

6757b2fb50ddcc2d.png

  • Run the following commands to authenticate for your account and set the default project to abap-sdk-poc. Zone us-west4-b is used as an example. If needed, please change the project and zone in the following commands based on your preference.
gcloud auth login
gcloud config set project abap-sdk-poc
gcloud config set compute/zone us-west4-b
  • You must have access to an SAP system with the ABAP SDK for Google Cloud installed.
  • You must complete codelab 1 (Install ABAP Platform Trial 1909 on Google Cloud Platform and Install ABAP SDK for Google Cloud) and codelab 2 (Configure ABAP SDK Authentication using tokens for SAP Hosted on Compute Engine VM) before proceeding with this codelab.
  • If you have completed codelab 1 and codelab 2, this would have provisioned you with an ABAP Platform Trial 1909 System on Google Cloud, along with the required setup for authentication and connectivity.
  • If you have not completed codelab 1 and codelab 2, you will not have all the required infrastructure and connectivity to perform the steps provided in this codelab. Therefore, you must complete codelab 1 and codelab 2 before proceeding with this codelab.

4. Enable Cloud Translation API V3 in your Google Cloud Project

  1. Execute the below command in the Cloud Shell, this will enable the Cloud Translation API in your Google Cloud Project:
gcloud services enable translate.googleapis.com

On successful execution you should see a message displayed as shown below:

b5f52859df2c2f56.png

You should now have the Cloud Translation API enabled in your Google Cloud Project

5. Create a Service Account with Cloud Translation User Role

To create a service account with required role, perform the following steps:

  1. Run the following command in the Cloud Shell terminal:
gcloud iam service-accounts create abap-sdk-translation-tester \
--display-name="Service Account for Translation"
  1. Now add the required roles to the service account created in the above step
gcloud projects add-iam-policy-binding abap-sdk-poc\
--member='serviceAccount:abap-sdk-translation-tester@abap-sdk-poc.iam.gserviceaccount.com' \
--role='roles/cloudtranslate.user'

The above command uses abap-sdk-poc as template name for the Google Cloud Project. Replace it with your project id.

  1. To verify, the role has been added, go to IAM page. The service account you created should be listed along with the role that has been assigned to it as shown below:

6af77a519ed3e357.png

6. Create Client Key Configuration

Now that you have set up the pre-requisites on the Google Cloud side, we can move ahead with the configuration on the SAP side.

For authentication and connectivity related configuration, the ABAP SDK for Google Cloud uses the table /GOOG/CLIENT_KEY

To maintain the configuration in the table /GOOG/CLIENT_KEY table, perform the following steps:

  1. In the SAP GUI, enter transaction code SPRO.
  2. Click SAP Reference IMG.
  3. Click ABAP SDK for Google Cloud > Basic Settings > Configure Client Key.
  4. Maintain the following values against the fields:

Field

Value

Google Cloud Key Name

TEST_TRANSLATION

Google Cloud Service Account Name

abap-sdk-translation-tester@abap-sdk-poc.iam.gserviceaccount.com

Google Cloud Scope

https://www.googleapis.com/auth/cloud-platform

Project ID

abap-sdk-poc

Authorization Class

/GOOG/CL_AUTH_GOOGLE

Leave all other fields blank

7. Create a Z-Report to call the Cloud Translation API V3 and Translate a Text

  1. Log in to your SAP system.
  2. Go to transaction code SE38 and create a Report Program with the name ZDEMO_TRANSLATE_TEXT.
  3. In the pop-up that opens up, provide details as shown below, and click Save.

6ba124f9187d1fc0.png

In the next pop-up either select Local Object or Provide a package name as per your choice.

  1. In the ABAP Editor, add the following code:
DATA lo_translate TYPE REF TO /goog/cl_translation_v3.
DATA ls_input TYPE /goog/cl_translation_v3=>ty_050.

TRY.
    lo_translate = NEW #( iv_key_name = 'TEST_TRANSLATION').

    ls_input = VALUE #(  mime_type = 'text/plain'
                         source_language_code = 'en-US'
                         target_language_code = 'es-ES'
                         contents =  VALUE #( ( |Sun is our nearest star| ) ) ).

    lo_translate->translate_text_projects(
      EXPORTING
        iv_p_projects_id = CONV #( lo_translate->gv_project_id )
        is_input         = ls_input
      IMPORTING
        es_output        =  DATA(ls_output)
        ev_ret_code      =  DATA(lv_ret_code)
        ev_err_text      =  DATA(lv_err_text)
        es_err_resp      =  DATA(lv_err_resp)
    ).
    IF lo_translate->is_success( lv_ret_code ) = abap_true.
      cl_demo_output=>new(
      )->begin_section( 'API Call Successful:'
      )->write_text( 'Translated Text:'
      )->write_data( ls_output-translations[ 1 ]-translated_text
      )->display( ).
    ELSE.
      cl_demo_output=>new(
      )->begin_section( 'API Call Unsuccessful:'
      )->write_text( 'Error Message:'
      )->write_text( lv_err_text
      )->display( ).
    ENDIF.
  CATCH /goog/cx_sdk INTO DATA(lo_sdk_excp).
    DATA(lv_error) = lo_sdk_excp->get_text( ).
    cl_demo_output=>new(
     )->begin_section( 'Exception Occured:'
     )->write_text( lv_error
     )->display( ).
ENDTRY.
  1. Save and activate the Report.
  2. Execute the report (F8).

On successful execution you should see the report output as shown below:

2dd3bf270d2b1477.png

8. Create a Z-Report to call the Cloud Translation API V3 and Detect the language

  1. Log in to your SAP system.
  2. Go to transaction code SE38 and create a Report Program with the name ZDEMO_DETECT_LANGUAGE.
  3. In the pop-up that opens up, provide details as shown below, and click Save:

64c2eb0e0173ae51.png

In the next pop-up either select Local Object or Provide a package name as per your choice.

  1. In the ABAP Editor, add the following code:
DATA lo_translate TYPE REF TO /goog/cl_translation_v3.
DATA ls_input TYPE /goog/cl_translation_v3=>ty_009.

TRY.
    lo_translate = NEW #( iv_key_name = 'TEST_TRANSLATION').

    ls_input = VALUE #(  mime_type = |text/plain|
                         content =  |La luce viaggia più veloce del suono| ).

    lo_translate->detect_language_projects(
      EXPORTING
        iv_p_projects_id = CONV #( lo_translate->gv_project_id )
        is_input         = ls_input
      IMPORTING
        es_output        =  DATA(ls_output)
        ev_ret_code      =  DATA(lv_ret_code)
        ev_err_text      =  DATA(lv_err_text)
        es_err_resp      =  DATA(lv_err_resp)
    ).
    IF lo_translate->is_success( lv_ret_code ) = abap_true.
      cl_demo_output=>new(
      )->begin_section( 'API Call Successful:'
      )->write_text( 'Detected Language:'
      )->write_data( ls_output-languages[ 1 ]-language_code
      )->display( ).
    ELSE.
      cl_demo_output=>new(
      )->begin_section( 'API Call Unsuccessful:'
      )->write_text( 'Error Message:'
      )->write_text( lv_err_text
      )->display( ).
    ENDIF.
  CATCH /goog/cx_sdk INTO DATA(lo_sdk_excp).
    DATA(lv_error) = lo_sdk_excp->get_text( ).
    cl_demo_output=>new(
     )->begin_section( 'Exception Occured:'
     )->write_text( lv_error
     )->display( ).
ENDTRY.
  1. Save and activate the Report.
  2. Execute the report (F8).

On successful execution you should see the report output as shown below:

a36e48bded53e62d.png

9. Create a Z-Report to Call the GET_SUPPORTED_LANGUAGES Method

  1. Log in to your SAP System
  2. Go to transaction code SE38 and create a Report Program with the name ZDEMO_GET_LANGUAGES.
  3. In the pop-up that opens up, provide details as shown below and click Save.

3819fc355cf2cd4d.png

In the next pop-up either select Local Object or Provide a package name as per your choice.

  1. In the ABAP Editor, add the following code:
DATA lo_translate TYPE REF TO /goog/cl_translation_v3.

TRY.
    lo_translate = NEW #( iv_key_name = 'TEST_TRANSLATION').

    lo_translate->get_supported_languages_pro(
      EXPORTING
        iv_q_displaylanguagecode = 'en-US'
        iv_p_projects_id         =  CONV #( lo_translate->gv_project_id )
      IMPORTING
        es_output        =  DATA(ls_output)
        ev_ret_code      =  DATA(lv_ret_code)
        ev_err_text      =  DATA(lv_err_text)
        es_err_resp      =  DATA(lv_err_resp)
    ).

    IF lo_translate->is_success( lv_ret_code ) = abap_true.
      cl_demo_output=>new(
      )->begin_section( 'API Call Successful:'
      )->write_text( 'Supported Languages:'
      )->write_data( ls_output-languages
      )->display( ).
    ELSE.
      cl_demo_output=>new(
      )->begin_section( 'API Call Unsuccessful:'
      )->write_text( 'Error Message:'
      )->write_text( lv_err_text
      )->display( ).
    ENDIF.
  CATCH /goog/cx_sdk INTO DATA(lo_sdk_excp).
    DATA(lv_error) = lo_sdk_excp->get_text( ).
    cl_demo_output=>new(
     )->begin_section( 'Exception Occured:'
     )->write_text( lv_error
     )->display( ).
ENDTRY.
  1. Save and activate the Report.
  2. Execute the report (F8).

On successful execution you should see the report output as shown below:

78199926919e7801.png

10. Congratulations

Congratulations! You have successfully completed the "Using Translation API with ABAP SDK for Google Cloud" codelab.

Cloud Translation API V3 also known as Cloud Translation API Advanced has a lot of features including:

  • Translate with custom Models
  • Translate using Glossaries
  • Transliteration Support
  • Document Translation

You can now proceed with the following codelabs to continue with your learning journey of using ABAP SDK for Google Cloud to access various Google Cloud Services.

  • Send an event to Pub/Sub
  • Receive an event from Cloud Pub/Sub
  • Upload a large object to a Cloud Storage bucket using chunking
  • Use DLP API for PII redaction
  • Call BigQuery ML from ABAP

11. Clean up

If you do not wish to continue with the additional codelabs related to ABAP SDK for Google Cloud, please proceed with the cleanup.

Delete the project

  • Delete the Google Cloud project:
gcloud projects delete abap-sdk-poc

Delete individual resources

  1. Delete the compute instance:
gcloud compute instances delete abap-trial-docker
  1. Delete the firewall-rules:
gcloud compute firewall-rules delete sapmachine
  1. Delete the service account:
gcloud iam service-accounts delete \
    abap-sdk-dev@abap-sdk-poc.iam.gserviceaccount.com